-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(boemly-tag): implement closable option (#215)
- Loading branch information
1 parent
7c29d2e
commit 1e30161
Showing
5 changed files
with
81 additions
and
6 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
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,32 @@ | ||
import React from 'react'; | ||
import { screen, render, fireEvent } from '../../test/testUtils'; | ||
import { BoemlyTag, BoemlyTagProps } from './BoemlyTag'; | ||
|
||
const defaultProps: BoemlyTagProps = { | ||
children: 'Test Tag', | ||
variant: 'solid', | ||
colorScheme: 'blue', | ||
}; | ||
|
||
const setup = (props: BoemlyTagProps = defaultProps) => { | ||
render(<BoemlyTag {...props} />); | ||
}; | ||
|
||
describe('The BoemlyTag component', () => { | ||
it('renders the tag without a close button if closable is false', () => { | ||
setup({ ...defaultProps, isClosable: false }); | ||
|
||
expect(screen.getByText('Test Tag')).toBeInTheDocument(); | ||
expect(screen.queryByRole('button')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('closes the tag when the close button is clicked', () => { | ||
setup({ ...defaultProps, isClosable: true }); | ||
|
||
expect(screen.getByText('Test Tag')).toBeInTheDocument(); | ||
|
||
fireEvent.click(screen.getByRole('button')); | ||
|
||
expect(screen.queryByText('Test Tag')).not.toBeInTheDocument(); | ||
}); | ||
}); |
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,32 @@ | ||
import React, { useState } from 'react'; | ||
import { Tag, TagCloseButton, TagLabel, TagProps } from '@chakra-ui/react'; | ||
|
||
export interface BoemlyTagProps extends TagProps { | ||
isClosable?: boolean; | ||
onClose?: () => void; | ||
} | ||
|
||
export const BoemlyTag = ({ | ||
isClosable = false, | ||
onClose, | ||
children, | ||
...props | ||
}: BoemlyTagProps): JSX.Element => { | ||
const [isVisible, setIsVisible] = useState(true); | ||
|
||
const handleClose = () => { | ||
setIsVisible(false); | ||
if (onClose) { | ||
onClose(); | ||
} | ||
}; | ||
|
||
if (!isVisible) return <></>; | ||
|
||
return ( | ||
<Tag {...props}> | ||
<TagLabel>{children}</TagLabel> | ||
{isClosable && <TagCloseButton onClick={handleClose} />} | ||
</Tag> | ||
); | ||
}; |
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 @@ | ||
export { BoemlyTag } from './BoemlyTag'; |
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