Skip to content

Commit 2843468

Browse files
committed
fix: Made apiKey optional. Fixes #18
1 parent 3f2c789 commit 2843468

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

dist/AddressAutocomplete.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export interface AddressAutocompleteValue extends PlaceType, google.maps.places.
3030
export interface AddressAutocompleteProps extends Omit<
3131
AutocompleteProps<PlaceType, false, boolean, false, ChipTypeMap['defaultComponent']
3232
>, 'options' | 'renderInput' | 'onChange'> {
33-
apiKey: string;
33+
apiKey?: string;
3434
fields?: string[];
3535
label: string;
3636
renderInput?: (params: AutocompleteRenderInputParams) => React.ReactNode;

src/AddressAutocomplete.tsx

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,24 @@ const AddressAutocomplete = ({
157157
}, []);
158158

159159
// Load Google Maps API script if not already loaded
160-
if (typeof window !== 'undefined' && !loaded.current) {
161-
if (!document.querySelector('#google-maps')) {
162-
const script = document.createElement('script');
163-
164-
script.setAttribute('async', '');
165-
script.setAttribute('id', 'google-maps');
166-
script.src = `https://maps.googleapis.com/maps/api/js?key=${apiKey}&libraries=places`;
167-
document.querySelector('head')?.appendChild(script);
160+
useEffect(() => {
161+
if (typeof window !== 'undefined' && !loaded.current) {
162+
if (!document.querySelector('#google-maps')) {
163+
const script = document.createElement('script');
164+
165+
if (!apiKey) {
166+
console.error('You need to provide an API key to use this component');
167+
}
168+
169+
script.setAttribute('async', '');
170+
script.setAttribute('id', 'google-maps');
171+
script.src = `https://maps.googleapis.com/maps/api/js?key=${apiKey}&libraries=places`;
172+
document.querySelector('head')?.appendChild(script);
173+
}
174+
175+
loaded.current = true;
168176
}
169-
170-
loaded.current = true;
171-
}
177+
}, [apiKey, loaded]);
172178

173179
// Autocomplete predictions fetcher
174180
const fetch = useMemo(() => throttle((

0 commit comments

Comments
 (0)