Skip to content

Commit

Permalink
Finding preselectedOption before filtering in a backwards-compatible …
Browse files Browse the repository at this point in the history
…mode, and setting it again if using optionFilter
  • Loading branch information
Ole Martin Handeland committed Dec 11, 2024
1 parent 55d5f62 commit f7e4921
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/features/options/useGetOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ export function useFilteredAndSortedOptions({
const selectedValues = useSetOptions(valueType, dataModelBindings, unsorted).selectedValues;

return useMemo(() => {
let preselectedOption: IOptionInternal | undefined;
if (preselected !== undefined) {
preselectedOption = unsorted[preselected];
}

let options = verifyAndDeduplicateOptions(unsorted, valueType === 'multi');

if (optionFilter !== undefined && ExprValidation.isValid(optionFilter)) {
Expand All @@ -190,12 +195,15 @@ export function useFilteredAndSortedOptions({
}
return keep;
});
}

// This used to work on the original array, before any filtering. However, for this to still work after
// filtering using optionFilter, we need to find the preselected option in the filtered array instead.
const preselectedOption =
preselected !== undefined && options && options[preselected] ? options[preselected] : undefined;
// If we have an option filter AND a preselected option, we need to set which option is preselected
// again at this point. Previously, the preselected option was always just set to the first option in the list
// before any sorting an filtering was done, but for it to work correctly with optionFilter, we need to set it
// again here. Just setting it after the filtering is done might break some apps that rely on the old behavior.
if (preselected !== undefined) {
preselectedOption = options[preselected];
}
}

// No need to sort if there are 0 or 1 options. Using langAsString() can lead to re-rendering, so
// we avoid it if we don't need it.
Expand Down

0 comments on commit f7e4921

Please sign in to comment.