File tree Expand file tree Collapse file tree 4 files changed +12
-11
lines changed Expand file tree Collapse file tree 4 files changed +12
-11
lines changed Original file line number Diff line number Diff line change @@ -49,7 +49,7 @@ export const RemoteDataSetExample2 = memo((props: Omit<IAutocompleteDropdownProp
4949 < AutocompleteDropdown
5050 ref = { searchRef }
5151 controller = { dropdownController }
52- // initialValue={'1'}
52+ initialValue = { { id : '1' , title : 'Initial val' } }
5353 // direction={Platform.select({ ios: 'down' })}
5454 dataSet = { suggestionsList }
5555 onChangeText = { getSuggestions }
Original file line number Diff line number Diff line change @@ -43,6 +43,7 @@ export const RemoteDataSetExample3 = memo(() => {
4343 < AutocompleteDropdown
4444 ref = { searchRef }
4545 closeOnBlur = { false }
46+ // initialValue={{ id: '1', title: 'Initial val' }}
4647 // direction={Platform.select({ ios: 'down' })}
4748 controller = { controller => {
4849 dropdownController . current = controller
Original file line number Diff line number Diff line change @@ -193,20 +193,20 @@ export const AutocompleteDropdown = memo<
193193 useEffect ( ( ) => {
194194 const initialDataSet = initialDataSetRef . current
195195 const initialValue = initialValueRef . current
196- if ( ! Array . isArray ( initialDataSet ) ) {
197- // nothing to set or already setted
198- return
199- }
200196
201- let dataSetItem : AutocompleteDropdownItem | undefined
197+ let initialValueItem : AutocompleteDropdownItem | undefined
202198 if ( typeof initialValue === 'string' ) {
203- dataSetItem = initialDataSet . find ( el => el . id === initialValue )
199+ initialValueItem = initialDataSet ? .find ( el => el . id === initialValue )
204200 } else if ( typeof initialValue === 'object' && initialValue . id ) {
205- dataSetItem = initialDataSet . find ( el => el . id === initialValue ?. id )
201+ initialValueItem = initialDataSet ?. find ( el => el . id === initialValue ?. id )
202+ if ( ! initialValueItem ) {
203+ // set the item as it is if it's not in the list
204+ initialValueItem = initialValue
205+ }
206206 }
207207
208- if ( dataSetItem ) {
209- setSelectedItem ( dataSetItem )
208+ if ( initialValueItem ) {
209+ setSelectedItem ( initialValueItem )
210210 }
211211 } , [ ] )
212212
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ export interface IAutocompleteDropdownProps {
2828 dataSet : AutocompleteDropdownItem [ ] | null
2929 inputHeight ?: number
3030 suggestionsListMaxHeight ?: number
31- initialValue ?: string | { id : string }
31+ initialValue ?: string | { id : string } | AutocompleteDropdownItem
3232 loading ?: boolean
3333 useFilter ?: boolean
3434 showClear ?: boolean
You can’t perform that action at this time.
0 commit comments