@@ -340,36 +340,28 @@ export const getGroupCheckboxValue = <OptionValue>(
340
340
* @param optionsList - The list of options or groups of options.
341
341
* @param value - The value to compare against the options' values.
342
342
* @param defaultOption - The default option to return if no match is found.
343
+ * @param getOptionValue - Override the default value for the option
343
344
* @returns The matched option or the default option if no match is found.
344
345
*/
345
346
export const getSelectPickerOptionByValue = < OptionValue > (
346
347
optionsList : OptionsOrGroups < SelectPickerOptionType < OptionValue > , GroupBase < SelectPickerOptionType < OptionValue > > > ,
347
348
value : OptionValue ,
348
349
defaultOption : SelectPickerOptionType < OptionValue > = { label : '' , value : '' as unknown as OptionValue } ,
350
+ getOptionValue : SelectPickerProps < OptionValue > [ 'getOptionValue' ] = null ,
349
351
) : SelectPickerOptionType < OptionValue > => {
350
352
if ( ! Array . isArray ( optionsList ) ) {
351
353
return defaultOption
352
354
}
353
355
354
- const foundOption = optionsList . reduce (
355
- ( acc , curr ) => {
356
- if ( ! acc . notFound ) return acc
356
+ const flatOptionsList = optionsList . flatMap < SelectPickerOptionType < OptionValue > > ( ( groupOrBaseOption ) =>
357
+ 'options' in groupOrBaseOption ? groupOrBaseOption . options : [ groupOrBaseOption ] ,
358
+ )
357
359
358
- if ( 'value' in curr && curr . value === value ) {
359
- return { data : curr , notFound : false }
360
- }
361
-
362
- if ( 'options' in curr && curr . options ) {
363
- const nestedOption = curr . options . find ( ( { value : _value } ) => _value === value )
364
- if ( nestedOption ) {
365
- return { data : nestedOption , notFound : false }
366
- }
367
- }
368
-
369
- return acc
370
- } ,
371
- { notFound : true , data : defaultOption } ,
372
- ) . data
360
+ return (
361
+ flatOptionsList . find ( ( option ) => {
362
+ const optionValue = getOptionValue ? getOptionValue ( option ) : option . value
373
363
374
- return foundOption
364
+ return optionValue === value
365
+ } ) ?? defaultOption
366
+ )
375
367
}
0 commit comments