Skip to content

Commit 1926920

Browse files
committed
feat: add clear button functionality
1 parent 4b38cef commit 1926920

File tree

1 file changed

+149
-53
lines changed

1 file changed

+149
-53
lines changed

src/GooglePlacesTextInput.js

Lines changed: 149 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,57 +15,9 @@ import {
1515
ActivityIndicator,
1616
Keyboard,
1717
I18nManager,
18+
Platform,
1819
} from 'react-native';
1920

20-
const styles = StyleSheet.create({
21-
container: {
22-
marginHorizontal: 16,
23-
marginTop: 10,
24-
},
25-
input: {
26-
height: 50,
27-
borderRadius: 6,
28-
borderWidth: 1,
29-
paddingHorizontal: 10,
30-
backgroundColor: 'white',
31-
fontSize: 16,
32-
},
33-
loadingIndicator: {
34-
position: 'absolute',
35-
top: '50%',
36-
transform: [{ translateY: -10 }],
37-
},
38-
rtlText: {
39-
writingDirection: 'rtl',
40-
},
41-
suggestionsContainer: {
42-
backgroundColor: '#efeff1', // default background
43-
borderRadius: 6,
44-
marginTop: 3,
45-
overflow: 'hidden',
46-
maxHeight: 200,
47-
},
48-
suggestionItem: {
49-
padding: 10,
50-
borderBottomWidth: StyleSheet.hairlineWidth,
51-
borderBottomColor: '#c8c7cc',
52-
},
53-
mainText: {
54-
fontSize: 16,
55-
textAlign: 'left',
56-
color: '#000000',
57-
},
58-
secondaryText: {
59-
fontSize: 14,
60-
color: '#666',
61-
marginTop: 2,
62-
textAlign: 'left',
63-
},
64-
rightAligned: {
65-
right: 15,
66-
},
67-
});
68-
6921
const DEFAULT_GOOGLE_API_URL =
7022
'https://places.googleapis.com/v1/places:autocomplete';
7123
const GooglePlacesTextInput = forwardRef(
@@ -84,6 +36,7 @@ const GooglePlacesTextInput = forwardRef(
8436
onTextChange,
8537
debounceDelay = 200,
8638
showLoadingIndicator = true,
39+
showClearButton = true,
8740
style = {},
8841
},
8942
ref
@@ -267,19 +220,55 @@ const GooglePlacesTextInput = forwardRef(
267220
<View>
268221
<TextInput
269222
ref={inputRef}
270-
style={[styles.input, style.input]}
223+
style={[
224+
styles.input,
225+
style.input,
226+
{ paddingRight: showClearButton ? 75 : 45 }, // Adjust padding based on clear button visibility
227+
]}
271228
placeholder={placeHolderText}
272-
placeholderTextColor={style.placeholder?.color || '#666666'} // Default color
229+
placeholderTextColor={style.placeholder?.color || '#666666'}
273230
value={inputText}
274231
onChangeText={handleTextChange}
275232
onFocus={handleFocus}
276233
onBlur={() => setShowSuggestions(false)}
234+
clearButtonMode="never" // Disable iOS native clear button
277235
/>
236+
237+
{/* Clear button - shown only if showClearButton is true */}
238+
{showClearButton && inputText !== '' && (
239+
<TouchableOpacity
240+
style={[isRTL ? styles.leftIcon : styles.rightIcon]}
241+
onPress={() => {
242+
setInputText('');
243+
setPredictions([]);
244+
setShowSuggestions(false);
245+
onPlaceSelect?.(null);
246+
onTextChange?.('');
247+
inputRef.current?.focus();
248+
}}
249+
>
250+
<Text
251+
style={Platform.select({
252+
ios: styles.iOSclearButton,
253+
android: styles.androidClearButton,
254+
})}
255+
>
256+
{'×'}
257+
</Text>
258+
</TouchableOpacity>
259+
)}
260+
261+
{/* Loading indicator - position adjusts based on showClearButton */}
278262
{loading && showLoadingIndicator && (
279263
<ActivityIndicator
280-
style={[styles.loadingIndicator, styles.rightAligned]}
264+
style={[
265+
isRTL ? styles.leftLoadingIcon : styles.rightLoadingIcon,
266+
!showClearButton &&
267+
(isRTL ? styles.leftEdge : styles.rightEdge),
268+
styles.loadingIndicator,
269+
]}
281270
size={'small'}
282-
color={style.loadingIndicator?.color || '#000000'} // Default color
271+
color={style.loadingIndicator?.color || '#000000'}
283272
/>
284273
)}
285274
</View>
@@ -289,4 +278,111 @@ const GooglePlacesTextInput = forwardRef(
289278
}
290279
);
291280

281+
const styles = StyleSheet.create({
282+
container: {
283+
marginHorizontal: 16,
284+
marginTop: 10,
285+
},
286+
input: {
287+
height: 50,
288+
borderRadius: 6,
289+
borderWidth: 1,
290+
paddingHorizontal: 10,
291+
backgroundColor: 'white',
292+
fontSize: 16,
293+
},
294+
loadingIndicator: {
295+
position: 'absolute',
296+
top: '50%',
297+
transform: [{ translateY: -10 }],
298+
},
299+
rtlText: {
300+
writingDirection: 'rtl',
301+
},
302+
suggestionsContainer: {
303+
backgroundColor: '#efeff1', // default background
304+
borderRadius: 6,
305+
marginTop: 3,
306+
overflow: 'hidden',
307+
maxHeight: 200,
308+
},
309+
suggestionItem: {
310+
padding: 10,
311+
borderBottomWidth: StyleSheet.hairlineWidth,
312+
borderBottomColor: '#c8c7cc',
313+
},
314+
mainText: {
315+
fontSize: 16,
316+
textAlign: 'left',
317+
color: '#000000',
318+
},
319+
secondaryText: {
320+
fontSize: 14,
321+
color: '#666',
322+
marginTop: 2,
323+
textAlign: 'left',
324+
},
325+
rightAligned: {
326+
right: 15,
327+
},
328+
rightIcon: {
329+
position: 'absolute',
330+
top: '50%',
331+
transform: [{ translateY: -13 }],
332+
right: 12,
333+
padding: 0,
334+
},
335+
leftIcon: {
336+
position: 'absolute',
337+
top: '50%',
338+
transform: [{ translateY: -13 }],
339+
left: 12,
340+
padding: 0,
341+
},
342+
rightLoadingIcon: {
343+
position: 'absolute',
344+
top: '50%',
345+
transform: [{ translateY: -10 }],
346+
right: 45,
347+
},
348+
leftLoadingIcon: {
349+
position: 'absolute',
350+
top: '50%',
351+
transform: [{ translateY: -10 }],
352+
left: 45,
353+
},
354+
rightEdge: {
355+
right: 12,
356+
},
357+
leftEdge: {
358+
left: 12,
359+
},
360+
iOSclearButton: {
361+
fontSize: 18,
362+
fontWeight: '400',
363+
color: 'white',
364+
backgroundColor: '#999',
365+
width: 25,
366+
height: 25,
367+
borderRadius: 12.5,
368+
textAlign: 'center',
369+
textAlignVertical: 'center',
370+
lineHeight: 19,
371+
includeFontPadding: false,
372+
},
373+
androidClearButton: {
374+
fontSize: 24,
375+
fontWeight: '400',
376+
color: 'white',
377+
backgroundColor: '#999',
378+
width: 24,
379+
height: 24,
380+
borderRadius: 12,
381+
textAlign: 'center',
382+
textAlignVertical: 'center',
383+
lineHeight: 20,
384+
includeFontPadding: false,
385+
},
386+
});
387+
292388
export default GooglePlacesTextInput;

0 commit comments

Comments
 (0)