Skip to content

Commit 10833e4

Browse files
committed
Disable auto-focus on confirmation modal children by default
1 parent 36dec3c commit 10833e4

File tree

7 files changed

+68
-18
lines changed

7 files changed

+68
-18
lines changed

packages/compass-components/src/hooks/use-confirmation.spec.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ describe('use-confirmation', function () {
4343
within(modal).getByText('This action can not be undone.')
4444
).to.exist;
4545
expect(within(modal).getByText('Yes')).to.exist;
46-
expect(within(modal).getByText('Cancel')).to.exist;
46+
const cancelElement = within(modal).getByText('Cancel');
47+
expect(cancelElement).to.exist;
48+
expect(cancelElement.parentElement).to.equal(document.activeElement);
4749
});
4850

4951
it('handles cancel action', async function () {
@@ -78,10 +80,13 @@ describe('use-confirmation', function () {
7880
screen.getByRole('button', { name: 'Confirm' })
7981
).to.have.attribute('aria-disabled', 'true');
8082

81-
userEvent.type(
82-
screen.getByRole('textbox', { name: /Type "Yes"/ }),
83-
'Yes'
84-
);
83+
const textInputElement = screen.getByRole('textbox', {
84+
name: /Type "Yes"/,
85+
});
86+
87+
// The input should have focus
88+
expect(textInputElement).to.equal(document.activeElement);
89+
userEvent.type(textInputElement, 'Yes');
8590

8691
expect(
8792
screen.getByRole('button', { name: 'Confirm' })

packages/compass-components/src/hooks/use-confirmation.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ export { ConfirmationModalVariant };
1818
type ConfirmationModalProps = React.ComponentProps<typeof ConfirmationModal>;
1919

2020
type ConfirmationProperties = Partial<
21-
Pick<ConfirmationModalProps, 'title' | 'variant' | 'requiredInputText'>
21+
Pick<
22+
ConfirmationModalProps,
23+
'title' | 'variant' | 'requiredInputText' | 'initialFocus'
24+
>
2225
> & {
2326
buttonText?: React.ReactNode;
2427
confirmButtonProps?: Omit<ButtonProps, 'onClick'>;
@@ -196,6 +199,13 @@ const ConfirmationModalStateHandler: React.FunctionComponent = ({
196199
onClick: handleCancel,
197200
}}
198201
requiredInputText={confirmationProps.requiredInputText ?? undefined}
202+
initialFocus={
203+
confirmationProps.initialFocus ??
204+
(confirmationProps.requiredInputText
205+
? 'auto'
206+
: // TODO: Update this once https://jira.mongodb.org/browse/LG-5735 gets resolved
207+
'[data-testid=lg-confirmation_modal-footer-cancel_button]')
208+
}
199209
>
200210
{confirmationProps.description}
201211
{confirmationProps.warning && (
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {
2+
render,
3+
screen,
4+
waitFor,
5+
within,
6+
userEvent,
7+
} from '@mongodb-js/testing-library-compass';
8+
import { expect } from 'chai';
9+
import React from 'react';
10+
11+
import { ConfirmationModalArea } from './use-confirmation';
12+
import { showErrorDetails } from './use-error-details';
13+
14+
describe('use-error-details', function () {
15+
context('showErrorDetails global function', function () {
16+
let modal: HTMLElement;
17+
beforeEach(async function () {
18+
render(
19+
<ConfirmationModalArea>
20+
<button
21+
type="button"
22+
onClick={() => {
23+
showErrorDetails({
24+
details: { oh: 'noes' },
25+
closeAction: 'back',
26+
});
27+
}}
28+
>
29+
Open Modal
30+
</button>
31+
</ConfirmationModalArea>
32+
);
33+
userEvent.click(screen.getByText('Open Modal'));
34+
await waitFor(() => {
35+
modal = screen.getByTestId('confirmation-modal');
36+
});
37+
});
38+
39+
it('renders modal with cancel button focused', function () {
40+
expect(within(modal).getByText('Error details')).to.exist;
41+
const confirmElement = within(modal).getByText('Back');
42+
expect(confirmElement).to.exist;
43+
expect(confirmElement.parentElement).to.equal(document.activeElement);
44+
});
45+
});
46+
});

packages/compass-components/src/hooks/use-error-details.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const showErrorDetails = function showErrorDetails({
2525
buttonText: closeAction.replace(/\b\w/g, (c) => c.toUpperCase()),
2626
confirmButtonProps: {
2727
variant: ButtonVariant.Default,
28-
autoFocus: true,
2928
},
29+
initialFocus: '[data-testid=lg-confirmation_modal-footer-confirm_button]',
3030
});
3131
};

packages/compass-e2e-tests/tests/collection-aggregations-tab.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -676,10 +676,6 @@ describe('Collection aggregations tab', function () {
676676
const errorDetailsJson = browser.$(Selectors.ErrorDetailsJson);
677677
await errorDetailsJson.waitForDisplayed();
678678

679-
// exit details
680-
// leafygreen autofocus triggers a tooltip on the error code element,
681-
// "Tab" to remove the focus
682-
await browser.keys('Tab');
683679
// now click the close button
684680
await browser.clickVisible(Selectors.confirmationModalConfirmButton());
685681
// wait for the modal to go away

packages/compass-e2e-tests/tests/collection-documents-tab.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -727,10 +727,6 @@ FindIterable<Document> result = collection.find(filter);`);
727727
const errorDetailsJson = browser.$(Selectors.ErrorDetailsJson);
728728
await errorDetailsJson.waitForDisplayed();
729729

730-
// exit details
731-
// leafygreen autofocus triggers a tooltip on the error code element,
732-
// "Tab" to remove the focus
733-
await browser.keys('Tab');
734730
// now click the close button
735731
await browser.clickVisible(Selectors.confirmationModalConfirmButton());
736732
// wait for the modal to go away

packages/compass-e2e-tests/tests/collection-import.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,6 @@ describe('Collection import', function () {
597597
expect(await errorDetailsJson.getText()).to.include(
598598
'schemaRulesNotSatisfied'
599599
);
600-
// leafygreen autofocus triggers a tooltip on the error code element,
601-
// "Tab" to remove the focus
602-
await browser.keys('Tab');
603600
// now click the close button
604601
await browser.clickVisible(Selectors.confirmationModalConfirmButton());
605602
// wait for the modal to go away

0 commit comments

Comments
 (0)