-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add delete confirm modal for widgets
- Loading branch information
1 parent
f59a069
commit 84fb016
Showing
14 changed files
with
288 additions
and
37 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
64 changes: 64 additions & 0 deletions
64
packages/dashboard/src/components/confirmDeleteModal/index.spec.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,64 @@ | ||
import React from 'react'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import ConfirmDeleteModal from './index'; | ||
|
||
describe('Confirm Delete Modal', () => { | ||
test('renders correctly', () => { | ||
const props = { | ||
headerTitle: 'Confirmation', | ||
submitTitle: 'Submit', | ||
description: 'Are you sure you want to submit?', | ||
visible: true, | ||
handleDismiss: jest.fn(), | ||
handleCancel: jest.fn(), | ||
handleSubmit: jest.fn(), | ||
}; | ||
const { getByText, getByTestId } = render(<ConfirmDeleteModal {...props} />); | ||
|
||
// Assert that the header, description, cancel button, and submit button are rendered correctly | ||
expect(getByText('Confirmation')).toBeInTheDocument(); | ||
expect(getByText('Are you sure you want to submit?')).toBeInTheDocument(); | ||
expect(getByText('Cancel')).toBeInTheDocument(); | ||
expect(getByTestId('custom-orange-button')).toBeInTheDocument(); | ||
}); | ||
|
||
test('calls handleCancel when cancel button is clicked', () => { | ||
const handleCancel = jest.fn(); | ||
const props = { | ||
headerTitle: 'Confirmation', | ||
submitTitle: 'Submit', | ||
description: 'Are you sure you want to submit?', | ||
visible: true, | ||
handleDismiss: jest.fn(), | ||
handleCancel, | ||
handleSubmit: jest.fn(), | ||
}; | ||
const { getByText } = render(<ConfirmDeleteModal {...props} />); | ||
const cancelButton = getByText('Cancel'); | ||
|
||
fireEvent.click(cancelButton); // Simulate clicking the cancel button | ||
|
||
// Assert that handleCancel is called | ||
expect(handleCancel).toHaveBeenCalled(); | ||
}); | ||
|
||
test('calls handleSubmit when submit button is clicked', () => { | ||
const handleSubmit = jest.fn(); | ||
const props = { | ||
headerTitle: 'Confirmation', | ||
submitTitle: 'Submit', | ||
description: 'Are you sure you want to submit?', | ||
visible: true, | ||
handleDismiss: jest.fn(), | ||
handleCancel: jest.fn(), | ||
handleSubmit, | ||
}; | ||
const { getByTestId } = render(<ConfirmDeleteModal {...props} />); | ||
const submitButton = getByTestId('custom-orange-button'); | ||
|
||
fireEvent.click(submitButton); // Simulate clicking the submit button | ||
|
||
// Assert that handleSubmit is called | ||
expect(handleSubmit).toHaveBeenCalled(); | ||
}); | ||
}); |
52 changes: 52 additions & 0 deletions
52
packages/dashboard/src/components/confirmDeleteModal/index.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,52 @@ | ||
import React, { ReactElement } from 'react'; | ||
|
||
import { Box, Button, Modal, SpaceBetween } from '@cloudscape-design/components'; | ||
|
||
import CustomOrangeButton from '../customOrangeButton'; | ||
|
||
interface ConfirmDeleteModalProps { | ||
headerTitle: string; | ||
cancelTitle?: string; | ||
submitTitle: string; | ||
description: ReactElement | string; | ||
visible: boolean; | ||
handleDismiss: () => void; | ||
handleCancel: () => void; | ||
handleSubmit: () => void; | ||
} | ||
|
||
const ConfirmDeleteModal = ({ | ||
headerTitle, | ||
cancelTitle = 'Cancel', | ||
submitTitle, | ||
description, | ||
visible, | ||
handleDismiss, | ||
handleCancel, | ||
handleSubmit, | ||
}: ConfirmDeleteModalProps) => { | ||
return ( | ||
<Modal | ||
visible={visible} | ||
onDismiss={handleDismiss} | ||
header={headerTitle} | ||
data-testid='confirm-modal' | ||
footer={ | ||
<Box float='right'> | ||
<SpaceBetween direction='horizontal' size='xs'> | ||
{cancelTitle && ( | ||
<Button variant='link' onClick={handleCancel}> | ||
{cancelTitle} | ||
</Button> | ||
)} | ||
<CustomOrangeButton title={submitTitle} handleClick={handleSubmit} /> | ||
</SpaceBetween> | ||
</Box> | ||
} | ||
> | ||
{description} | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default ConfirmDeleteModal; |
9 changes: 9 additions & 0 deletions
9
packages/dashboard/src/components/customOrangeButton/index.css
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,9 @@ | ||
.btn-custom-primary { | ||
border-color: var(--colors-background-button) !important; | ||
background-color: var(--colors-background-button) !important; | ||
} | ||
|
||
.btn-custom-primary:hover { | ||
border-color: var(--colors-button-hover) !important; | ||
background-color: var(--colors-button-hover) !important; | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/dashboard/src/components/customOrangeButton/index.spec.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,19 @@ | ||
import React from 'react'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import CustomOrangeButton from './index'; | ||
|
||
describe('CustomOrangeButton', () => { | ||
const title = 'Test Button'; | ||
const handleClick = jest.fn(); | ||
|
||
test('renders button with correct title', () => { | ||
const { getByText } = render(<CustomOrangeButton title={title} handleClick={handleClick} />); | ||
expect(getByText(title)).toBeInTheDocument(); | ||
}); | ||
test('calls handleClick when button is clicked', () => { | ||
const { getByRole } = render(<CustomOrangeButton title={title} handleClick={handleClick} />); | ||
const button = getByRole('button'); | ||
fireEvent.click(button); | ||
expect(handleClick).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
packages/dashboard/src/components/customOrangeButton/index.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,20 @@ | ||
import React from 'react'; | ||
|
||
import { colorBackgroundHomeHeader } from '@cloudscape-design/design-tokens'; | ||
import { Button, ButtonProps } from '@cloudscape-design/components'; | ||
|
||
import './index.css'; | ||
|
||
const CustomOrangeButton = ({ | ||
title, | ||
handleClick, | ||
...rest | ||
}: { title: string; handleClick: () => void } & ButtonProps) => { | ||
return ( | ||
<Button className='btn-custom-primary' onClick={handleClick} data-testid='custom-orange-button' {...rest}> | ||
<span style={{ color: colorBackgroundHomeHeader }}>{title}</span> | ||
</Button> | ||
); | ||
}; | ||
|
||
export default CustomOrangeButton; |
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
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
Oops, something went wrong.