forked from cipto-hd/react-native-mapbox-places-autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
156 lines (150 loc) · 3.45 KB
/
index.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
import {
Image,
StyleSheet,
Text,
TextInput,
TouchableOpacity,
View,
} from "react-native";
import React from "react";
import { closeBtnUri } from "./images";
import usePlacesAutocomplete from "./utils/usePlacesAutocomplete";
/**
* Main Component
*/
const MapboxPlacesAutocomplete = ({
inputStyle,
containerStyle,
inputClassName = "",
containerClassName = "",
placeholder = "Address",
accessToken = "",
onPlaceSelect,
onClearInput,
setValue,
value,
onPressIn,
}) => {
const id = "origin";
const placesAutocomplete = usePlacesAutocomplete(
accessToken,
setValue,
value
);
return (
<View
style={[styles.container, containerStyle]}
className={containerClassName}
>
{placesAutocomplete.suggestions?.length > 0 && value && (
<PlaceSuggestionList
{...{ placesAutocomplete, onPlaceSelect }}
setValue={setValue}
/>
)}
<TextInput
autoCorrect={false}
value={value}
{...{ ...placesAutocomplete, placeholder }}
style={inputStyle}
className={inputClassName}
onPressIn={onPressIn}
/>
{value && (
<TouchableOpacity
style={styles.clearBtn}
onPress={() => {
setValue("");
onClearInput({ id }); // tell the consumer about which input is cleared
}}
>
<Image source={closeBtnUri} style={styles.clearBtnImage} />
</TouchableOpacity>
)}
</View>
);
};
/** Place Suggestion List below text input */
const PlaceSuggestionList = ({
placesAutocomplete,
onPlaceSelect,
setValue,
}) => {
return (
<View style={styles.suggestionList}>
{placesAutocomplete.suggestions.map((suggestion, index) => {
return (
<TouchableOpacity
key={index}
onPress={() => {
setValue(suggestion.place_name);
placesAutocomplete.setSuggestions([]);
onPlaceSelect && onPlaceSelect(suggestion);
}}
>
<Text style={styles.suggestionItem}>{suggestion.place_name}</Text>
</TouchableOpacity>
);
})}
</View>
);
};
export default MapboxPlacesAutocomplete;
const styles = StyleSheet.create({
container: {
margin: 5,
position: "relative",
height: 32,
display: "flex",
flexDirection: "column",
justifyContent: "center",
alignItems: "center",
},
clearBtnImage: { width: 20, height: 20 },
clearBtn: { position: "absolute", top: 6, right: 5 },
suggestionList: {
position: "absolute",
paddingTop: 50,
borderTopLeftRadius: 20,
borderTopRightRadius: 20,
borderBottomLeftRadius: 8,
borderBottomRightRadius: 8,
paddingHorizontal: 20,
paddingVertical: 8,
backgroundColor: "#212121",
borderRadius: 4,
marginHorizontal: 2,
top: -5,
left: -40,
right: 50,
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 2,
},
shadowOpacity: 0.25,
shadowRadius: 3.84,
borderColor: "#333",
borderWidth: 1,
},
suggestionItem: {
color: "#FFFFFF",
fontWeight: "300",
fontSize: 14,
paddingVertical: 14,
},
creditBox: {
display: "flex",
flexDirection: "row",
justifyContent: "flex-end",
alignItems: "center",
padding: 4,
},
creditText: {
color: "#6b7280",
fontWeight: "400",
fontSize: 12,
padding: 2,
},
creditImage: { width: 16, height: 16, marginLeft: 2 },
});