Skip to content

Commit 53fc9d0

Browse files
committed
feat(WarningModal): Add warning modal component
1 parent 695905d commit 53fc9d0

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
# Sidenav top-level section
3+
# should be the same for all markdown files
4+
section: extensions
5+
subsection: Component groups
6+
# Sidenav secondary level section
7+
# should be the same for all markdown files
8+
id: Warning modal
9+
# Tab (react | react-demos | html | html-demos | design-guidelines | accessibility)
10+
source: react
11+
# If you use typescript, the name of the interface to display props for
12+
# These are found through the sourceProps function provided in patternfly-docs.source.js
13+
propComponents: ['WarningModal']
14+
---
15+
16+
import WarningModal from '@patternfly/react-component-groups/dist/dynamic/WarningModal';
17+
18+
A **warning modal** component displays a modal asking user to confirm his intention to perform a risky action.
19+
20+
## Examples
21+
22+
### Basic warning modal
23+
24+
A basic warning modal component provides users with a basic layout to which only specific texts need to be passed.
25+
Action buttons callbacks can be customized using `onConfirm` and `onClose`.
26+
27+
```js file="./WarningModalExample.tsx"
28+
29+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
import WarningModal from '@patternfly/react-component-groups/dist/dynamic/WarningModal';
3+
import { Button } from '@patternfly/react-core';
4+
5+
export const BasicExample: React.FunctionComponent = () => {
6+
const [ isOpen, setIsOpen ] = React.useState(false);
7+
return <>
8+
<Button onClick={() => setIsOpen(true)}>Open modal</Button>
9+
<WarningModal
10+
isOpen={isOpen}
11+
title='Unsaved changes'
12+
onClose={() => setIsOpen(false)}
13+
onConfirm={() => setIsOpen(false)}>
14+
Your page contains unsaved changes. Do you want to leave?
15+
</WarningModal>
16+
</>
17+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react';
2+
import { render } from '@testing-library/react';
3+
import WarningModal from './WarningModal';
4+
5+
describe('WarningModal component', () => {
6+
const initialProps = {
7+
onConfirm: jest.fn(),
8+
children: <>By confirming this action, unsaved data may be lost. Do you want to continue?</>
9+
};
10+
11+
it('should render', () => {
12+
const { container } = render(<WarningModal {...initialProps}/>);
13+
expect(container.firstChild).toMatchSnapshot();
14+
});
15+
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import React from 'react';
2+
import { Button, ModalProps, Modal, ModalVariant, ButtonVariant, variantIcons, } from '@patternfly/react-core';
3+
4+
export interface WarningModalProps extends Omit<ModalProps, 'ref'> {
5+
/** Callback for the confirm action button. */
6+
onConfirm?: () => void;
7+
/** Custom label for the confirm action button */
8+
confirmButtonLabel? : string;
9+
/** Custom label for the cancel action button */
10+
cancelButtonLabel? : string;
11+
}
12+
13+
const WarningModal: React.FunctionComponent<WarningModalProps> = ({
14+
isOpen,
15+
onConfirm,
16+
onClose,
17+
children,
18+
confirmButtonLabel = 'Confirm',
19+
cancelButtonLabel = 'Cancel',
20+
variant = ModalVariant.small,
21+
titleIconVariant = 'warning',
22+
...props
23+
}: WarningModalProps) => (
24+
<Modal
25+
variant={variant}
26+
isOpen={isOpen}
27+
onClose={onClose}
28+
onEscapePress={onClose}
29+
titleIconVariant={titleIconVariant}
30+
actions={[
31+
<Button
32+
ouiaId="primary-confirm-button"
33+
key="confirm"
34+
variant={ButtonVariant.primary}
35+
onClick={onConfirm}
36+
>
37+
{confirmButtonLabel}
38+
</Button>,
39+
<Button
40+
ouiaId="secondary-cancel-button"
41+
key="cancel"
42+
variant={ButtonVariant.link}
43+
onClick={onClose}
44+
>
45+
{cancelButtonLabel}
46+
</Button>,
47+
]}
48+
{...props}
49+
>
50+
{children}
51+
</Modal>
52+
);
53+
54+
55+
export default WarningModal;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { default } from './WarningModal';
2+
export * from './WarningModal';

packages/module/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ export * from './TagCount';
3232

3333
export { default as UnavailableContent } from './UnavailableContent';
3434
export * from './UnavailableContent';
35+
36+
export { default as WarningModal } from './WarningModal';
37+
export * from './WarningModal';

0 commit comments

Comments
 (0)