-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(WarningModal): Add warning modal component
- Loading branch information
Showing
6 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
...-docs/content/extensions/component-groups/examples/WarningModal/WarningModal.md
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,29 @@ | ||
--- | ||
# Sidenav top-level section | ||
# should be the same for all markdown files | ||
section: extensions | ||
subsection: Component groups | ||
# Sidenav secondary level section | ||
# should be the same for all markdown files | ||
id: Warning modal | ||
# Tab (react | react-demos | html | html-demos | design-guidelines | accessibility) | ||
source: react | ||
# If you use typescript, the name of the interface to display props for | ||
# These are found through the sourceProps function provided in patternfly-docs.source.js | ||
propComponents: ['WarningModal'] | ||
--- | ||
|
||
import WarningModal from '@patternfly/react-component-groups/dist/dynamic/WarningModal'; | ||
|
||
A **warning modal** component displays a modal asking user to confirm his intention to perform a risky action. | ||
|
||
## Examples | ||
|
||
### Basic warning modal | ||
|
||
A basic warning modal component provides users with a basic layout to which only specific texts need to be passed. | ||
Action buttons callbacks can be customized using `onConfirm` and `onClose`. | ||
|
||
```js file="./WarningModalExample.tsx" | ||
|
||
``` |
17 changes: 17 additions & 0 deletions
17
...ly-docs/content/extensions/component-groups/examples/WarningModal/WarningModalExample.tsx
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,17 @@ | ||
import React from 'react'; | ||
import WarningModal from '@patternfly/react-component-groups/dist/dynamic/WarningModal'; | ||
import { Button } from '@patternfly/react-core'; | ||
|
||
export const BasicExample: React.FunctionComponent = () => { | ||
const [ isOpen, setIsOpen ] = React.useState(false); | ||
return <> | ||
<Button onClick={() => setIsOpen(true)}>Open modal</Button> | ||
<WarningModal | ||
isOpen={isOpen} | ||
title='Unsaved changes' | ||
onClose={() => setIsOpen(false)} | ||
onConfirm={() => setIsOpen(false)}> | ||
Your page contains unsaved changes. Do you want to leave? | ||
</WarningModal> | ||
</> | ||
}; |
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,15 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import WarningModal from './WarningModal'; | ||
|
||
describe('WarningModal component', () => { | ||
const initialProps = { | ||
onConfirm: jest.fn(), | ||
children: <>By confirming this action, unsaved data may be lost. Do you want to continue?</> | ||
}; | ||
|
||
it('should render', () => { | ||
const { container } = render(<WarningModal {...initialProps}/>); | ||
expect(container.firstChild).toMatchSnapshot(); | ||
}); | ||
}); |
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,55 @@ | ||
import React from 'react'; | ||
import { Button, ModalProps, Modal, ModalVariant, ButtonVariant, variantIcons, } from '@patternfly/react-core'; | ||
|
||
export interface WarningModalProps extends Omit<ModalProps, 'ref'> { | ||
/** Callback for the confirm action button. */ | ||
onConfirm?: () => void; | ||
/** Custom label for the confirm action button */ | ||
confirmButtonLabel? : string; | ||
/** Custom label for the cancel action button */ | ||
cancelButtonLabel? : string; | ||
} | ||
|
||
const WarningModal: React.FunctionComponent<WarningModalProps> = ({ | ||
isOpen, | ||
onConfirm, | ||
onClose, | ||
children, | ||
confirmButtonLabel = 'Confirm', | ||
cancelButtonLabel = 'Cancel', | ||
variant = ModalVariant.small, | ||
titleIconVariant = 'warning', | ||
...props | ||
}: WarningModalProps) => ( | ||
<Modal | ||
variant={variant} | ||
isOpen={isOpen} | ||
onClose={onClose} | ||
onEscapePress={onClose} | ||
titleIconVariant={titleIconVariant} | ||
actions={[ | ||
<Button | ||
ouiaId="primary-confirm-button" | ||
key="confirm" | ||
variant={ButtonVariant.primary} | ||
onClick={onConfirm} | ||
> | ||
{confirmButtonLabel} | ||
</Button>, | ||
<Button | ||
ouiaId="secondary-cancel-button" | ||
key="cancel" | ||
variant={ButtonVariant.link} | ||
onClick={onClose} | ||
> | ||
{cancelButtonLabel} | ||
</Button>, | ||
]} | ||
{...props} | ||
> | ||
{children} | ||
</Modal> | ||
); | ||
|
||
|
||
export default WarningModal; |
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,2 @@ | ||
export { default } from './WarningModal'; | ||
export * from './WarningModal'; |
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