-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor(web-react): Introduce
ModalCloseButton
component
- Loading branch information
Showing
5 changed files
with
80 additions
and
23 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
packages/web-react/src/components/Modal/ModalCloseButton.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,14 @@ | ||
import React from 'react'; | ||
import { ModalCloseButtonProps } from '../../types'; | ||
import { Icon } from '../Icon'; | ||
import { VisuallyHidden } from '../VisuallyHidden'; | ||
import { Button } from '../Button'; | ||
|
||
const ModalCloseButton = ({ label = 'Close', onClose, id, isOpen, ...restProps }: ModalCloseButtonProps) => ( | ||
<Button {...restProps} isSquare color="tertiary" onClick={onClose} aria-expanded={isOpen} aria-controls={id}> | ||
<Icon name="close" /> | ||
<VisuallyHidden>{label}</VisuallyHidden> | ||
</Button> | ||
); | ||
|
||
export default ModalCloseButton; |
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
50 changes: 50 additions & 0 deletions
50
packages/web-react/src/components/Modal/__tests__/ModalCloseButton.test.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,50 @@ | ||
import '@testing-library/jest-dom'; | ||
import { fireEvent, render } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { classNamePrefixProviderTest } from '../../../../tests/providerTests/classNamePrefixProviderTest'; | ||
import { restPropsTest } from '../../../../tests/providerTests/restPropsTest'; | ||
import { stylePropsTest } from '../../../../tests/providerTests/stylePropsTest'; | ||
import ModalCloseButton from '../ModalCloseButton'; | ||
|
||
describe('ModalCloseButton', () => { | ||
classNamePrefixProviderTest(ModalCloseButton, 'Button'); | ||
|
||
stylePropsTest(ModalCloseButton); | ||
|
||
restPropsTest(ModalCloseButton, 'button'); | ||
|
||
it('should have icon', () => { | ||
const dom = render(<ModalCloseButton label="close" id="test" isOpen onClose={() => {}} />); | ||
|
||
expect(dom.container.querySelector('svg')).toBeDefined(); | ||
}); | ||
|
||
it('should have visually hidden label', () => { | ||
const dom = render(<ModalCloseButton label="close" id="test" isOpen onClose={() => {}} />); | ||
|
||
expect(dom.container.querySelector('button > span')?.textContent).toBe('close'); | ||
expect(dom.container.querySelector('button > span')).toHaveClass('accessibility-hidden'); | ||
}); | ||
|
||
it('should be square', () => { | ||
const dom = render(<ModalCloseButton label="close" id="test" isOpen onClose={() => {}} />); | ||
|
||
expect(dom.container.querySelector('button')).toHaveClass('Button--square'); | ||
}); | ||
|
||
it('should have tertiary color', () => { | ||
const dom = render(<ModalCloseButton label="close" id="test" isOpen onClose={() => {}} />); | ||
|
||
expect(dom.container.querySelector('button')).toHaveClass('Button--tertiary'); | ||
}); | ||
|
||
it('should handle on close click', () => { | ||
const mockedOnClose = jest.fn(); | ||
const dom = render(<ModalCloseButton label="close" id="test" isOpen onClose={mockedOnClose} />); | ||
|
||
const button = dom.getByRole('button'); | ||
fireEvent.click(button); | ||
|
||
expect(mockedOnClose).toHaveBeenCalled(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
export { default as Modal } from './Modal'; | ||
export { default as ModalDialog } from './ModalDialog'; | ||
export { default as ModalBody } from './ModalBody'; | ||
export { default as ModalHeader } from './ModalHeader'; | ||
export { default as ModalFooter } from './ModalFooter'; | ||
export { default as ModalCloseButton } from './ModalCloseButton'; | ||
export * from './ModalContext'; | ||
export { default as ModalDialog } from './ModalDialog'; | ||
export { default as ModalFooter } from './ModalFooter'; | ||
export { default as ModalHeader } from './ModalHeader'; | ||
export * from './useModalStyleProps'; |
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