From a294bdf297b76ab8a3bae512dc7bc8bed87e759c Mon Sep 17 00:00:00 2001 From: ronronscelestes Date: Thu, 2 Sep 2021 15:28:35 +0200 Subject: [PATCH] feedback fixes + tests --- .../core/admin/admin/src/translations/en.json | 2 + .../ConfirmDialog/ConfirmDialog.stories.mdx | 37 +++++++- .../lib/src/components/ConfirmDialog/index.js | 76 +++++++++++++-- .../ConfirmDialog/tests/index.test.js | 93 +++++++++++++++++++ 4 files changed, 197 insertions(+), 11 deletions(-) create mode 100644 packages/core/helper-plugin/lib/src/components/ConfirmDialog/tests/index.test.js diff --git a/packages/core/admin/admin/src/translations/en.json b/packages/core/admin/admin/src/translations/en.json index 929fa64fae..12abd619dc 100644 --- a/packages/core/admin/admin/src/translations/en.json +++ b/packages/core/admin/admin/src/translations/en.json @@ -210,9 +210,11 @@ "app.components.BlockLink.documentation": "Read the documentation", "app.components.BlockLink.documentation.content": "Discover the concepts, reference guides and tutorials.", "app.components.Button.cancel": "Cancel", + "app.components.Button.confirm": "Confirm", "app.components.Button.reset": "Reset", "app.components.Button.save": "Save", "app.components.ComingSoonPage.comingSoon": "Coming soon", + "app.components.ConfirmDialog.title": "Confirmation", "app.components.DownloadInfo.download": "Download in progress...", "app.components.DownloadInfo.text": "This could take a minute. Thanks for your patience.", "app.components.EmptyAttributes.title": "There are no fields yet", diff --git a/packages/core/helper-plugin/lib/src/components/ConfirmDialog/ConfirmDialog.stories.mdx b/packages/core/helper-plugin/lib/src/components/ConfirmDialog/ConfirmDialog.stories.mdx index ec2e93cfd9..d72c7a3e29 100644 --- a/packages/core/helper-plugin/lib/src/components/ConfirmDialog/ConfirmDialog.stories.mdx +++ b/packages/core/helper-plugin/lib/src/components/ConfirmDialog/ConfirmDialog.stories.mdx @@ -3,6 +3,8 @@ import { useState } from 'react'; import { ArgsTable, Meta, Canvas, Story } from '@storybook/addon-docs'; import { Button } from '@strapi/parts/Button'; +import CheckIcon from '@strapi/icons/CheckIcon'; +import HelpIcon from '@strapi/icons/HelpIcon'; import ConfirmDialog from './index'; @@ -43,7 +45,40 @@ TODO return ( <> - + + + ); + }} + + + {() => { + const [dialogOpen, setDialogOpen] = useState(false); + const handleToggle = () => setDialogOpen(prev => !prev); + const handleDelete = () => { + handleToggle(); + alert('you deleted your item'); + }; + const isLoading = false; + return ( + <> + + } + iconRightButton={} + isConfirmButtonLoading={isLoading} + isVisible={dialogOpen} + onToggleDialog={handleToggle} + onConfirm={handleDelete} + title={{id: 'app.components.ConfirmDialog.title', defaultMessage: 'Confirmation'}} + rightButtonText={{id: 'app.components.Button.save', defaultMessage: 'Save'}} + variantRightButton='success-light' + /> ); }} diff --git a/packages/core/helper-plugin/lib/src/components/ConfirmDialog/index.js b/packages/core/helper-plugin/lib/src/components/ConfirmDialog/index.js index b37e880090..8beaf96411 100644 --- a/packages/core/helper-plugin/lib/src/components/ConfirmDialog/index.js +++ b/packages/core/helper-plugin/lib/src/components/ConfirmDialog/index.js @@ -9,7 +9,19 @@ import { Button } from '@strapi/parts/Button'; import AlertWarningIcon from '@strapi/icons/AlertWarningIcon'; import DeleteIcon from '@strapi/icons/DeleteIcon'; -const ConfirmDialog = ({ isConfirmButtonLoading, onToggleDialog, onConfirm, isVisible }) => { +const ConfirmDialog = ({ + bodyText, + iconRightButton, + iconBody, + isConfirmButtonLoading, + isVisible, + leftButtonText, + onToggleDialog, + onConfirm, + rightButtonText, + title, + variantRightButton, +}) => { const { formatMessage } = useIntl(); if (!isVisible) { @@ -20,19 +32,19 @@ const ConfirmDialog = ({ isConfirmButtonLoading, onToggleDialog, onConfirm, isVi - }> + {formatMessage({ - id: 'Settings.webhooks.confirmation.delete', - defaultMessage: 'Are you sure you want to delete this?', + id: bodyText.id, + defaultMessage: bodyText.defaultMessage, })} @@ -41,18 +53,24 @@ const ConfirmDialog = ({ isConfirmButtonLoading, onToggleDialog, onConfirm, isVi - {formatMessage({ id: 'app.components.Button.cancel', defaultMessage: 'Cancel' })} + {formatMessage({ + id: leftButtonText.id, + defaultMessage: leftButtonText.defaultMessage, + })} } endAction={ } /> @@ -61,14 +79,52 @@ const ConfirmDialog = ({ isConfirmButtonLoading, onToggleDialog, onConfirm, isVi }; ConfirmDialog.defaultProps = { + bodyText: { + id: 'components.popUpWarning.message', + defaultMessage: 'Are you sure you want to delete this?', + }, + iconBody: , + iconRightButton: , isConfirmButtonLoading: false, + leftButtonText: { + id: 'app.components.Button.cancel', + defaultMessage: 'Cancel', + }, + rightButtonText: { + id: 'app.components.Button.confirm', + defaultMessage: 'Confirm', + }, + title: { + id: 'app.components.ConfirmDialog.title', + defaultMessage: 'Confirmation', + }, + variantRightButton: 'danger-light', }; ConfirmDialog.propTypes = { + bodyText: PropTypes.shape({ + id: PropTypes.string, + defaultMessage: PropTypes.string, + }), + iconBody: PropTypes.node, + iconRightButton: PropTypes.node, isConfirmButtonLoading: PropTypes.bool, isVisible: PropTypes.bool.isRequired, onConfirm: PropTypes.func.isRequired, onToggleDialog: PropTypes.func.isRequired, + leftButtonText: PropTypes.shape({ + id: PropTypes.string, + defaultMessage: PropTypes.string, + }), + rightButtonText: PropTypes.shape({ + id: PropTypes.string, + defaultMessage: PropTypes.string, + }), + title: PropTypes.shape({ + id: PropTypes.string, + defaultMessage: PropTypes.string, + }), + variantRightButton: PropTypes.string, }; export default ConfirmDialog; diff --git a/packages/core/helper-plugin/lib/src/components/ConfirmDialog/tests/index.test.js b/packages/core/helper-plugin/lib/src/components/ConfirmDialog/tests/index.test.js new file mode 100644 index 0000000000..f3ef49ff34 --- /dev/null +++ b/packages/core/helper-plugin/lib/src/components/ConfirmDialog/tests/index.test.js @@ -0,0 +1,93 @@ +import React from 'react'; +import { render, waitFor, screen } from '@testing-library/react'; +import { IntlProvider } from 'react-intl'; +import { ThemeProvider, lightTheme } from '@strapi/parts'; +import ConfirmDialog from '../index'; + +const App = ( + + + + + +); + +describe('ConfirmDialog', () => { + it('renders and matches the snapshot', async () => { + const { + container: { firstChild }, + } = render(App); + + expect(firstChild).toMatchInlineSnapshot(` + .c0 { + border: 0; + -webkit-clip: rect(0 0 0 0); + clip: rect(0 0 0 0); + height: 1px; + margin: -1px; + overflow: hidden; + padding: 0; + position: absolute; + width: 1px; + } + +
+

+

+

+ `); + }); + + it('renders and matches the snapshot', async () => { + const { + container: { firstChild }, + } = render(App); + + await waitFor(() => { + expect(screen.getByText('Are you sure you want to delete this?')).toBeInTheDocument(); + }); + }); + + it('renders and matches the snapshot', async () => { + const AppCustom = ( + + + + + + ); + + const { + container: { firstChild }, + } = render(AppCustom); + + await waitFor(() => { + expect(screen.getByText('Are you sure you want to unpublish it?')).toBeInTheDocument(); + }); + }); +});