mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-26 08:13:34 +08:00
fix(radio): checked state is updated when value changes (#26936)
This commit is contained in:
@ -60,6 +60,16 @@ export class Radio implements ComponentInterface {
|
||||
*/
|
||||
@Prop() value?: any | null;
|
||||
|
||||
@Watch('value')
|
||||
valueChanged() {
|
||||
/**
|
||||
* The new value of the radio may
|
||||
* match the radio group's value,
|
||||
* so we see if it should be checked.
|
||||
*/
|
||||
this.updateState();
|
||||
}
|
||||
|
||||
/**
|
||||
* Emitted when the styles change.
|
||||
* @internal
|
||||
|
@ -1,4 +1,6 @@
|
||||
import { Radio } from '../radio.tsx';
|
||||
import { RadioGroup } from '../../radio-group/radio-group.tsx';
|
||||
import { newSpecPage } from '@stencil/core/testing';
|
||||
|
||||
describe('ion-radio', () => {
|
||||
it('should set a default value', async () => {
|
||||
@ -8,4 +10,24 @@ describe('ion-radio', () => {
|
||||
|
||||
expect(radio.value).toEqual('ion-rb-0');
|
||||
});
|
||||
|
||||
it('should update the checked state when updating the value', async () => {
|
||||
const page = await newSpecPage({
|
||||
components: [Radio, RadioGroup],
|
||||
html: `
|
||||
<ion-radio-group value="a">
|
||||
<ion-radio value="other-value"></ion-radio>
|
||||
</ion-radio-group>
|
||||
`,
|
||||
});
|
||||
|
||||
const radio = page.root.querySelector('ion-radio');
|
||||
expect(radio.classList.contains('radio-checked')).toBe(false);
|
||||
|
||||
radio.value = 'a';
|
||||
|
||||
await page.waitForChanges();
|
||||
|
||||
expect(radio.classList.contains('radio-checked')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user