最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use react-hook-form with my customized react-date-picker? - Stack Overflow

programmeradmin5浏览0评论

I am trying to use react-hook-form with my customized date-picker, but I only see this example (Is it possible to use react-datepicker with react hooks forms?), which is using the default react-date-picker.

However, it just only works on the original react-date-picker. I tried the same way with my customized date-picker, and it doesn't work...

This is my customized date-picker:

import React, { useState } from 'react';
import ReactDatePicker from 'react-datepicker';
import tw from "date-fns/locale/zh-TW";

import "react-datepicker/dist/react-datepicker.css";

const DatePicker = props => {
    const [date, setDate] = useState('');
    return (
        <ReactDatePicker
            className="form-control"
            selected={date}
            onChange={date => setDate(date)}
            locale={tw}
            dateFormat="yyyy/MM/dd"
            dateFormatCalendar="yyyy年 MM月"
            isClearable
        />
    )
};

export default DatePicker;

I am trying to use react-hook-form with my customized date-picker, but I only see this example (Is it possible to use react-datepicker with react hooks forms?), which is using the default react-date-picker.

However, it just only works on the original react-date-picker. I tried the same way with my customized date-picker, and it doesn't work...

This is my customized date-picker:

import React, { useState } from 'react';
import ReactDatePicker from 'react-datepicker';
import tw from "date-fns/locale/zh-TW";

import "react-datepicker/dist/react-datepicker.css";

const DatePicker = props => {
    const [date, setDate] = useState('');
    return (
        <ReactDatePicker
            className="form-control"
            selected={date}
            onChange={date => setDate(date)}
            locale={tw}
            dateFormat="yyyy/MM/dd"
            dateFormatCalendar="yyyy年 MM月"
            isClearable
        />
    )
};

export default DatePicker;

Here is how I use react-hook-form with my customized date-picker:

import React from 'react';
import { useForm, Controller } from 'react-hook-form';
import DatePicker from '../../ponents/UI/Form/DatePicker';

const Form = props => {
    const { register, handleSubmit, control} = useForm();

    const onSubmit = (data) => {
        console.log(data);
    }
    return (
        <form onSubmit={handleSubmit(onSubmit)}>
            <label>First Name:</label>
            <input type="text" name='firstName' ref={register} />

            <label>Last Name:</label>
            <input type='text' name='lastName' ref={register} />

            <label>birthday:</label>
            <Controller as={DatePicker} control={control} valueName="selected" name="birthday" />

            <input type="submit" value="Submit" />
        </form>
    );
}

export default Form;

After I submit the form, the 'birthday' value is undefined. Do I still need to add any props to my customized date-picker?

{ birthday: undefined, firstName: "Mike", lastName: "Smith" }

Share Improve this question asked May 5, 2020 at 3:49 theedchentheedchen 2,0544 gold badges11 silver badges21 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

The customised form control currently does not offer any props to control it from outside the ponent. For someone actually using the ponent, it has to have both selected and onChange fields to extract value out of it (The react-date-picker has these props and hence works)

Controller by default has an onChange which reads from an event passed to it, which is why you see it omitted from examples like this:

<Controller as={TextField} name="TextField" control={control} defaultValue="" />

To change your custom ponent to work with Controlller syntax, you can expose the selected and onChange accordingly:

const DatePicker = ({ selected, onChange }) => {
    return (
        <ReactDatePicker
            className="form-control"
            selected={selected}
            onChange={onChange}
            locale={tw}
            dateFormat="yyyy/MM/dd"
            dateFormatCalendar="yyyy年 MM月"
            isClearable
        />
    )
};

and on the Controller:

<Controller 
  as={DatePicker} 
  control={control} 
  valueName="selected"
  name="birthday"
  onChange={(date) => date};
/>

Also it's better if you pass onChangeName="onChangeDates" instead so you can pass value in.

<ControllerWrapper
            as={
              <DatePicker
                error={has(formErrors, fieldsConfiguration.datePicker.name)}
              />
            }
            rules={fieldsConfiguration.datePicker.rules}
            name={fieldsConfiguration.datePicker.name}
            onChangeName="onChangeDates"
            onChange={dateRangePickerSelector}
          />
发布评论

评论列表(0)

  1. 暂无评论