Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[mui-x][DateTimePicker][v6] How to set onChange and onBlur with 3rd party form library?

Code example with Formik (not reproducible): https://stackblitz.com/edit/next-typescript-ffn4ta?file=components/CustomDateTimePicker.tsx

Description

Well, mui-x DateTimePicker v5 used to provide us onBlur or onChange with event, so that we can communicate with 3rd party library (like doing onChange={e => onChange(e.target.value)} or so).

But this time, we have values like date or string instead of event.

How do I manage to it work with Formik (or something else)?

Expected result

I am expecting DateTimePicker to work with Formik (also other 3rd part libs)

Thus, we can use like:

<DateTimePicker
  value={value}
  onChange={newValue => onChange(new ChangeEvent(...))}
  onBlur={() => onBlur(new FocusEvent('blur'))}
/>
like image 908
RikuS3n Avatar asked Nov 15 '25 21:11

RikuS3n


1 Answers

Sorry if this isn't answering your question, but you're able to add onFocus and onBlur functionality to date picker with the following! Docs here under the textField slot prop.

    const [isDateFieldFocused, setDateFieldFocused] = useState(false);

    const handleDateFieldFocus = () => {
       setDateFieldFocused(true);
    };

    const handleDateFieldBlur = () => {
       setDateFieldFocused(false);
    };

    <StyledDatePicker
        value={value}
        onChange={(newValue: any) => onChange(newValue)}
        slotProps={{
            textField: {
                onFocus: handleDateFieldFocus,
                onBlur: handleDateFieldBlur,
            },
        }}
    />
like image 95
Savanah Avatar answered Nov 18 '25 19:11

Savanah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!