Replies: 4 comments
-
|
Hi, can I take this issue? |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
|
Not sure if this is exactly related, but with MUI X v9 pickers, RHF's const fieldRef = useRef<FieldRef<Date | null> | null>(null);
useImperativeHandle(
ref,
() =>
({
focus: () => fieldRef.current?.focusField(0),
}) as HTMLInputElement,
[]
);
return (
<DatePicker
slotProps={{
...props.slotProps,
field: {
...props.slotProps?.field,
fieldRef: fieldRef,
},
}}
/>
);
}); |
Beta Was this translation helpful? Give feedback.
-
|
The bridge in the last comment is probably the right direction. The key detail is that React Hook Form focuses the registered field by calling For MUI X v9, the public API is import * as React from "react"
import { DatePicker } from "@mui/x-date-pickers/DatePicker"
import type { FieldRef } from "@mui/x-date-pickers/models"
const RHFDatePicker = React.forwardRef<HTMLInputElement, DatePickerProps<Date | null>>(
function RHFDatePicker(props, ref) {
const fieldRef = React.useRef<FieldRef<Date | null>>(null)
React.useImperativeHandle(
ref,
() =>
({
focus: () => fieldRef.current?.focusField(0),
}) as HTMLInputElement,
[]
)
return (
<DatePicker
{...props}
slotProps={{
...props.slotProps,
field: {
...props.slotProps?.field,
fieldRef,
},
}}
/>
)
}
)Then in <Controller
name="date"
control={control}
render={({ field, fieldState }) => (
<RHFDatePicker
value={field.value}
onChange={field.onChange}
ref={field.ref}
slotProps={{
textField: {
error: !!fieldState.error,
helperText: fieldState.error?.message,
},
}}
/>
)}
/>For MUI X v8, the same idea applies, but the prop name was still <DatePicker
slotProps={{
field: {
unstableFieldRef: fieldRef,
},
}}
/>MUI documents the v8 -> v9 rename here: https://next.mui.com/x/migration/migration-pickers-v8/#renamed-fieldref-props And the current MUI field docs describe using So I would not expect RHF core to be able to solve this generically unless MUI exposes a plain focusable input ref for the exact focus target. The stable integration point is to adapt the picker field API to the |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Version Number
7.67.0
Codesandbox/Expo snack
https://codesandbox.io/p/devbox/nifty-lumiere-5jnwkg?workspaceId=ws_Q8FtixA3WEZW4dYkGFmhL2
Steps to reproduce
Expected behaviour
The focus should be set to the date field. But it just flickers and the focus is moved away from the field again.
Setting the focus to a date input programatically works fine. Below the example I added a button and a second date field. Click on the button "set focus" and the focus will be set as expected.
There is something going on when submitting, that steals away the focus.
Update: Even more interestingly, I found out, that validating the form with "trigger" and shouldFocus: true works fine.
I updated the sandbox.
What browsers are you seeing the problem on?
Chrome
Relevant log output
Code of Conduct
Beta Was this translation helpful? Give feedback.
All reactions