Skip to content

Commit 9cb5b59

Browse files
authored
use the selected options in the render (#39535)
fixes: #39496 Signed-off-by: Erik Jan de Wit <[email protected]>
1 parent 7a7543d commit 9cb5b59

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

js/libs/ui-shared/src/controls/select-control/SelectControl.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export type SelectControlProps<
4141
name: string;
4242
label?: string;
4343
options: OptionType;
44-
selectedOptions?: SelectControlOption[];
44+
selectedOptions?: OptionType;
4545
labelIcon?: string;
4646
controller: Omit<ControllerProps, "name" | "render">;
4747
onFilter?: (value: string) => void;

js/libs/ui-shared/src/controls/select-control/SingleSelectControl.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const SingleSelectControl = <
3030
name,
3131
label,
3232
options,
33+
selectedOptions = [],
3334
controller,
3435
labelIcon,
3536
isDisabled,
@@ -105,7 +106,7 @@ export const SingleSelectControl = <
105106
isOpen={open}
106107
>
107108
<SelectList data-testid={`select-${name}`}>
108-
{options.map((option) => (
109+
{[...options, ...selectedOptions].map((option) => (
109110
<SelectOption key={key(option)} value={key(option)}>
110111
{isString(option) ? option : option.value}
111112
</SelectOption>

js/libs/ui-shared/src/controls/select-control/TypeaheadSelectControl.tsx

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
import { getRuleValue } from "../../utils/getRuleValue";
2525
import { FormLabel } from "../FormLabel";
2626
import {
27+
OptionType,
2728
SelectControlOption,
2829
SelectControlProps,
2930
SelectVariant,
@@ -65,22 +66,19 @@ export const TypeaheadSelectControl = <
6566
const required = getRuleValue(controller.rules?.required) === true;
6667
const isTypeaheadMulti = variant === SelectVariant.typeaheadMulti;
6768

68-
const filteredOptions = options.filter((option) =>
69-
getValue(option).toLowerCase().startsWith(filterValue.toLowerCase()),
69+
const combinedOptions = useMemo(
70+
() =>
71+
[
72+
...options.filter(
73+
(o) => !selectedOptions.map((o) => getValue(o)).includes(getValue(o)),
74+
),
75+
...selectedOptions,
76+
] as OptionType,
77+
[selectedOptions, options],
7078
);
7179

72-
const convert = useMemo(
73-
() =>
74-
filteredOptions.map((option, index) => (
75-
<SelectOption
76-
key={key(option)}
77-
value={key(option)}
78-
isFocused={focusedItemIndex === index}
79-
>
80-
{getValue(option)}
81-
</SelectOption>
82-
)),
83-
[focusedItemIndex, filteredOptions],
80+
const filteredOptions = combinedOptions.filter((option) =>
81+
getValue(option).toLowerCase().startsWith(filterValue.toLowerCase()),
8482
);
8583

8684
const updateValue = (
@@ -96,10 +94,10 @@ export const TypeaheadSelectControl = <
9694
}
9795
} else {
9896
field.onChange([...field.value, option]);
99-
if (isSelectBasedOptions(options)) {
97+
if (isSelectBasedOptions(combinedOptions)) {
10098
setSelectedOptions([
10199
...selectedOptionsState,
102-
options.find((o) => o.key === option)!,
100+
combinedOptions.find((o) => o.key === option)!,
103101
]);
104102
}
105103
}
@@ -187,8 +185,8 @@ export const TypeaheadSelectControl = <
187185
{...rest}
188186
onOpenChange={() => setOpen(false)}
189187
selected={
190-
isSelectBasedOptions(options)
191-
? options
188+
isSelectBasedOptions(combinedOptions)
189+
? combinedOptions
192190
.filter((o) =>
193191
Array.isArray(field.value)
194192
? field.value.includes(o.key)
@@ -216,8 +214,8 @@ export const TypeaheadSelectControl = <
216214
placeholder={placeholderText}
217215
value={
218216
variant === SelectVariant.typeahead && field.value
219-
? isSelectBasedOptions(options)
220-
? options.find(
217+
? isSelectBasedOptions(combinedOptions)
218+
? combinedOptions.find(
221219
(o) =>
222220
o.key ===
223221
(Array.isArray(field.value)
@@ -255,11 +253,10 @@ export const TypeaheadSelectControl = <
255253
);
256254
}}
257255
>
258-
{isSelectBasedOptions(options)
256+
{isSelectBasedOptions(combinedOptions)
259257
? [
260-
...options,
258+
...combinedOptions,
261259
...selectedOptionsState,
262-
...selectedOptions,
263260
].find((o) => selection === o.key)?.value
264261
: getValue(selection)}
265262
</Chip>
@@ -299,7 +296,18 @@ export const TypeaheadSelectControl = <
299296
}}
300297
isOpen={open}
301298
>
302-
<SelectList>{convert}</SelectList>
299+
<SelectList>
300+
{filteredOptions.map((option, index) => (
301+
<SelectOption
302+
key={key(option)}
303+
value={key(option)}
304+
isFocused={focusedItemIndex === index}
305+
isActive={field.value.includes(getValue(option))}
306+
>
307+
{getValue(option)}
308+
</SelectOption>
309+
))}
310+
</SelectList>
303311
</Select>
304312
)}
305313
/>

0 commit comments

Comments
 (0)