Skip to content

Commit 30bda6e

Browse files
author
maksymkordiyak
committed
2 parents 2f532f0 + eb45651 commit 30bda6e

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ Change valueExtractor and pass the data to Autocomplete without fetchDataUrl
163163
renderIcon | Render icon near input | Function | -
164164
valueExtractor | Extract value from item (args: item, index) | Function | ({ value }) => value
165165
rightTextExtractor | Extract value from item (args: item, index) | Function | ({ value }) => value
166+
fetchData | Fetch data for autocomplete | Function | (searchInput) => Promise
166167

167168
### Authors:
168169

components/Autocomplete/index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,22 @@ class Autocomplete extends Component {
5757

5858
async triggerChange() {
5959
const {inputValue, items} = this.state;
60-
const {fetchDataUrl, valueExtractor} = this.props;
61-
if (fetchDataUrl) {
60+
const {fetchData, fetchDataUrl, valueExtractor} = this.props;
61+
if (fetchData) {
62+
try {
63+
const response = await fetchData(inputValue);
64+
if (response.length && this.mounted) {
65+
this.setState({items: response, loading: false});
66+
} else {
67+
this.setState({items: [NO_DATA], loading: false});
68+
}
69+
if (this.dropdown) {
70+
this.dropdown.onPress(this.container);
71+
}
72+
} catch (error) {
73+
throw new Error(error);
74+
}
75+
} else if (fetchDataUrl) {
6276
try {
6377
const response = await get(fetchDataUrl, {search: inputValue});
6478
if (response.length && this.mounted) {
@@ -222,6 +236,7 @@ Autocomplete.propTypes = {
222236
onDropdownClose: func.isRequired,
223237
onDropdownShow: func.isRequired,
224238
rightTextExtractor: func,
239+
fetchData: func,
225240
};
226241

227242
export default Autocomplete;

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ type AutocompleteProps = {
1515
spinnerColor?: string,
1616
listHeader?: string,
1717
fetchDataUrl?: string,
18-
fetchDataParams?: any,
1918
noDataText?: string;
2019
inputContainerStyle?: any;
2120
inputStyle?: any;
@@ -38,6 +37,7 @@ type AutocompleteProps = {
3837
renderIcon?: () => void,
3938
valueExtractor?: (item: any) => void,
4039
rightTextExtractor?: (item: any) => void,
40+
fetchData?: (search: string) => Promise<any>,
4141
}
4242

4343
export class Autocomplete extends React.Component<AutocompleteProps, any> {

0 commit comments

Comments
 (0)