@@ -101,7 +101,7 @@ export const MultiSelect = React.forwardRef<
101101 setIsPopoverOpen ( open ?? false ) ;
102102 } , [ open ] ) ;
103103
104- const allValues = children
104+ const allValues = React . useMemo ( ( ) => children
105105 ?
106106 // @ts -ignore
107107 Children . map ( children , ( child ) => {
@@ -110,7 +110,18 @@ export const MultiSelect = React.forwardRef<
110110 }
111111 return null ;
112112 } ) . filter ( Boolean ) as any [ ]
113- : [ ] ;
113+ : [ ] , [ children ] ) ;
114+
115+ const optionsMap = React . useMemo ( ( ) => {
116+ const map = new Map < string , React . ReactNode > ( ) ;
117+ Children . forEach ( children , ( child ) => {
118+ if ( React . isValidElement ( child ) ) {
119+ // @ts -ignore
120+ map . set ( String ( child . props . value ) , child . props . children ) ;
121+ }
122+ } ) ;
123+ return map ;
124+ } , [ children ] ) ;
114125
115126 React . useEffect ( ( ) => {
116127 setSelectedValues ( value ?? [ ] ) ;
@@ -220,24 +231,17 @@ export const MultiSelect = React.forwardRef<
220231 { renderValues && renderValues ( selectedValues ) }
221232 { ! renderValues && selectedValues . map ( ( value ) => {
222233
223- // @ts -ignore
224- const childrenProps : MultiSelectItemProps [ ] = Children . map ( children , ( child ) => {
225- if ( React . isValidElement ( child ) ) {
226- return child . props ;
227- }
228- } ) . filter ( Boolean ) ;
229-
230- const option = childrenProps . find ( ( o ) => String ( o . value ) === String ( value ) ) ;
234+ const optionChildren = optionsMap . get ( String ( value ) ) ;
231235 if ( ! useChips ) {
232- return option ?. children ;
236+ return optionChildren ;
233237 }
234238 return (
235239 < Chip
236240 size = { "medium" }
237241 key = { String ( value ) }
238242 className = { cls ( "flex flex-row items-center p-1" ) }
239243 >
240- { option ?. children }
244+ { optionChildren }
241245 < CloseIcon
242246 size = { "smallest" }
243247 onClick = { ( event ) => {
@@ -341,7 +345,7 @@ export interface MultiSelectItemProps<T extends MultiSelectValue = string> {
341345 className ?: string ;
342346}
343347
344- export function MultiSelectItem < T extends MultiSelectValue = string > ( {
348+ export const MultiSelectItem = React . memo ( function MultiSelectItem < T extends MultiSelectValue = string > ( {
345349 children,
346350 value,
347351 className
@@ -378,9 +382,9 @@ export function MultiSelectItem<T extends MultiSelectValue = string>({
378382 < InnerCheckBox checked = { isSelected } />
379383 { children }
380384 </ CommandPrimitive . Item > ;
381- }
385+ } ) ;
382386
383- function InnerCheckBox ( { checked } : { checked : boolean } ) {
387+ const InnerCheckBox = React . memo ( function InnerCheckBox ( { checked } : { checked : boolean } ) {
384388 return < div className = { cls (
385389 "p-2" ,
386390 "w-8 h-8" ,
@@ -397,5 +401,5 @@ function InnerCheckBox({ checked }: { checked: boolean }) {
397401 { checked && < CheckIcon size = { 16 } className = { "absolute" } /> }
398402 </ div >
399403 </ div >
400- }
404+ } ) ;
401405
0 commit comments