我正在使用ProgressiveInput
在react-native中自动完成。我正在调用函数searchLocation(query)
onChangeText(),它返回ListView
个建议。
这是我的代码: -
import React, { Component } from 'react';
import ProgressiveInput from 'react-native-progressive-input';
import {
StyleSheet,
Text,
View,
ListView,
TouchableOpacity,
} from 'react-native';
const ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1.id !== r2.id,
});
export default class FirstProject extends Component {
constructor(props) {
super(props);
this.state = {
isLoading: false,
dataSource: ds.cloneWithRows([]),
value: '',
query:'',
};
}
searchLocation(query) {
this.setState({ isLoading: true, value: query });
fetch('https://reactnativecode.000webhostapp.com/FruitsList.php', {
method: 'POST',
body: JSON.stringify({
"name":query
})
}).then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
dataSource: ds.cloneWithRows(responseJson),
}, function() {
});
})
.catch((error) => {
console.error(error);
});
}
renderRow(prediction) {
return (
<TouchableOpacity
onPress={() => this.onListItemClicked(prediction)}
style={styles.listItem}
>
<Text>{prediction}</Text>
</TouchableOpacity>
);
}
renderSeparator() {
return <View style={styles.listItemSeparator} />;
}
onInputCleared() {
this.setState({
value: '',
isLoading: false,
dataSource: ds.cloneWithRows([]),
});
}
onListItemClicked(prediction) {
this.setState({
value: prediction,
dataSource: ds.cloneWithRows([]),
isLoading: true,
});
this.setState({ isLoading: false });
}
render() {
return (
<View style={styles.container}>
<ProgressiveInput
value={this.state.value}
style={styles.progressiveInput}
isLoading={this.state.isLoading}
oonChangeText={(text) => this.searchLocation(text)}
onInputCleared={this.onInputCleared}
/>
<View style={styles.listViewContainer}>
<ListView
enableEmptySections
style={styles.listView}
dataSource={this.state.dataSource}
renderRow={this.renderRow}
renderSeparator={this.renderSeparator}
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'grey',
flexDirection: 'column',
justifyContent: 'flex-start',
},
map: {
position: 'absolute',
top: 0,
right: 0,
left: 0,
bottom: 0,
},
progressiveInput: {
marginTop: 20,
marginLeft: 10,
marginRight: 10,
},
listViewContainer: {
flex: 0,
},
listView: {
backgroundColor: 'white',
margin: 10,
},
listItem: {
padding: 10,
},
listItemSeparator: {
borderWidth: 0.5,
borderColor: 'lightgrey',
},
});
请建议我在这种情况下如何使用ProgressiveInput
。