Skip to content

Commit 99317bd

Browse files
authored
feat: allow the initial value to be set without a dataSet (#148)
1 parent 7df36a4 commit 99317bd

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

example/components/RemoteDataSetExample2.tsx

+1-1
Original file line numberDiff line numberDiff 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}

example/components/RemoteDataSetExample3.tsx

+1
Original file line numberDiff line numberDiff 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

src/index.tsx

+9-9
Original file line numberDiff line numberDiff 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

src/types/index.ts

+1-1
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)