Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Mui X v6 #363

Open
dylmye opened this issue Apr 3, 2023 · 7 comments
Open

Support for Mui X v6 #363

dylmye opened this issue Apr 3, 2023 · 7 comments
Labels
enhancement New feature or request

Comments

@dylmye
Copy link

dylmye commented Apr 3, 2023

Summary 馃挕

Mui released v6 on the 3rd of March. There are a number of breaking changes but also improvements.

A migration guide is available here: https://mui.com/x/migration/migration-pickers-v5

Motivation 馃敠

The `renderInput` prop has been removed in version 6.0 of the Date and Time Pickers.
You can replace it with the `textField` component slot in most cases.
For more information, please have a look at the migration guide
Uncaught TypeError: date.isBefore is not a function

(the second error is mui/mui-x#8351 )

Thanks!

@dylmye dylmye added the enhancement New feature or request label Apr 3, 2023
@cliedeman
Copy link
Collaborator

Looking at this in the next few days

@ethanIncentify
Copy link

any updates on this?

@cliedeman
Copy link
Collaborator

The components were re-written from the ground up. Spent an hour or two on this but didn't make much progress.
Will probably try again in a week or so

@sunho-cho
Copy link

sunho-cho commented Jun 6, 2023

import * as React from 'react';

import TextField, { TextFieldProps } from '@mui/material/TextField';
import {
  DatePicker as MuiDatePicker,
  DatePickerProps as MuiDatePickerProps,
} from '@mui/x-date-pickers/DatePicker';
import { FieldProps, getIn } from 'formik';

import { createErrorHandler } from './errorHandler';

export interface FDatePickerProps<TDate>
  extends FieldProps,
    Omit<MuiDatePickerProps<TDate>, 'name' | 'value' | 'error'> {
  textField?: TextFieldProps;
}

export function fieldToDatePicker<TDate>({
  field: { onChange: _onChange, ...field },
  form: { isSubmitting, touched, errors, setFieldValue, setFieldError, setFieldTouched },
  textField: { helperText, onBlur, ...textField } = {},
  disabled,
  label,
  onChange,
  onError,
  ...props
}: FDatePickerProps<TDate>): MuiDatePickerProps<TDate> {
  const fieldError = getIn(errors, field.name);
  const showError = getIn(touched, field.name) && !!fieldError;

  return {
    slotProps: {
      textField: {
        error: showError,
        helperText: showError ? fieldError : helperText,
        label: label,
        onBlur:
          onBlur ??
          function () {
            setFieldTouched(field.name, true, true);
          },
        ...textField,
      },
    },
    disabled: disabled ?? isSubmitting,
    onChange:
      onChange ??
      function (date) {
        // Do not switch this order, otherwise you might cause a race condition
        // See https://github.com/formium/formik/issues/2083#issuecomment-884831583
        setFieldTouched(field.name, true, false);
        setFieldValue(field.name, date, true);
      },
    onError: onError ?? createErrorHandler(fieldError, field.name, setFieldError),
    ...field,
    ...props,
  };
}

export function FDatePicker<TDate>({ ...props }: FDatePickerProps<TDate>) {
  // custom props
  props.textField = {
    ...props.textField,
    size: 'small',
  };

  return <MuiDatePicker format="YYYY-MM-DD" {...fieldToDatePicker(props)} />;
}

FDatePicker.displayName = 'FormikJoyDatePicker';

This is the version I modified for use with Formik.

However, the downside is that props.textField cannot be passed externally.
I'm trying to figure out why not.

@dylmye
Copy link
Author

dylmye commented Oct 11, 2023

How's it going? :)

@alex-boyko
Copy link

    onError: onError ?? createErrorHandler(fieldError, field.name, setFieldError),

@sunho-cho thank you for sharing! what did you end up doing in createErrorHandler?

@melong0124
Copy link

    onError: onError ?? createErrorHandler(fieldError, field.name, setFieldError),

@sunho-cho thank you for sharing! what did you end up doing in createErrorHandler?

export function createErrorHandler(
fieldError: unknown,
fieldName: string,
setFieldError: (field: string, message?: string) => void
) {
return (error?: ReactNode) => {
if (error !== fieldError && error !== '') {
setFieldError(fieldName, error ? String(error) : undefined);
}
};
}

It's part of this project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants