fix(radio-group): pressing spacebar correctly unselects radio with allow-empty-selection (#23194)

resolves #22734
This commit is contained in:
William Martin
2021-04-21 14:34:27 -04:00
committed by GitHub
parent a31fb55bac
commit 7139b3f39e
2 changed files with 92 additions and 3 deletions

View File

@@ -141,10 +141,11 @@ export class RadioGroup implements ComponentInterface {
}
// Update the radio group value when a user presses the
// space bar on top of a selected radio (only applies
// to radios in a select popover)
// space bar on top of a selected radio
if (['Space'].includes(ev.code)) {
this.value = current.value;
this.value = (this.allowEmptySelection && this.value !== undefined)
? undefined
: current.value;
// Prevent browsers from jumping
// to the bottom of the screen

View File

@@ -0,0 +1,88 @@
import { newE2EPage } from '@stencil/core/testing';
/**
* @param page the E2E page that contains the radio button
* @param radioButtonId the id of the radio button to focus
* @returns the checked property of the focused radio button
*/
const selectRadio = async (page, radioButtonId: string, selectionMethod: 'keyboard' | 'mouse'): Promise<boolean> => {
const selector = `ion-radio#${radioButtonId}`;
if (selectionMethod === 'keyboard') {
await page.focus(selector);
await page.keyboard.press('Space');
} else if (selectionMethod === 'mouse') {
await page.click(selector);
}
await page.waitForChanges();
const radioGroup = await page.find(`ion-radio#${radioButtonId} >>> input`);
const checked = await radioGroup.getProperty('checked');
return checked;
}
describe('radio-group', () => {
it('Spacebar should not deselect without allowEmptySelection', async () => {
const page = await newE2EPage();
await page.setContent(`
<ion-radio-group value="one" allow-empty-selection="false">
<ion-item>
<ion-label>One</ion-label>
<ion-radio id="one" value="one"></ion-radio>
</ion-item>
</ion-radio-group>
`);
const checked = await selectRadio(page, 'one', 'keyboard');
expect(checked).toBe(true);
});
it('Spacebar should deselect with allowEmptySelection', async () => {
const page = await newE2EPage();
await page.setContent(`
<ion-radio-group value="one" allow-empty-selection="true">
<ion-item>
<ion-label>One</ion-label>
<ion-radio id="one" value="one"></ion-radio>
</ion-item>
</ion-radio-group>
`);
const checked = await selectRadio(page, 'one', 'keyboard');
expect(checked).toBe(false);
});
it('Click should not deselect without allowEmptySelection', async () => {
const page = await newE2EPage();
await page.setContent(`
<ion-radio-group value="one" allow-empty-selection="false">
<ion-item>
<ion-label>One</ion-label>
<ion-radio id="one" value="one"></ion-radio>
</ion-item>
</ion-radio-group>
`);
const checked = await selectRadio(page, 'one', 'mouse');
expect(checked).toBe(true);
});
it('Click should deselect with allowEmptySelection', async () => {
const page = await newE2EPage();
await page.setContent(`
<ion-radio-group value="one" allow-empty-selection="true">
<ion-item>
<ion-label>One</ion-label>
<ion-radio id="one" value="one"></ion-radio>
</ion-item>
</ion-radio-group>
`);
const checked = await selectRadio(page, 'one', 'mouse');
expect(checked).toBe(false);
});
});