Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(radio): fix radio not showing checked state when not in a group #24423

Merged
merged 4 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core/src/components/radio/radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { createColorClasses, hostContext } from '../../utils/theme';
export class Radio implements ComponentInterface {
private inputId = `ion-rb-${radioButtonIds++}`;
private radioGroup: HTMLIonRadioGroupElement | null = null;
private nativeInput!: HTMLInputElement;

@Element() el!: HTMLIonRadioElement;

Expand Down Expand Up @@ -128,6 +129,10 @@ export class Radio implements ComponentInterface {
}
}

private onClick = () => {
this.checked = this.nativeInput.checked;
};

private onFocus = () => {
this.ionFocus.emit();
}
Expand All @@ -150,6 +155,7 @@ export class Radio implements ComponentInterface {
tabindex={buttonTabindex}
onFocus={this.onFocus}
onBlur={this.onBlur}
onClick={this.onClick}
class={createColorClasses(color, {
[mode]: true,
'in-item': hostContext('ion-item', el),
Expand All @@ -171,6 +177,7 @@ export class Radio implements ComponentInterface {
disabled={disabled}
tabindex="-1"
id={inputId}
ref={el => this.nativeInput = el as HTMLInputElement}
/>
</Host>
);
Expand Down
8 changes: 8 additions & 0 deletions core/src/components/radio/test/basic/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ test('radio: basic', async () => {

const compare = await page.compareScreenshot();
expect(compare).toMatchScreenshot();

const groupedRadio = await page.find('#groupedRadio');
await groupedRadio.click();
expect(groupedRadio).toHaveClass('radio-checked');

const ungroupedRadio = await page.find('#ungroupedRadio');
await ungroupedRadio.click();
expect(ungroupedRadio).toHaveClass('radio-checked');
});

test('radio:rtl: basic', async () => {
Expand Down
4 changes: 2 additions & 2 deletions core/src/components/radio/test/basic/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</ion-item-divider>
<ion-item>
<ion-label>Pepperoni</ion-label>
<ion-radio slot="start" value="pepperoni"></ion-radio>
<ion-radio slot="start" value="pepperoni" id="groupedRadio"></ion-radio>
</ion-item>

<ion-item>
Expand Down Expand Up @@ -137,7 +137,7 @@
</ion-list-header>
<ion-item>
<ion-label>Kiwi, (ionChange) Secondary color</ion-label>
<ion-radio slot="start" color="secondary"></ion-radio>
<ion-radio slot="start" color="secondary" id="ungroupedRadio"></ion-radio>
</ion-item>

<ion-item>
Expand Down