Skip to content

Add onSubmitEditingSelectFirst option #936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions GooglePlacesAutocomplete.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ interface GooglePlacesAutocompleteProps {
onFail?: (error?: any) => void;
onNotFound?: () => void;
onPress?: (data: GooglePlaceData, detail: GooglePlaceDetail | null) => void;
onSubmitEditingSelectFirst?: boolean;
onTimeout?: () => void;
placeholder: string;
predefinedPlaces?: Place[];
Expand Down
17 changes: 17 additions & 0 deletions GooglePlacesAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,12 @@ export const GooglePlacesAutocomplete = forwardRef((props, ref) => {
inputRef?.current?.blur();
};

const _onSubmitEditing = useCallback((e) => {
if (props.onSubmitEditingSelectFirst && dataSource && dataSource.length > 0) {
_onPress(dataSource[0]);
}
}, [dataSource]);

const _onFocus = () => setListViewDisplayed(true);

const _renderPoweredLogo = () => {
Expand Down Expand Up @@ -829,6 +835,7 @@ export const GooglePlacesAutocomplete = forwardRef((props, ref) => {
let {
onFocus,
onBlur,
onSubmitEditing,
onChangeText, // destructuring here stops this being set after onChangeText={_handleChangeText}
clearButtonMode,
InputComp,
Expand Down Expand Up @@ -875,6 +882,14 @@ export const GooglePlacesAutocomplete = forwardRef((props, ref) => {
}
: _onBlur
}
onSubmitEditing={
onSubmitEditing
? (e) => {
_onSubmitEditing(e);
onSubmitEditing(e);
}
: _onSubmitEditing
}
clearButtonMode={clearButtonMode || 'while-editing'}
onChangeText={_handleChangeText}
{...userProps}
Expand Down Expand Up @@ -921,6 +936,7 @@ GooglePlacesAutocomplete.propTypes = {
onFail: PropTypes.func,
onNotFound: PropTypes.func,
onPress: PropTypes.func,
onSubmitEditingSelectFirst: PropTypes.bool,
onTimeout: PropTypes.func,
placeholder: PropTypes.string,
predefinedPlaces: PropTypes.array,
Expand Down Expand Up @@ -972,6 +988,7 @@ GooglePlacesAutocomplete.defaultProps = {
onFail: () => {},
onNotFound: () => {},
onPress: () => {},
onSubmitEditingSelectFirst: false,
onTimeout: () => console.warn('google places autocomplete: request timeout'),
placeholder: '',
predefinedPlaces: [],
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ _This list is a work in progress. PRs welcome!_
| onFail | function | returns if an unspecified error comes back from the API | | |
| onNotFound | function | returns if the Google Places Details API returns a 'not found' code (when you press a suggestion). | | |
| onPress | function | returns when after a suggestion is selected | | |
| onSubmitEditingSelectFirst | boolean | select the first result (if any) when a user submits the text input | false | true \| false |
| onTimeout | function | callback when a request timeout | `()=>console.warn('google places autocomplete: request timeout')` | |
| placeholder | string | placeholder text https://reactnative.dev/docs/textinput#placeholder | 'Search' | |
| predefinedPlaces | array | Allows you to show pre-defined places (e.g. home, work) | | |
Expand Down