-
Notifications
You must be signed in to change notification settings - Fork 545
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(DataTable): add Table.ErrorDialog component (#3276)
* feat(DataTable): add ErrorDialog component * chore: add changeset * chore: remove unused import * Update generated/components.json * chore: address eslint violations --------- Co-authored-by: Josh Black <joshblack@users.noreply.github.com>
- Loading branch information
Showing
9 changed files
with
362 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@primer/react': minor | ||
--- | ||
|
||
Add experimental Table.ErrorDialog component |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from 'react' | ||
import {ConfirmationDialog} from '../Dialog/ConfirmationDialog' | ||
|
||
export type TableErrorDialogProps = React.PropsWithChildren<{ | ||
/** | ||
* Provide an optional title for the dialog | ||
* @default 'Error' | ||
*/ | ||
title?: string | ||
|
||
/** | ||
* Provide an optional handler to be called when the user confirms to retry | ||
*/ | ||
onRetry?: () => void | ||
|
||
/** | ||
* Provide an optional handler to be called when the user dismisses the dialog | ||
*/ | ||
onDismiss?: () => void | ||
}> | ||
|
||
export function ErrorDialog({title = 'Error', children, onRetry, onDismiss}: TableErrorDialogProps) { | ||
return ( | ||
<ConfirmationDialog | ||
title={title} | ||
onClose={gesture => { | ||
if (gesture === 'confirm') { | ||
onRetry?.() | ||
} else { | ||
onDismiss?.() | ||
} | ||
}} | ||
confirmButtonContent="Retry" | ||
cancelButtonContent="Dismiss" | ||
> | ||
{children} | ||
</ConfirmationDialog> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import userEvent from '@testing-library/user-event' | ||
import {render, screen} from '@testing-library/react' | ||
import React from 'react' | ||
import {ErrorDialog} from '../ErrorDialog' | ||
|
||
describe('Table.ErrorDialog', () => { | ||
it('should use a default title of "Error" if `title` is not provided', () => { | ||
render(<ErrorDialog />) | ||
expect( | ||
screen.getByRole('alertdialog', { | ||
name: 'Error', | ||
}), | ||
).toBeInTheDocument() | ||
}) | ||
|
||
it('should allow customizing the title of the dialog through `title`', () => { | ||
const customTitle = 'custom-title' | ||
render(<ErrorDialog title={customTitle} />) | ||
expect( | ||
screen.getByRole('alertdialog', { | ||
name: customTitle, | ||
}), | ||
).toBeInTheDocument() | ||
}) | ||
|
||
it('should use "Retry" as the confirm text of the dialog', () => { | ||
render(<ErrorDialog />) | ||
expect(screen.getByRole('button', {name: 'Retry'})).toBeInTheDocument() | ||
}) | ||
|
||
it('should call `onRetry` if the confirm button is interacted with', async () => { | ||
const user = userEvent.setup() | ||
const onRetry = jest.fn() | ||
|
||
render(<ErrorDialog onRetry={onRetry} />) | ||
await user.click(screen.getByText('Retry')) | ||
expect(onRetry).toHaveBeenCalledTimes(1) | ||
|
||
onRetry.mockClear() | ||
|
||
await user.keyboard('{Enter}') | ||
expect(onRetry).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should set "Dismiss" as the cancel text of the dialog', () => { | ||
render(<ErrorDialog />) | ||
expect(screen.getByRole('button', {name: 'Dismiss'})).toBeInTheDocument() | ||
}) | ||
|
||
it('should call `onDismiss` if the cancel button is interacted with', async () => { | ||
const user = userEvent.setup() | ||
const onDismiss = jest.fn() | ||
|
||
render(<ErrorDialog onDismiss={onDismiss} />) | ||
await user.click(screen.getByText('Dismiss')) | ||
expect(onDismiss).toHaveBeenCalledTimes(1) | ||
|
||
onDismiss.mockClear() | ||
|
||
await user.keyboard('{Enter}') | ||
expect(onDismiss).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should render `children` as the content of the dialog', () => { | ||
render( | ||
<ErrorDialog> | ||
<span data-testid="children">children</span> | ||
</ErrorDialog>, | ||
) | ||
|
||
expect(screen.getByRole('alertdialog', {name: 'Error'})).toContainElement(screen.getByTestId('children')) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.