首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何集成react-google-places-autocomplete和react-hook-from?

如何集成react-google-places-autocomplete和react-hook-from?
EN

Stack Overflow用户
提问于 2021-10-22 04:25:42
回答 1查看 389关注 0票数 3

我希望在我的表单中有一个位置输入,如下所示

我为这个https://www.npmjs.com/package/react-google-places-autocomplete使用了现成的组件

代码语言:javascript
复制
import React from "react";
import GooglePlacesAutocomplete from "react-google-places-autocomplete";

const GooglePlacesAutocompleteComponent = () => (
  <div>
    <GooglePlacesAutocomplete
      apiKey="xxxxxxxxxxxxxxx"
    />
  </div>
);

export default Component;

我通常用react hook form和material ui Textfield来做下面的事情:

代码语言:javascript
复制
const validationSchema = Yup.object().shape({
  location: Yup.string().required("Location is required"),
});

const {
  control,
  handleSubmit,
  formState: { errors },
  reset,
  setError,
} = useForm({
  resolver: yupResolver(validationSchema),
});

and materialui textfield

<Controller
  name="location"
  control={control}
  render={({ field: { ref, ...field } }) => (
    <TextField
      {...field}
      inputRef={ref}
      fullWidth
      label="location"
      margin="dense"
      error={errors.location ? true : false}
    />
  )}
/>
<Typography variant="inherit" color="textSecondary">
  {errors.name?.message}
</Typography>

所以这里我必须使用GooglePlacesAutocompleteComponent而不是TextField

我想让用户知道这是必需的。

我认为像下面这样的事情应该是可能的,但我不知道应该通过什么道具:

代码语言:javascript
复制
<Controller
  name="location"
  control={control}
  render={({ field: { ref, ...field } }) => (
    <GooglePlacesAutocompleteComponent
      <--------------------------->
      But for this component how can i pass the below things
      {...field}
      inputRef={ref}
      fullWidth
      label="location"
      margin="dense"
      error={errors.location ? true : false}
      <--------------------------->
    />
  )}
/>
<Typography variant="inherit" color="textSecondary">
  {errors.name?.message}
</Typography>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-22 05:21:17

GooglePlacesAutocomplete在内部使用react-select。在RHF docs中,它向您展示了如何通过react-select与Select组件集成:

代码语言:javascript
复制
<Controller
  name="iceCreamType"
  control={control}
  render={({ field }) => <Select 
    {...field} 
    options={[
      { value: "chocolate", label: "Chocolate" },
      { value: "strawberry", label: "Strawberry" },
      { value: "vanilla", label: "Vanilla" }
    ]} 
  />}
/>

GooglePlacesAutocomplete公开了一个SelectProps属性来让你覆盖Select属性,所以这就是你如何在RHF中使用它:

代码语言:javascript
复制
const GooglePlacesAutocompleteComponent = ({ error, ...field }) => {
  return (
    <div>
      <GooglePlacesAutocomplete
        apiKey="xxxxxxxxxxxxxxx"
        selectProps={{ ...field, isClearable: true }}
      />
      {error && <div style={{ color: "red" }}>{error.message}</div>}
    </div>
  );
};

在你的表格里:

代码语言:javascript
复制
<Controller
  name="location"
  rules={{
    required: "This is a required field"
  }}
  control={control}
  render={({ field, fieldState }) => (
    <GooglePlacesAutocompleteComponent
      {...field}
      error={fieldState.error}
    />
  )}
/>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69671416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档