Skip to content

Commit 72b3899

Browse files
authored
fix(alert): stop Enter keypress for checkboxes (#28279)
1 parent 897ff6f commit 72b3899

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

core/src/components/alert/alert.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,15 @@ export class Alert implements ComponentInterface, OverlayInterface {
228228
onKeydown(ev: any) {
229229
const inputTypes = new Set(this.processedInputs.map((i) => i.type));
230230

231+
/**
232+
* Based on keyboard navigation requirements, the
233+
* checkbox should not respond to the enter keydown event.
234+
*/
235+
if (inputTypes.has('checkbox') && ev.key === 'Enter') {
236+
ev.preventDefault();
237+
return;
238+
}
239+
231240
// The only inputs we want to navigate between using arrow keys are the radios
232241
// ignore the keydown event if it is not on a radio button
233242
if (

core/src/components/alert/test/a11y/alert.e2e.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,22 @@ configs({ directions: ['ltr'] }).forEach(({ config, title }) => {
7676
await expect(alertButton).toHaveAttribute('aria-labelledby', 'close-label');
7777
await expect(alertButton).toHaveAttribute('aria-label', 'close button');
7878
});
79+
80+
test('should not toggle the checkbox when pressing the Enter key', async ({ page }) => {
81+
const didPresent = await page.spyOnEvent('ionAlertDidPresent');
82+
83+
const button = page.locator('#checkbox');
84+
await button.click();
85+
86+
await didPresent.next();
87+
88+
const alertCheckbox = page.locator('ion-alert .alert-checkbox');
89+
const ariaChecked = await alertCheckbox.getAttribute('aria-checked');
90+
91+
await expect(alertCheckbox).toHaveAttribute('aria-checked', ariaChecked!);
92+
93+
await alertCheckbox.press('Enter');
94+
await expect(alertCheckbox).toHaveAttribute('aria-checked', ariaChecked!);
95+
});
7996
});
8097
});

core/src/components/alert/test/a11y/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ <h1>Alert - A11y</h1>
2525
<ion-button id="noMessage" expand="block" onclick="presentNoMessage()">No Message</ion-button>
2626
<ion-button id="customAria" expand="block" onclick="presentCustomAria()">Custom Aria</ion-button>
2727
<ion-button id="ariaLabelButton" expand="block" onclick="presentAriaLabelButton()">Aria Label Button</ion-button>
28+
<ion-button id="checkbox" expand="block" onclick="presentAlertCheckbox()">Checkbox</ion-button>
2829
</main>
2930

3031
<script>
@@ -94,6 +95,21 @@ <h1>Alert - A11y</h1>
9495
],
9596
});
9697
}
98+
99+
function presentAlertCheckbox() {
100+
openAlert({
101+
header: 'Checkbox',
102+
inputs: [
103+
{
104+
type: 'checkbox',
105+
label: 'Checkbox 1',
106+
value: 'value1',
107+
checked: true,
108+
},
109+
],
110+
buttons: ['OK'],
111+
});
112+
}
97113
</script>
98114
</body>
99115
</html>

0 commit comments

Comments
 (0)