From 53fc9d06e3f8835b116fc6381f83f2e554b23df1 Mon Sep 17 00:00:00 2001 From: Filip Hlavac Date: Mon, 23 Oct 2023 16:46:39 +0200 Subject: [PATCH 1/2] feat(WarningModal): Add warning modal component --- .../examples/WarningModal/WarningModal.md | 29 ++++++++++ .../WarningModal/WarningModalExample.tsx | 17 ++++++ .../src/WarningModal/WarningModal.test.tsx | 15 +++++ .../module/src/WarningModal/WarningModal.tsx | 55 +++++++++++++++++++ packages/module/src/WarningModal/index.ts | 2 + packages/module/src/index.ts | 3 + 6 files changed, 121 insertions(+) create mode 100644 packages/module/patternfly-docs/content/extensions/component-groups/examples/WarningModal/WarningModal.md create mode 100644 packages/module/patternfly-docs/content/extensions/component-groups/examples/WarningModal/WarningModalExample.tsx create mode 100644 packages/module/src/WarningModal/WarningModal.test.tsx create mode 100644 packages/module/src/WarningModal/WarningModal.tsx create mode 100644 packages/module/src/WarningModal/index.ts diff --git a/packages/module/patternfly-docs/content/extensions/component-groups/examples/WarningModal/WarningModal.md b/packages/module/patternfly-docs/content/extensions/component-groups/examples/WarningModal/WarningModal.md new file mode 100644 index 00000000..21dd2eb8 --- /dev/null +++ b/packages/module/patternfly-docs/content/extensions/component-groups/examples/WarningModal/WarningModal.md @@ -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" + +``` diff --git a/packages/module/patternfly-docs/content/extensions/component-groups/examples/WarningModal/WarningModalExample.tsx b/packages/module/patternfly-docs/content/extensions/component-groups/examples/WarningModal/WarningModalExample.tsx new file mode 100644 index 00000000..92b3079b --- /dev/null +++ b/packages/module/patternfly-docs/content/extensions/component-groups/examples/WarningModal/WarningModalExample.tsx @@ -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 <> + + setIsOpen(false)} + onConfirm={() => setIsOpen(false)}> + Your page contains unsaved changes. Do you want to leave? + + +}; diff --git a/packages/module/src/WarningModal/WarningModal.test.tsx b/packages/module/src/WarningModal/WarningModal.test.tsx new file mode 100644 index 00000000..6eda1571 --- /dev/null +++ b/packages/module/src/WarningModal/WarningModal.test.tsx @@ -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(); + expect(container.firstChild).toMatchSnapshot(); + }); +}); \ No newline at end of file diff --git a/packages/module/src/WarningModal/WarningModal.tsx b/packages/module/src/WarningModal/WarningModal.tsx new file mode 100644 index 00000000..d36c50e7 --- /dev/null +++ b/packages/module/src/WarningModal/WarningModal.tsx @@ -0,0 +1,55 @@ +import React from 'react'; +import { Button, ModalProps, Modal, ModalVariant, ButtonVariant, variantIcons, } from '@patternfly/react-core'; + +export interface WarningModalProps extends Omit { + /** 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 = ({ + isOpen, + onConfirm, + onClose, + children, + confirmButtonLabel = 'Confirm', + cancelButtonLabel = 'Cancel', + variant = ModalVariant.small, + titleIconVariant = 'warning', + ...props +}: WarningModalProps) => ( + + {confirmButtonLabel} + , + , + ]} + {...props} + > + {children} + +); + + +export default WarningModal; diff --git a/packages/module/src/WarningModal/index.ts b/packages/module/src/WarningModal/index.ts new file mode 100644 index 00000000..c640c292 --- /dev/null +++ b/packages/module/src/WarningModal/index.ts @@ -0,0 +1,2 @@ +export { default } from './WarningModal'; +export * from './WarningModal'; diff --git a/packages/module/src/index.ts b/packages/module/src/index.ts index 437bd712..e5905892 100644 --- a/packages/module/src/index.ts +++ b/packages/module/src/index.ts @@ -32,3 +32,6 @@ export * from './TagCount'; export { default as UnavailableContent } from './UnavailableContent'; export * from './UnavailableContent'; + +export { default as WarningModal } from './WarningModal'; +export * from './WarningModal'; From 329ce6c255d899c9d11d155b0dda1d6e267b38fc Mon Sep 17 00:00:00 2001 From: Filip Hlavac Date: Mon, 23 Oct 2023 17:16:57 +0200 Subject: [PATCH 2/2] Fix snapshot testing --- .../src/WarningModal/WarningModal.test.tsx | 9 +- .../module/src/WarningModal/WarningModal.tsx | 2 +- .../__snapshots__/WarningModal.test.tsx.snap | 183 ++++++++++++++++++ 3 files changed, 189 insertions(+), 5 deletions(-) create mode 100644 packages/module/src/WarningModal/__snapshots__/WarningModal.test.tsx.snap diff --git a/packages/module/src/WarningModal/WarningModal.test.tsx b/packages/module/src/WarningModal/WarningModal.test.tsx index 6eda1571..9467870e 100644 --- a/packages/module/src/WarningModal/WarningModal.test.tsx +++ b/packages/module/src/WarningModal/WarningModal.test.tsx @@ -4,12 +4,13 @@ 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? + title: 'Unsaved changes', + onClose: () => null, + onConfirm: () => null, }; it('should render', () => { - const { container } = render(); - expect(container.firstChild).toMatchSnapshot(); + const container = render(Warning modal content); + expect(container).toMatchSnapshot(); }); }); \ No newline at end of file diff --git a/packages/module/src/WarningModal/WarningModal.tsx b/packages/module/src/WarningModal/WarningModal.tsx index d36c50e7..5f5f25b6 100644 --- a/packages/module/src/WarningModal/WarningModal.tsx +++ b/packages/module/src/WarningModal/WarningModal.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import { Button, ModalProps, Modal, ModalVariant, ButtonVariant, variantIcons, } from '@patternfly/react-core'; +import { Button, ModalProps, Modal, ModalVariant, ButtonVariant } from '@patternfly/react-core'; export interface WarningModalProps extends Omit { /** Callback for the confirm action button. */ diff --git a/packages/module/src/WarningModal/__snapshots__/WarningModal.test.tsx.snap b/packages/module/src/WarningModal/__snapshots__/WarningModal.test.tsx.snap new file mode 100644 index 00000000..8bc5beaf --- /dev/null +++ b/packages/module/src/WarningModal/__snapshots__/WarningModal.test.tsx.snap @@ -0,0 +1,183 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`WarningModal component should render 1`] = ` +{ + "asFragment": [Function], + "baseElement": +