11import clsx from 'clsx'
22import { useState } from 'react'
3+ import { IoIosClose } from 'react-icons/io'
34import { Option } from 'src/types'
45import './chipset.css'
56type ChipProps = {
67 option : Option
78 onSelect : ( option : Option ) => void
9+ onRemove ?: ( option : Option ) => void
810 active : boolean
911}
1012
11- const Chip = ( { option, onSelect, active = false } : ChipProps ) => {
13+ const Chip = ( { option, onSelect, onRemove , active = false } : ChipProps ) => {
1214 return (
13- < button className = { 'chip ' + ( active && 'active' ) } onClick = { ( ) => onSelect ( option ) } >
14- { option . icon && < span className = "chipIcon" > { option . icon } </ span > }
15- { option . label }
16- </ button >
15+ < div className = { 'chip ' + ( active && 'active' ) } >
16+ < button onClick = { ( ) => onSelect ( option ) } >
17+ { option . icon && < span className = "chipIcon" > { option . icon } </ span > }
18+ { option . label }
19+ </ button >
20+ { option . removeable && onRemove && (
21+ < button className = "deleteButton" onClick = { ( ) => onRemove ( option ) } >
22+ < IoIosClose className = "icon" />
23+ </ button >
24+ ) }
25+ </ div >
1726 )
1827}
1928type ChangeAction = {
@@ -26,13 +35,15 @@ type ChipsSetProps = {
2635 defaultValues ?: string [ ]
2736 canSelectMultiple ?: boolean
2837 onChange ?: ( changes : ChangeAction , options : Option [ ] ) => void
38+ onRemove ?: ( option : Option ) => void
2939}
3040
3141export const ChipsSet = ( {
3242 className,
3343 options,
3444 canSelectMultiple = false ,
3545 onChange,
46+ onRemove,
3647 defaultValues,
3748} : ChipsSetProps ) => {
3849 const [ selectedChips , setSelectedChips ] = useState < string [ ] | undefined > ( defaultValues || [ ] )
@@ -80,6 +91,7 @@ export const ChipsSet = ({
8091 key = { option . value }
8192 option = { option }
8293 onSelect = { onSelect }
94+ onRemove = { onRemove }
8395 active = { selectedChips ?. some ( ( chipValue ) => chipValue === option . value ) || false }
8496 />
8597 )
0 commit comments