@@ -53,7 +53,7 @@ export function ListItem(props: {
5353 children ?: JSX . Element | JSX . Element [ ] ;
5454 icon ?: JSX . Element ;
5555 className ?: string ;
56- onClick ?: ( ) => void ;
56+ onClick ?: ( e : React . MouseEvent < HTMLDivElement , MouseEvent > ) => void ;
5757} ) {
5858 return (
5959 < div
@@ -454,25 +454,45 @@ export function Selector<T>(props: {
454454 onClose ?: ( ) => void ;
455455 multiple ?: boolean ;
456456} ) {
457+ const [ selectedValues , setSelectedValues ] = useState < T [ ] > (
458+ Array . isArray ( props . defaultSelectedValue )
459+ ? props . defaultSelectedValue
460+ : props . defaultSelectedValue !== undefined
461+ ? [ props . defaultSelectedValue ]
462+ : [ ] ,
463+ ) ;
464+
465+ const handleSelection = (
466+ e : React . MouseEvent < HTMLDivElement , MouseEvent > ,
467+ value : T ,
468+ ) => {
469+ if ( props . multiple ) {
470+ e . stopPropagation ( ) ;
471+ const newSelectedValues = selectedValues . includes ( value )
472+ ? selectedValues . filter ( ( v ) => v !== value )
473+ : [ ...selectedValues , value ] ;
474+ setSelectedValues ( newSelectedValues ) ;
475+ props . onSelection ?.( newSelectedValues ) ;
476+ } else {
477+ setSelectedValues ( [ value ] ) ;
478+ props . onSelection ?.( [ value ] ) ;
479+ props . onClose ?.( ) ;
480+ }
481+ } ;
482+
457483 return (
458484 < div className = { styles [ "selector" ] } onClick = { ( ) => props . onClose ?.( ) } >
459485 < div className = { styles [ "selector-content" ] } >
460486 < List >
461487 { props . items . map ( ( item , i ) => {
462- const selected = props . multiple
463- ? // @ts -ignore
464- props . defaultSelectedValue ?. includes ( item . value )
465- : props . defaultSelectedValue === item . value ;
488+ const selected = selectedValues . includes ( item . value ) ;
466489 return (
467490 < ListItem
468491 className = { styles [ "selector-item" ] }
469492 key = { i }
470493 title = { item . title }
471494 subTitle = { item . subTitle }
472- onClick = { ( ) => {
473- props . onSelection ?.( [ item . value ] ) ;
474- props . onClose ?.( ) ;
475- } }
495+ onClick = { ( e ) => handleSelection ( e , item . value ) }
476496 >
477497 { selected ? (
478498 < div
@@ -494,7 +514,6 @@ export function Selector<T>(props: {
494514 </ div >
495515 ) ;
496516}
497-
498517export function FullScreen ( props : any ) {
499518 const { children, right = 10 , top = 10 , ...rest } = props ;
500519 const ref = useRef < HTMLDivElement > ( ) ;
0 commit comments