@@ -2,7 +2,6 @@ import { CheckIcon, ChevronUpIcon } from '@chakra-ui/icons';
22import {
33 Box ,
44 Flex ,
5- FlexProps ,
65 FormControl ,
76 FormControlProps ,
87 FormLabel ,
@@ -16,22 +15,27 @@ import {
1615} from '@chakra-ui/react' ;
1716import { autoUpdate , offset , shift , useFloating } from '@floating-ui/react-dom' ;
1817import { useSelect } from 'downshift' ;
18+ import { isString } from 'lodash-es' ;
1919import { OverlayScrollbarsComponent } from 'overlayscrollbars-react' ;
2020
2121import { memo , useMemo } from 'react' ;
2222import { getInputOutlineStyles } from 'theme/util/getInputOutlineStyles' ;
2323
2424export type ItemTooltips = { [ key : string ] : string } ;
2525
26+ export type IAICustomSelectOption = {
27+ value : string ;
28+ label : string ;
29+ tooltip ?: string ;
30+ } ;
31+
2632type IAICustomSelectProps = {
2733 label ?: string ;
28- items : string [ ] ;
29- itemTooltips ?: ItemTooltips ;
30- selectedItem : string ;
31- setSelectedItem : ( v : string | null | undefined ) => void ;
34+ value : string ;
35+ data : IAICustomSelectOption [ ] | string [ ] ;
36+ onChange : ( v : string ) => void ;
3237 withCheckIcon ?: boolean ;
3338 formControlProps ?: FormControlProps ;
34- buttonProps ?: FlexProps ;
3539 tooltip ?: string ;
3640 tooltipProps ?: Omit < TooltipProps , 'children' > ;
3741 ellipsisPosition ?: 'start' | 'end' ;
@@ -40,18 +44,33 @@ type IAICustomSelectProps = {
4044const IAICustomSelect = ( props : IAICustomSelectProps ) => {
4145 const {
4246 label,
43- items,
44- itemTooltips,
45- setSelectedItem,
46- selectedItem,
4747 withCheckIcon,
4848 formControlProps,
4949 tooltip,
50- buttonProps,
5150 tooltipProps,
5251 ellipsisPosition = 'end' ,
52+ data,
53+ value,
54+ onChange,
5355 } = props ;
5456
57+ const values = useMemo ( ( ) => {
58+ return data . map < IAICustomSelectOption > ( ( v ) => {
59+ if ( isString ( v ) ) {
60+ return { value : v , label : v } ;
61+ }
62+ return v ;
63+ } ) ;
64+ } , [ data ] ) ;
65+
66+ const stringValues = useMemo ( ( ) => {
67+ return values . map ( ( v ) => v . value ) ;
68+ } , [ values ] ) ;
69+
70+ const valueData = useMemo ( ( ) => {
71+ return values . find ( ( v ) => v . value === value ) ;
72+ } , [ values , value ] ) ;
73+
5574 const {
5675 isOpen,
5776 getToggleButtonProps,
@@ -60,10 +79,11 @@ const IAICustomSelect = (props: IAICustomSelectProps) => {
6079 highlightedIndex,
6180 getItemProps,
6281 } = useSelect ( {
63- items,
64- selectedItem,
65- onSelectedItemChange : ( { selectedItem : newSelectedItem } ) =>
66- setSelectedItem ( newSelectedItem ) ,
82+ items : stringValues ,
83+ selectedItem : value ,
84+ onSelectedItemChange : ( { selectedItem : newSelectedItem } ) => {
85+ newSelectedItem && onChange ( newSelectedItem ) ;
86+ } ,
6787 } ) ;
6888
6989 const { refs, floatingStyles } = useFloating < HTMLButtonElement > ( {
@@ -94,7 +114,6 @@ const IAICustomSelect = (props: IAICustomSelectProps) => {
94114 < Tooltip label = { tooltip } { ...tooltipProps } >
95115 < Flex
96116 { ...getToggleButtonProps ( { ref : refs . setReference } ) }
97- { ...buttonProps }
98117 sx = { {
99118 alignItems : 'center' ,
100119 userSelect : 'none' ,
@@ -119,7 +138,7 @@ const IAICustomSelect = (props: IAICustomSelectProps) => {
119138 direction : labelTextDirection ,
120139 } }
121140 >
122- { selectedItem }
141+ { valueData ?. label }
123142 </ Text >
124143 < ChevronUpIcon
125144 sx = { {
@@ -155,8 +174,8 @@ const IAICustomSelect = (props: IAICustomSelectProps) => {
155174 } }
156175 >
157176 < OverlayScrollbarsComponent >
158- { items . map ( ( item , index ) => {
159- const isSelected = selectedItem === item ;
177+ { values . map ( ( v , index ) => {
178+ const isSelected = value === v . value ;
160179 const isHighlighted = highlightedIndex === index ;
161180 const fontWeight = isSelected ? 700 : 500 ;
162181 const bg = isHighlighted
@@ -166,9 +185,9 @@ const IAICustomSelect = (props: IAICustomSelectProps) => {
166185 : undefined ;
167186 return (
168187 < Tooltip
169- isDisabled = { ! itemTooltips }
170- key = { `${ item } ${ index } ` }
171- label = { itemTooltips ?. [ item ] }
188+ isDisabled = { ! v . tooltip }
189+ key = { `${ v . value } ${ index } ` }
190+ label = { v . tooltip }
172191 hasArrow
173192 placement = "right"
174193 >
@@ -182,8 +201,7 @@ const IAICustomSelect = (props: IAICustomSelectProps) => {
182201 transitionProperty : 'common' ,
183202 transitionDuration : '0.15s' ,
184203 } }
185- key = { `${ item } ${ index } ` }
186- { ...getItemProps ( { item, index } ) }
204+ { ...getItemProps ( { item : v . value , index } ) }
187205 >
188206 { withCheckIcon ? (
189207 < Grid gridTemplateColumns = "1.25rem auto" >
@@ -198,7 +216,7 @@ const IAICustomSelect = (props: IAICustomSelectProps) => {
198216 fontWeight,
199217 } }
200218 >
201- { item }
219+ { v . label }
202220 </ Text >
203221 </ GridItem >
204222 </ Grid >
@@ -210,7 +228,7 @@ const IAICustomSelect = (props: IAICustomSelectProps) => {
210228 fontWeight,
211229 } }
212230 >
213- { item }
231+ { v . label }
214232 </ Text >
215233 ) }
216234 </ ListItem >
0 commit comments