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

feat(action-sheet): add htmlAttributes property for passing attributes to buttons #27863

Merged
merged 5 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions core/src/components/action-sheet/action-sheet-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface ActionSheetButton<T = any> {
icon?: string;
cssClass?: string | string[];
id?: string;
htmlAttributes?: { [key: string]: any };
handler?: () => boolean | void | Promise<boolean | void>;
data?: T;
}
15 changes: 13 additions & 2 deletions core/src/components/action-sheet/action-sheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,13 @@ export class ActionSheet implements ComponentInterface, OverlayInterface {
</div>
)}
{buttons.map((b) => (
<button type="button" id={b.id} class={buttonClass(b)} onClick={() => this.buttonClick(b)}>
<button
{...b.htmlAttributes}
brandyscarney marked this conversation as resolved.
Show resolved Hide resolved
type="button"
id={b.id}
class={buttonClass(b)}
onClick={() => this.buttonClick(b)}
>
<span class="action-sheet-button-inner">
{b.icon && <ion-icon icon={b.icon} aria-hidden="true" lazy={false} class="action-sheet-icon" />}
{b.text}
Expand All @@ -398,7 +404,12 @@ export class ActionSheet implements ComponentInterface, OverlayInterface {

{cancelButton && (
<div class="action-sheet-group action-sheet-group-cancel">
<button type="button" class={buttonClass(cancelButton)} onClick={() => this.buttonClick(cancelButton)}>
<button
{...cancelButton.htmlAttributes}
type="button"
class={buttonClass(cancelButton)}
onClick={() => this.buttonClick(cancelButton)}
>
<span class="action-sheet-button-inner">
{cancelButton.icon && (
<ion-icon icon={cancelButton.icon} aria-hidden="true" lazy={false} class="action-sheet-icon" />
Expand Down
43 changes: 41 additions & 2 deletions core/src/components/action-sheet/test/a11y/action-sheet.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,43 @@ const testAria = async (page: E2EPage, buttonID: string, expectedAriaLabelledBy:
await button.click();
await didPresent.next();

const alert = page.locator('ion-action-sheet');
const actionSheet = page.locator('ion-action-sheet');

/**
* expect().toHaveAttribute() can't check for a null value, so grab and check
* the value manually instead.
*/
const ariaLabelledBy = await alert.getAttribute('aria-labelledby');
const ariaLabelledBy = await actionSheet.getAttribute('aria-labelledby');

expect(ariaLabelledBy).toBe(expectedAriaLabelledBy);
};

const testAriaButton = async (
page: E2EPage,
buttonID: string,
expectedAriaLabelledBy: string | null,
expectedAriaLabel: string | null
) => {
const didPresent = await page.spyOnEvent('ionActionSheetDidPresent');

const button = page.locator(`#${buttonID}`);
await button.click();

await didPresent.next();

const actionSheetButton = page.locator('ion-action-sheet .action-sheet-button');

/**
* expect().toHaveAttribute() can't check for a null value, so grab and check
* the value manually instead.
*/
const ariaLabelledBy = await actionSheetButton.getAttribute('aria-labelledby');
brandyscarney marked this conversation as resolved.
Show resolved Hide resolved
expect(ariaLabelledBy).toBe(expectedAriaLabelledBy);

const ariaLabel = await actionSheetButton.getAttribute('aria-label');
expect(ariaLabel).toBe(expectedAriaLabel);
};

configs({ directions: ['ltr'] }).forEach(({ config, title }) => {
test.describe(title('action-sheet: a11y'), () => {
test.beforeEach(async ({ page }) => {
Expand Down Expand Up @@ -52,5 +79,17 @@ configs({ directions: ['ltr'] }).forEach(({ config, title }) => {
test('should allow for manually specifying aria attributes', async ({ page }) => {
await testAria(page, 'customAria', 'Custom title');
});

test('should have aria-labelledby and aria-label added to the button when htmlAttributes is set', async ({
page,
}) => {
await testAriaButton(page, 'ariaLabelButton', 'close-label', 'close button');
});

test('should have aria-labelledby and aria-label added to the cancel button when htmlAttributes is set', async ({
page,
}) => {
await testAriaButton(page, 'ariaLabelCancelButton', 'cancel-label', 'cancel button');
});
});
});
37 changes: 37 additions & 0 deletions core/src/components/action-sheet/test/a11y/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ <h1>Action Sheet - A11y</h1>
<ion-button id="subHeaderOnly" expand="block" onclick="presentSubHeaderOnly()">Subheader Only</ion-button>
<ion-button id="noHeaders" expand="block" onclick="presentNoHeaders()">No Headers</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>
<ion-button id="ariaLabelCancelButton" expand="block" onclick="presentAriaLabelCancelButton()"
>Aria Label Cancel Button</ion-button
>
</main>

<script>
Expand Down Expand Up @@ -63,6 +67,39 @@ <h1>Action Sheet - A11y</h1>
},
});
}

function presentAriaLabelButton() {
openActionSheet({
header: 'Header',
subHeader: 'Subtitle',
buttons: [
{
text: 'Close',
htmlAttributes: {
'aria-label': 'close button',
'aria-labelledby': 'close-label',
},
},
],
});
}

function presentAriaLabelCancelButton() {
openActionSheet({
header: 'Header',
subHeader: 'Subtitle',
buttons: [
{
text: 'Cancel',
role: 'cancel',
htmlAttributes: {
'aria-label': 'cancel button',
'aria-labelledby': 'cancel-label',
liamdebeasi marked this conversation as resolved.
Show resolved Hide resolved
},
},
],
});
}
</script>
</body>
</html>