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

Allow initial focus to be provided to confirm prompt #1224

Merged
merged 1 commit into from
Sep 8, 2023
Merged
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: 15 additions & 4 deletions src/util/prompts.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { render } from 'preact';
import { createRef } from 'preact';
import type { RefObject } from 'preact';
import type { ComponentChildren } from 'preact';

import ModalDialog from '../components/feedback/ModalDialog';
Expand All @@ -11,6 +10,12 @@ export type ConfirmModalProps = {
message: ComponentChildren;
confirmAction?: string;
cancelAction?: string;

/**
* Determines which of the two buttons should be initially focused.
* Defaults to 'cancel'
*/
initialFocus?: 'cancel' | 'confirm';
};

/**
Expand All @@ -30,8 +35,13 @@ export async function confirm({
message,
confirmAction = 'Yes',
cancelAction = 'Cancel',
initialFocus = 'cancel',
}: ConfirmModalProps): Promise<boolean> {
const cancelButton = createRef<HTMLElement | undefined>();
const cancelButton = createRef<HTMLElement>();
const confirmButton = createRef<HTMLElement>();
const initialFocusRef =
initialFocus === 'cancel' ? cancelButton : confirmButton;

const container = document.createElement('div');
container.setAttribute('data-testid', 'confirm-container');

Expand Down Expand Up @@ -61,15 +71,16 @@ export async function confirm({
{cancelAction}
</Button>
<Button
data-testid="confirm-button"
variant="primary"
elementRef={confirmButton}
data-testid="confirm-button"
onClick={() => close(true)}
>
{confirmAction}
</Button>
</>
}
initialFocus={cancelButton as RefObject<HTMLElement>}
initialFocus={initialFocusRef}
title={title}
onClose={() => close(false)}
>
Expand Down
19 changes: 19 additions & 0 deletions src/util/test/prompts-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,23 @@ describe('confirm', () => {
clickCancel();
assert.isFalse(await result);
});

[
[undefined, getCancelButton],
['cancel', getCancelButton],
['confirm', getConfirmButton],
].forEach(([initialFocus, getButton]) => {
it('sets initial focus to proper button', async () => {
const result = confirm({ title: 'Confirm action?', initialFocus });

const waitForButtonToFocus = () =>
new Promise(resolve => getButton().addEventListener('focus', resolve));
await waitForButtonToFocus();

assert.equal(document.activeElement, getButton());

clickClose();
await result;
});
});
});