-
I'm trying to do autofocus on validation errors using a controlled react-datepicker, but the onFocus not firing her is my code :
Codesandbox : |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
<Controller
control={control}
rules={{ required: "Date required" }}
name="ReactDatepicker"
render={({ field: { onChange, onBlur, name, value, ref } }) => (
<ReactDatePicker
name={name}
onChange={onChange}
onBlur={onBlur}
selected={value}
customInput={<Input inputRef={ref} />} // use custom input to control input field focus
onFocus={() => console.log('should trigger')} // this is what actually being focus
/>
)}
/> This works for me. Ref: https://reactdatepicker.com/#example-custom-input Edit: I also added |
Beta Was this translation helpful? Give feedback.
-
The react-datepicker implement a custom "ref" for its default input element therefore "ref" handling from "react-hook-form" is not triggered. You can set the "ref" from the parent component as a workaround for this case. Working sample: https://codesandbox.io/s/react-hook-form-focus-forked-yyhsi?file=/src/index.js |
Beta Was this translation helpful? Give feedback.
-
For RangeDatePicker still onFocus event is not firing but it is executing for singleDatePicker |
Beta Was this translation helpful? Give feedback.
@oualidmaz
The react-datepicker implement a custom "ref" for its default input element therefore "ref" handling from "react-hook-form" is not triggered. You can set the "ref" from the parent component as a workaround for this case.
Working sample: https://codesandbox.io/s/react-hook-form-focus-forked-yyhsi?file=/src/index.js