Skip to content

Commit 4dc9d3d

Browse files
committed
fix: handling for option comparison
1 parent 085837b commit 4dc9d3d

File tree

3 files changed

+15
-22
lines changed

3 files changed

+15
-22
lines changed

src/Shared/Components/SelectPicker/SelectPicker.component.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
209209
shouldShowNoOptionsMessage = true,
210210
...props
211211
}: SelectPickerProps<OptionValue, IsMulti>) => {
212-
const { inputId, required, isDisabled, controlShouldRenderValue = true, value, options } = props
212+
const { inputId, required, isDisabled, controlShouldRenderValue = true, value, options, getOptionValue } = props
213213
const { isGroupHeadingSelectable = false, getIsOptionValid = () => true } = multiSelectProps
214214

215215
// Only large variant is supported for multi select picker
@@ -246,8 +246,9 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
246246
value as SelectPickerOptionType<OptionValue>[],
247247
trimmedInput as OptionValue,
248248
null,
249+
getOptionValue,
249250
) &&
250-
!getSelectPickerOptionByValue<OptionValue>(options, trimmedInput as OptionValue, null)
251+
!getSelectPickerOptionByValue<OptionValue>(options, trimmedInput as OptionValue, null, getOptionValue)
251252
)
252253
}
253254

src/Shared/Components/SelectPicker/type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ export interface FilterSelectPickerProps
271271
| 'shouldMenuAlignRight'
272272
| 'optionListError'
273273
| 'reloadOptionList'
274-
| 'getOptionLabel'
274+
| 'getOptionValue'
275275
> {
276276
appliedFilterOptions: SelectPickerOptionType[]
277277
handleApplyFilter: (filtersToApply: SelectPickerOptionType<number | string>[]) => void

src/Shared/Components/SelectPicker/utils.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -340,36 +340,28 @@ export const getGroupCheckboxValue = <OptionValue>(
340340
* @param optionsList - The list of options or groups of options.
341341
* @param value - The value to compare against the options' values.
342342
* @param defaultOption - The default option to return if no match is found.
343+
* @param getOptionValue - Override the default value for the option
343344
* @returns The matched option or the default option if no match is found.
344345
*/
345346
export const getSelectPickerOptionByValue = <OptionValue>(
346347
optionsList: OptionsOrGroups<SelectPickerOptionType<OptionValue>, GroupBase<SelectPickerOptionType<OptionValue>>>,
347348
value: OptionValue,
348349
defaultOption: SelectPickerOptionType<OptionValue> = { label: '', value: '' as unknown as OptionValue },
350+
getOptionValue: SelectPickerProps<OptionValue>['getOptionValue'] = null,
349351
): SelectPickerOptionType<OptionValue> => {
350352
if (!Array.isArray(optionsList)) {
351353
return defaultOption
352354
}
353355

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+
)
357359

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
373363

374-
return foundOption
364+
return optionValue === value
365+
}) ?? defaultOption
366+
)
375367
}

0 commit comments

Comments
 (0)