Skip to content
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
20 changes: 18 additions & 2 deletions src/google-places-autocomplete.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef, useImperativeHandle } from 'react';
import React, { forwardRef, useImperativeHandle, useCallback } from 'react';
import AsyncSelect from 'react-select/async';

import GooglePlacesAutocompleteProps, { GooglePlacesAutocompleteHandle } from './types';
Expand All @@ -24,6 +24,22 @@ const GooglePlacesAutocomplete: React.ForwardRefRenderFunction<GooglePlacesAutoc
withSessionToken: args.withSessionToken ?? false,
});

// Create combined loadOptions function
const combinedLoadOptions = useCallback((inputValue: string, callback: (options: any[]) => void) => {
const customSuggestions = args.customSuggestions || [];

// Fetch Google Places suggestions
fetchSuggestions(inputValue, (googleSuggestions: any[]) => {
// Combine custom suggestions with Google suggestions
const combinedSuggestions = [
...customSuggestions,
...googleSuggestions
];

callback(combinedSuggestions);
});
}, [fetchSuggestions, args.customSuggestions]);

useImperativeHandle(ref, () => ({
getSessionToken: () => {
return sessionToken;
Expand All @@ -36,7 +52,7 @@ const GooglePlacesAutocomplete: React.ForwardRefRenderFunction<GooglePlacesAutoc
return (
<AsyncSelect
{...args.selectProps ?? {}}
loadOptions={fetchSuggestions}
loadOptions={combinedLoadOptions}
getOptionValue={({ value }) => value.place_id}
/>
);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ export default interface GooglePlacesAutocompleteProps {
onLoadFailed?: (error: Error) => void;
selectProps?: AsyncProps<Option, false, GroupBase<Option>>;
withSessionToken?: boolean;
customSuggestions?: Array<Option>;
}