Skip to content
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

Standalone search Box Error #3406

Open
Villva-vinoth opened this issue Feb 6, 2025 · 0 comments
Open

Standalone search Box Error #3406

Villva-vinoth opened this issue Feb 6, 2025 · 0 comments

Comments

@Villva-vinoth
Copy link

Villva-vinoth commented Feb 6, 2025

import { Form, Input } from "antd";
import React, { useEffect, useRef, useState } from "react";
import { useJsApiLoader, StandaloneSearchBox } from "@react-google-maps/api";
import { FormItems } from "../types.form";
const libraries = ['places'];
const GoogleAddress = React.memo((formItem: FormItems) => {
const { name, initialValue, rules, disabled, placeholder } = formItem;
const form = Form.useFormInstance();

const { isLoaded ,loadError} = useJsApiLoader({
id: "google-map-script",
googleMapsApiKey: "process.env.MAP_KEY",
[libraries] :libraries,
});
const [address, setAddress] = useState(initialValue || "");
const inputRef = useRef<google.maps.places.SearchBox | null>(null);
const handlePlaceChange = () => {
if (inputRef.current) {
const places = inputRef.current.getPlaces();
if (places && places.length > 0) {
const formattedAddress = places[0].formatted_address || "";
setAddress(formattedAddress);
form.setFieldValue(name, formattedAddress);
}
}
};
console.log("isloaded", isLoaded,loadError);
useEffect(() => {
const fieldValue = form.getFieldValue(name);
if (fieldValue && fieldValue !== address) {
setAddress(fieldValue);
}
}, [name, address, form]);
if (!isLoaded || loadError) return

Loading...

;
return (
<Form.Item style={{ width: "100%" }} initialValue={initialValue} name={name} rules={rules}>
{isLoaded && (
<StandaloneSearchBox onLoad={(ref) => (inputRef.current = ref)} onPlacesChanged={handlePlaceChange}>
<Input
placeholder={placeholder || "Enter address"}
disabled={disabled}
value={address}
onChange={(e) => {
setAddress(e.target.value);
form.setFieldValue(name, e.target.value);
}}
/>

)}
</Form.Item>
);
});

export default GoogleAddress;

errors : -

You need to provide libraries={["places"]} prop to '' component undefined

The above error occurred in the StandaloneSearchBox component

Answers are welcome !
-thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant