Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
19 changes: 17 additions & 2 deletions core/src/components/alert/alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -846,12 +846,27 @@ export class Alert implements ComponentInterface, OverlayInterface {
}

private renderAlertButtons() {
const buttons = this.processedButtons;
const { processedButtons, isButtonGroupWrapped } = this;
const mode = getIonMode(this);
const isStacked = processedButtons.length > 2 || isButtonGroupWrapped;
const alertButtonGroupClass = {
'alert-button-group': true,
'alert-button-group-vertical': buttons.length > 2 || this.isButtonGroupWrapped,
'alert-button-group-vertical': isStacked,
};

/**
* Stacked buttons render the cancel button last, below the other
* actions, which keep the order they were passed in. Reorder the
* DOM rather than use CSS `order` so the tab order matches what
* is on screen.
*/
const buttons = isStacked
? [
...processedButtons.filter((button) => button.role !== 'cancel'),
...processedButtons.filter((button) => button.role === 'cancel'),
]
: processedButtons;

return (
<div class={alertButtonGroupClass} ref={(el) => (this.buttonGroupEl = el)}>
{buttons.map((button) => (
Expand Down
69 changes: 69 additions & 0 deletions core/src/components/alert/test/basic/alert.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,75 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ config, title }) =>
});
});

/**
* This behavior does not vary across directions.
*/
configs({ directions: ['ltr'] }).forEach(({ config, title }) => {
test.describe(title('alert: stacked button order'), () => {
const openAlert = async (page: E2EPage, buttons: unknown[]) => {
test.info().annotations.push({
type: 'issue',
description: 'https://github.com/ionic-team/ionic-framework/issues/26367',
});

await page.setContent(
`
<ion-alert header="Alert"></ion-alert>

<script>
const alert = document.querySelector('ion-alert');
alert.buttons = ${JSON.stringify(buttons)};
</script>
`,
config
);

const ionAlertDidPresent = await page.spyOnEvent('ionAlertDidPresent');
await page.locator('ion-alert').evaluate((el: HTMLIonAlertElement) => el.present());
await ionAlertDidPresent.next();
};

/**
* Returns the button labels from top to bottom, and checks
* that the DOM order matches so tab order follows the screen.
*/
const getStackedOrder = async (page: E2EPage) => {
const buttons = page.locator('ion-alert .alert-button');
const domOrder = await buttons.allTextContents();
const tops = await buttons.evaluateAll((els) => els.map((el) => el.getBoundingClientRect().top));
expect(tops).toEqual([...tops].sort((a, b) => a - b));
return domOrder;
};

test('should render the cancel button last when two buttons wrap', async ({ page }) => {
await openAlert(page, [
{ text: 'Cancel Cancel Cancel Cancel', role: 'cancel' },
{ text: 'Confirm Confirm Confirm Confirm' },
]);

await expect(page.locator('ion-alert .alert-button-group')).toHaveClass(/alert-button-group-vertical/);
expect(await getStackedOrder(page)).toEqual(['Confirm Confirm Confirm Confirm', 'Cancel Cancel Cancel Cancel']);
});

test('should render the cancel button last when there are more than two buttons', async ({ page }) => {
await openAlert(page, [
{ text: 'Cancel', role: 'cancel' },
{ text: 'Regular' },
{ text: 'Delete', role: 'destructive' },
]);

expect(await getStackedOrder(page)).toEqual(['Regular', 'Delete', 'Cancel']);
});

test('should keep the given order when two buttons do not wrap', async ({ page }) => {
await openAlert(page, [{ text: 'Cancel', role: 'cancel' }, { text: 'OK' }]);

await expect(page.locator('ion-alert .alert-button-group')).not.toHaveClass(/alert-button-group-vertical/);
expect(await page.locator('ion-alert .alert-button').allTextContents()).toEqual(['Cancel', 'OK']);
});
});
});

configs({ palettes: ['light', 'dark'] }).forEach(({ config, screenshot, title }) => {
test.describe(title('should not have visual regressions'), () => {
test('more than two buttons', async ({ page }) => {
Expand Down
3 changes: 2 additions & 1 deletion core/src/components/alert/test/basic/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@
header: 'Alert!',
buttons: [
{
text: 'Action Action Action Action',
text: 'Cancel Cancel Cancel Cancel',
role: 'cancel',
},
{
text: 'Action',
Expand Down
Loading