-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutocomplete.js
200 lines (191 loc) · 5.25 KB
/
Autocomplete.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import React, { Component } from "react";
import {
AppRegistry,
Text,
View,
FlatList,
List,
TouchableHighlight,
TextInput,
ScrollView,
Modal,
StyleSheet
} from "react-native";
import { Button } from "react-native-elements";
import I18n from "react-native-i18n";
import Icon from "react-native-vector-icons/FontAwesome";
/*
componant to get autocomplete on a input field
inital state : display a row with "icone"+ input + "clear button"
when you clik on the input it open a modal with the same previous row, and will display autocompleted item bottom it.
when you clik on item, set it with onSelectCallback function and close modal
usage :
<Autocomplete
title={"Localité"}
value={this.state.localite}
iconname={"location-arrow"}
placeholder={"Localité inconnue"}
autocompleteCallback={value => }
onSelectCallback={localite => this.setState({ localite })}
onClearCallback={values=>{
this.setState({ localite: null })
}}
/>
*/
export default class Autocomplete extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
showmodal: false,
autocompletedfields: []
};
}
toggleModalVisible() {
this.setState({ showmodal: !this.state.showmodal, autocompletedfields: [] });
}
renderNormal() {
return (
<View style={thisstyles.searchSection}>
<Icon style={{ padding: 10 }} name={this.props.iconname} size={20} color="#000" />
<TextInput
autoCorrect={false}
value={this.props.value}
style={thisstyles.input}
placeholder={this.props.placeholder}
onFocus={searchString => {
this.setState({ showmodal: true });
}}
underlineColorAndroid="transparent"
/>
<Icon
style={{ padding: 10 }}
name="close"
size={20}
color="#000"
onPress={res => this.props.onClearCallback()}
/>
</View>
);
}
renderWithModal() {
return (
<Modal
animationType={"none"}
transparent={false}
visible={this.state.modalOrganeVisible}
onRequestClose={() => this.toggleModalVisible()}
>
<View style={thisstyles.alignCenter}>
<Text style={thisstyles.title}>{this.props.title}</Text>
</View>
<View style={thisstyles.searchSection}>
<Icon style={{ padding: 10 }} name={this.props.iconname} size={20} color="#000" />
<TextInput
autoFocus={true}
autoCorrect={false}
value={this.props.value}
style={thisstyles.input}
placeholder={this.props.placeholder}
onChangeText={searchString => {
// this.setState({ value: searchString });
this.props.onSelectCallback(searchString);
this.props.autocompleteCallback(searchString).then(res => {
this.setState({ autocompletedfields: res });
});
}}
underlineColorAndroid="transparent"
/>
<Icon
style={{ padding: 10 }}
name="close"
size={20}
color="#000"
onPress={res => this.props.onSelectCallback(null)}
/>
</View>
<FlatList
data={this.state.autocompletedfields}
keyExtractor={(item, index) => item}
renderItem={({ item }) => this.renderItem(item)}
initialNumToRender={10}
ItemSeparatorComponent={this.renderSeparator}
/>
<View style={thisstyles.buttonBottom}>
<Button
title={I18n.t("annuler")}
onPress={() => this.toggleModalVisible()}
buttonStyle={thisstyles.cancelButton}
icon={{ name: "close", type: "font-awesome" }}
/>
</View>
</Modal>
);
}
renderSeparator = () => {
return (
<View
style={{
height: 1,
width: "96%",
backgroundColor: "#CED0CE",
marginLeft: "2%"
}}
/>
);
};
renderItem = item => {
// style={{ alignItems: "center", justifyContent: "center" }}
return (
<View>
<TouchableHighlight
underlayColor="grey"
onPress={() => {
this.props.onSelectCallback(item);
this.toggleModalVisible();
}}
style={[thisstyles.bordergrey, { margin: 5 }]}
>
<Text style={thisstyles.title}>{item}</Text>
</TouchableHighlight>
</View>
);
};
render() {
return this.state.showmodal ? this.renderWithModal() : this.renderNormal();
}
}
const thisstyles = StyleSheet.create({
searchSection: {
flexDirection: "row",
justifyContent: "center",
alignItems: "center",
backgroundColor: "#f0f8ff",
marginTop: 1,
marginBottom: 1
},
input: {
flex: 1,
margin: 2,
color: "black"
},
title: {
fontSize: 20,
margin: 5
},
alignCenter: {
justifyContent: "center",
alignItems: "center"
},
buttonBottomContainer: {
position: 'absolute',
bottom: 5,
left:5,
justifyContent: "center",
alignItems: "center"
},
cancelButton: {
margin: 5,
borderRadius: 10,
backgroundColor: "orange"
},
});