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

Default ConfirmationDialog to focusing cancel when the confirmation is a dangerous one #2185

Merged
merged 5 commits into from
Aug 1, 2022
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
5 changes: 5 additions & 0 deletions .changeset/lucky-pianos-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Set ConfirmationDialog initial focus based on the confirmationButtonVariant. When `danger` autoFocus the cancel button, otherwise autoFocus the confirmation button
6 changes: 4 additions & 2 deletions src/Dialog/ConfirmationDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,17 @@ export const ConfirmationDialog: React.FC<React.PropsWithChildren<ConfirmationDi
const onConfirmButtonClick = useCallback(() => {
onClose('confirm')
}, [onClose])
const isConfirmationDangerous = confirmButtonType === 'danger'
const cancelButton: DialogButtonProps = {
content: cancelButtonContent,
onClick: onCancelButtonClick
onClick: onCancelButtonClick,
autoFocus: isConfirmationDangerous
}
const confirmButton: DialogButtonProps = {
content: confirmButtonContent,
buttonType: confirmButtonType,
onClick: onConfirmButtonClick,
autoFocus: true
autoFocus: !isConfirmationDangerous
}
const footerButtons = [cancelButton, confirmButton]
return (
Expand Down
25 changes: 23 additions & 2 deletions src/__tests__/ConfirmationDialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {behavesAsComponent, checkExports} from '../utils/testing'

expect.extend(toHaveNoViolations)

const Basic = () => {
const Basic = ({confirmButtonType}: Pick<React.ComponentProps<typeof ConfirmationDialog>, 'confirmButtonType'>) => {
const [isOpen, setIsOpen] = useState(false)
const buttonRef = useRef<HTMLButtonElement>(null)
const onDialogClose = useCallback(() => setIsOpen(false), [])
Expand All @@ -32,6 +32,7 @@ const Basic = () => {
onClose={onDialogClose}
cancelButtonContent="Secondary"
confirmButtonContent="Primary"
confirmButtonType={confirmButtonType}
>
Lorem ipsum dolor sit Pippin good dog.
</ConfirmationDialog>
Expand Down Expand Up @@ -95,7 +96,7 @@ describe('ConfirmationDialog', () => {
cleanup()
})

it('focuses the primary action when opened', async () => {
it('focuses the primary action when opened and the confirmButtonType is not set', async () => {
const {getByText} = HTMLRender(<Basic />)
act(() => {
fireEvent.click(getByText('Show dialog'))
Expand All @@ -105,6 +106,26 @@ describe('ConfirmationDialog', () => {
cleanup()
})

it('focuses the primary action when opened and the confirmButtonType is not danger', async () => {
const {getByText} = HTMLRender(<Basic confirmButtonType="primary" />)
act(() => {
fireEvent.click(getByText('Show dialog'))
})
expect(getByText('Primary')).toEqual(document.activeElement)
expect(getByText('Secondary')).not.toEqual(document.activeElement)
cleanup()
})

it('focuses the secondary action when opened and the confirmButtonType is danger', async () => {
const {getByText} = HTMLRender(<Basic confirmButtonType="danger" />)
act(() => {
fireEvent.click(getByText('Show dialog'))
})
expect(getByText('Primary')).not.toEqual(document.activeElement)
expect(getByText('Secondary')).toEqual(document.activeElement)
cleanup()
})

it('supports nested `focusTrap`s', async () => {
const {getByText} = HTMLRender(<ShorthandHookFromActionMenu />)
act(() => {
Expand Down