mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-18 03:00:58 +08:00
feat(alert): add htmlAttributes property for passing attributes to buttons (#27862)
Issue number: N/A --------- ## What is the current behavior? Buttons containing only icons are not accessible as there is no way to pass an `aria-label` attribute (or any other html attribute). ## What is the new behavior? - Adds the `htmlAttributes` property on the `AlertButton` interface - Passes the `htmlAttributes` to the buttons - Adds a test to verify `aria-label` and `aria-labelled-by` are passed to the button ## Does this introduce a breaking change? - [ ] Yes - [x] No
This commit is contained in:
@ -48,6 +48,7 @@ export interface AlertButton {
|
|||||||
role?: 'cancel' | 'destructive' | string;
|
role?: 'cancel' | 'destructive' | string;
|
||||||
cssClass?: string | string[];
|
cssClass?: string | string[];
|
||||||
id?: string;
|
id?: string;
|
||||||
|
htmlAttributes?: { [key: string]: any };
|
||||||
// TODO(FW-2832): type
|
// TODO(FW-2832): type
|
||||||
handler?: (value: any) => AlertButtonOverlayHandler | Promise<AlertButtonOverlayHandler>;
|
handler?: (value: any) => AlertButtonOverlayHandler | Promise<AlertButtonOverlayHandler>;
|
||||||
}
|
}
|
||||||
|
@ -666,6 +666,7 @@ export class Alert implements ComponentInterface, OverlayInterface {
|
|||||||
<div class={alertButtonGroupClass}>
|
<div class={alertButtonGroupClass}>
|
||||||
{buttons.map((button) => (
|
{buttons.map((button) => (
|
||||||
<button
|
<button
|
||||||
|
{...button.htmlAttributes}
|
||||||
type="button"
|
type="button"
|
||||||
id={button.id}
|
id={button.id}
|
||||||
class={buttonClass(button)}
|
class={buttonClass(button)}
|
||||||
|
@ -60,5 +60,21 @@ configs({ directions: ['ltr'] }).forEach(({ config, title }) => {
|
|||||||
test('should allow for manually specifying aria attributes', async ({ page }) => {
|
test('should allow for manually specifying aria attributes', async ({ page }) => {
|
||||||
await testAria(page, 'customAria', 'Custom title', 'Custom description');
|
await testAria(page, 'customAria', 'Custom title', 'Custom description');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should have aria-labelledby and aria-label added to the button when htmlAttributes is set', async ({
|
||||||
|
page,
|
||||||
|
}) => {
|
||||||
|
const didPresent = await page.spyOnEvent('ionAlertDidPresent');
|
||||||
|
|
||||||
|
const button = page.locator('#ariaLabelButton');
|
||||||
|
await button.click();
|
||||||
|
|
||||||
|
await didPresent.next();
|
||||||
|
|
||||||
|
const alertButton = page.locator('ion-alert .alert-button');
|
||||||
|
|
||||||
|
await expect(alertButton).toHaveAttribute('aria-labelledby', 'close-label');
|
||||||
|
await expect(alertButton).toHaveAttribute('aria-label', 'close button');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -24,6 +24,7 @@
|
|||||||
<ion-button id="noHeaders" expand="block" onclick="presentNoHeaders()">No Headers</ion-button>
|
<ion-button id="noHeaders" expand="block" onclick="presentNoHeaders()">No Headers</ion-button>
|
||||||
<ion-button id="noMessage" expand="block" onclick="presentNoMessage()">No Message</ion-button>
|
<ion-button id="noMessage" expand="block" onclick="presentNoMessage()">No Message</ion-button>
|
||||||
<ion-button id="customAria" expand="block" onclick="presentCustomAria()">Custom Aria</ion-button>
|
<ion-button id="customAria" expand="block" onclick="presentCustomAria()">Custom Aria</ion-button>
|
||||||
|
<ion-button id="ariaLabelButton" expand="block" onclick="presentAriaLabelButton()">Aria Label Button</ion-button>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
@ -76,6 +77,23 @@
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function presentAriaLabelButton() {
|
||||||
|
openAlert({
|
||||||
|
header: 'Header',
|
||||||
|
subHeader: 'Subtitle',
|
||||||
|
message: 'This is an alert message with custom aria attributes passed to the button.',
|
||||||
|
buttons: [
|
||||||
|
{
|
||||||
|
text: 'Close',
|
||||||
|
htmlAttributes: {
|
||||||
|
'aria-label': 'close button',
|
||||||
|
'aria-labelledby': 'close-label',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Reference in New Issue
Block a user