Skip to content

Commit abde927

Browse files
authored
Merge pull request #16 from Christopher2K/main
feat: Propagate onFocus/onBlur to the caller
2 parents 51679af + 2ecc6c8 commit abde927

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ const PlaceDetailsExample = () => {
211211
| placeHolderText | string | No | - | Placeholder text for the input |
212212
| onPlaceSelect | (place: Place, sessionToken?: string) => void | Yes | - | Callback when a place is selected |
213213
| onTextChange | (text: string) => void | No | - | Callback when input text changes |
214+
| onFocus | (event: NativeSyntheticEvent<TextInputFocusEventData>) => void | No | - | Callback when input is focused |
215+
| onBlur | (event: NativeSyntheticEvent<TextInputFocusEventData>) => void | No | - | Callback when input is blurred |
214216
| **Search Configuration** |
215217
| proxyUrl | string | No | - | Custom proxy URL for API requests (Required for Expo web) |
216218
| languageCode | string | No | - | Language code (e.g., 'en', 'fr') |

src/GooglePlacesTextInput.tsx

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import {
55
useRef,
66
useState,
77
} from 'react';
8-
import type { StyleProp, TextStyle, ViewStyle } from 'react-native';
8+
import type {
9+
StyleProp,
10+
TextStyle,
11+
ViewStyle,
12+
TextInputProps,
13+
TextInputFocusEventData,
14+
NativeSyntheticEvent,
15+
} from 'react-native';
916
import {
1017
ActivityIndicator,
1118
FlatList,
@@ -72,7 +79,9 @@ interface GooglePlacesTextInputStyles {
7279
};
7380
}
7481

75-
interface GooglePlacesTextInputProps {
82+
type TextInputInheritedProps = Pick<TextInputProps, 'onFocus' | 'onBlur'>;
83+
84+
interface GooglePlacesTextInputProps extends TextInputInheritedProps {
7685
apiKey: string;
7786
value?: string;
7887
placeHolderText?: string;
@@ -139,6 +148,8 @@ const GooglePlacesTextInput = forwardRef<
139148
detailsFields = [],
140149
onError,
141150
enableDebug = false,
151+
onFocus,
152+
onBlur,
142153
},
143154
ref
144155
) => {
@@ -398,7 +409,11 @@ const GooglePlacesTextInput = forwardRef<
398409
setSessionToken(generateSessionToken());
399410
};
400411

401-
const handleFocus = (): void => {
412+
const handleFocus = (
413+
event: NativeSyntheticEvent<TextInputFocusEventData>
414+
): void => {
415+
onFocus?.(event);
416+
402417
if (skipNextFocusFetch.current) {
403418
skipNextFocusFetch.current = false;
404419
return;
@@ -409,6 +424,20 @@ const GooglePlacesTextInput = forwardRef<
409424
}
410425
};
411426

427+
const handleBlur = (
428+
event: NativeSyntheticEvent<TextInputFocusEventData>
429+
): void => {
430+
onBlur?.(event);
431+
432+
setTimeout(() => {
433+
if (suggestionPressing.current) {
434+
suggestionPressing.current = false;
435+
} else {
436+
setShowSuggestions(false);
437+
}
438+
}, 10);
439+
};
440+
412441
const renderSuggestion = ({ item }: { item: PredictionItem }) => {
413442
const { mainText, secondaryText } = item.placePrediction.structuredFormat;
414443

@@ -521,15 +550,7 @@ const GooglePlacesTextInput = forwardRef<
521550
value={inputText}
522551
onChangeText={handleTextChange}
523552
onFocus={handleFocus}
524-
onBlur={() => {
525-
setTimeout(() => {
526-
if (suggestionPressing.current) {
527-
suggestionPressing.current = false;
528-
} else {
529-
setShowSuggestions(false);
530-
}
531-
}, 10);
532-
}}
553+
onBlur={handleBlur}
533554
clearButtonMode="never" // Disable iOS native clear button
534555
/>
535556

0 commit comments

Comments
 (0)