Skip to content

Commit ce86063

Browse files
committed
fix(radio-group): add Enter key support for selection
1 parent eaface1 commit ce86063

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

core/src/components/radio-group/radio-group.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ export class RadioGroup implements ComponentInterface {
212212
}
213213

214214
// Update the radio group value when a user presses the
215-
// space bar on top of a selected radio
216-
if ([' '].includes(ev.key)) {
215+
// enter or space bar on top of a selected radio
216+
if (['Enter', ' '].includes(ev.key)) {
217217
const previousValue = this.value;
218218
this.value = this.allowEmptySelection && this.value !== undefined ? undefined : current.value;
219219
if (previousValue !== this.value || this.allowEmptySelection) {

core/src/components/radio-group/test/basic/radio-group.e2e.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) =>
2626
config
2727
);
2828

29-
await radioFixture.checkRadio('keyboard');
29+
await radioFixture.checkRadio({ method: 'keyboard', key: 'Space' });
3030
await radioFixture.expectChecked(true);
3131
});
3232

@@ -42,7 +42,39 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) =>
4242
config
4343
);
4444

45-
await radioFixture.checkRadio('keyboard');
45+
await radioFixture.checkRadio({ method: 'keyboard', key: 'Space' });
46+
await radioFixture.expectChecked(false);
47+
});
48+
49+
test('enter should not deselect without allowEmptySelection', async ({ page }) => {
50+
await page.setContent(
51+
`
52+
<ion-radio-group value="one" allow-empty-selection="false">
53+
<ion-item>
54+
<ion-radio id="one" value="one">One</ion-radio>
55+
</ion-item>
56+
</ion-radio-group>
57+
`,
58+
config
59+
);
60+
61+
await radioFixture.checkRadio({ method: 'keyboard', key: 'Enter' });
62+
await radioFixture.expectChecked(true);
63+
});
64+
65+
test('enter should deselect with allowEmptySelection', async ({ page }) => {
66+
await page.setContent(
67+
`
68+
<ion-radio-group value="one" allow-empty-selection="true">
69+
<ion-item>
70+
<ion-radio id="one" value="one">One</ion-radio>
71+
</ion-item>
72+
</ion-radio-group>
73+
`,
74+
config
75+
);
76+
77+
await radioFixture.checkRadio({ method: 'keyboard', key: 'Enter' });
4678
await radioFixture.expectChecked(false);
4779
});
4880

@@ -58,7 +90,7 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) =>
5890
config
5991
);
6092

61-
await radioFixture.checkRadio('mouse');
93+
await radioFixture.checkRadio({ method: 'mouse' });
6294
await radioFixture.expectChecked(true);
6395
});
6496

@@ -74,7 +106,7 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) =>
74106
config
75107
);
76108

77-
await radioFixture.checkRadio('mouse');
109+
await radioFixture.checkRadio({ method: 'mouse' });
78110
await radioFixture.expectChecked(false);
79111
});
80112

core/src/components/radio-group/test/fixtures.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@ import type { Locator } from '@playwright/test';
22
import { expect } from '@playwright/test';
33
import type { E2EPage } from '@utils/test/playwright';
44

5+
interface CheckRadioKeyboardOptions {
6+
method: 'keyboard';
7+
key: 'Space' | 'Enter';
8+
selector?: string;
9+
}
10+
11+
interface CheckRadioMouseOptions {
12+
method: 'mouse';
13+
selector?: string;
14+
}
15+
16+
type CheckRadioOptions = CheckRadioKeyboardOptions | CheckRadioMouseOptions;
17+
518
export class RadioFixture {
619
readonly page: E2EPage;
720

@@ -11,13 +24,14 @@ export class RadioFixture {
1124
this.page = page;
1225
}
1326

14-
async checkRadio(method: 'keyboard' | 'mouse', selector = 'ion-radio') {
27+
async checkRadio(options: CheckRadioOptions) {
1528
const { page } = this;
29+
const selector = options.selector ?? 'ion-radio';
1630
const radio = (this.radio = page.locator(selector));
1731

18-
if (method === 'keyboard') {
32+
if (options.method === 'keyboard') {
1933
await radio.focus();
20-
await page.keyboard.press('Space');
34+
await page.keyboard.press(options.key);
2135
} else {
2236
await radio.click();
2337
}

core/src/components/radio-group/test/search/radio-group.e2e.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) =>
1717
const searchbarInput = page.locator('ion-searchbar input');
1818

1919
// select radio
20-
const radio = await radioFixture.checkRadio('mouse', 'ion-radio[value=two]');
20+
const radio = await radioFixture.checkRadio({ method: 'mouse', selector: 'ion-radio[value=two]' });
2121
await radioFixture.expectChecked(true);
2222

2323
// filter radio so it is not in DOM

0 commit comments

Comments
 (0)