@@ -24,6 +24,7 @@ import {
2424import { getRuleValue } from "../../utils/getRuleValue" ;
2525import { FormLabel } from "../FormLabel" ;
2626import {
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