Skip to content

Not able to render date picker component for MUI #1409

New issue

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

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
YashM10 opened this issue Sep 5, 2023 · 9 comments · Fixed by #1411
Closed

Not able to render date picker component for MUI #1409

YashM10 opened this issue Sep 5, 2023 · 9 comments · Fixed by #1411
Assignees
Labels
bug Something isn't working Help needed Investiage Possible bug with unknown cause MUI MUI pull request released

Comments

@YashM10
Copy link

YashM10 commented Sep 5, 2023

scope: MUI
code:

import FormTemplate from '@data-driven-forms/mui-component-mapper/form-template';
import FormRenderer from '@data-driven-forms/react-form-renderer/form-renderer';
import componentMapper from '@data-driven-forms/mui-component-mapper/component-mapper';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import componentTypes from '@data-driven-forms/react-form-renderer/component-types/component-types';
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
import { useEffect, useState } from 'react';
import { injectIntl } from 'react-intl';


const DynamicForm = () => {
  const [schema1, setSchema1] = useState<any>({});
  const schema3 = {
    fields: [
      {
        component: componentTypes.TEXT_FIELD,
        name: 'account_name',
        label: 'Account Name',
        FormFieldGridProps: { xs: 4, p: 2 },
      },
      {
        component: componentTypes.SELECT,
        name: 'contract_length',
        label: 'Contract Length',
        FormFieldGridProps: { xs: 4, p: 2 },
        options: [
          {
            label: 'Days',
            value: 'days',
          },
          {
            label: 'Weeks',
            value: 'weeks',
          },
          {
            label: 'Months',
            value: 'months',
          },
          {
            label: 'Years',
            value: 'years',
          },
        ],
        isSearchable: true,
        isClearable: true,
      },
      {
        component: componentTypes.SELECT,
        name: 'billing_frequency',
        label: 'Billing Frequency',
        FormFieldGridProps: { xs: 4, p: 2 },
        options: [
          {
            label: 'Weekly',
            value: 'weekly',
          },
          {
            label: 'Monthly',
            value: 'monthly',
          },
          {
            label: 'Yearly',
            value: 'yearly',
          },
        ],
        isSearchable: true,
        isClearable: true,
      },
      {
        component: componentTypes.SELECT,
        name: 'payment_terms',
        label: 'Payment Terms',
        FormFieldGridProps: { xs: 4, p: 2 },
      },
      {
        component: componentTypes.SELECT,
        name: 'dummy',
        label: 'Payment Terms Dummy',
        FormFieldGridProps: { xs: 4, p: 2 },
      },
      {
        component: componentTypes.DATE_PICKER,
        name: 'contract_start',
        label: 'Contract Start',
        FormFieldGridProps: { xs: 3 },
        isClearable: true,
      },
    ],
  };
  useEffect(() => {
    setSchema1(schema3);
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  const schema2 = {
    fields: [
      {
        component: componentTypes.FIELD_ARRAY,
        name: 'new_terms',
        fieldKey: 'field_array',
        title: 'New Terms',
        label: 'New Terms',
        FormFieldGridProps: { xs: 12 },
        itemDefault: {
          name: 'Enter field key',
          label: 'Enter field label',
          component: '',
          initalValue: 'Enter default value',
          options: '',
        },
        fields: [
          {
            component: componentTypes.TEXT_FIELD,
            name: 'name',
            label: 'Key',
            FormFieldGridProps: { xs: 4, p: 2 },
          },
          {
            component: componentTypes.TEXT_FIELD,
            name: 'label',
            label: 'Label',
            FormFieldGridProps: { xs: 4, p: 2 },
          },
          {
            component: componentTypes.SELECT,
            name: 'component',
            label: 'Type',
            FormFieldGridProps: { xs: 4, p: 2 },
            options: [
              {
                label: 'Text Field',
                value: 'text-field',
              },
              {
                label: 'Select',
                value: 'select',
              },
              {
                label: 'Checkbox',
                value: 'checkbox',
              },
              {
                label: 'Radio Button',
                value: 'radio',
              },
              {
                label: 'Date Picker',
                value: 'date-picker',
              },
            ],
            isSearchable: true,
            isClearable: true,
          },
          {
            component: componentTypes.TEXT_FIELD,
            name: 'initialValue',
            label: 'Default Value',
            FormFieldGridProps: { xs: 4, p: 2 },
          },
          {
            component: componentTypes.TEXT_FIELD,
            name: 'options',
            label: 'Options',
            placeholder: 'Please enter comma separated values',
            condition: {
              when: `new_terms[0].component`,
              is: 'select',
            },
            FormFieldGridProps: { xs: 4, p: 2 },
          },
        ],
      },
    ],
  };
  const onSubmit2 = (values: any, form: any) => {
    if (values.new_terms?.length) {
      console.log(values.new_terms[0], form, schema1);
      console.log([...schema1.fields, values.new_terms[0]]);
      const outputData = values.new_terms.map((newTerm: any) => {
        return { ...newTerm, FormFieldGridProps: { xs: 4, p: 2 } };
      });
      setSchema1({
        fields: [...schema1.fields, ...outputData],
      });
      form.reset();
    }
  };
  const onSubmit1 = (values: any) => {
    console.log(values);
  };
  return (
    <LocalizationProvider dateAdapter={AdapterDateFns}>
      {schema1?.fields?.length ? (
        <FormRenderer
          schema={schema1}
          FormTemplate={FormTemplate}
          componentMapper={componentMapper}
          onSubmit={onSubmit1}
        />
      ) : null}

      <br />
      <FormRenderer
        componentMapper={componentMapper}
        schema={schema2}
        FormTemplate={FormTemplate}
        onSubmit={onSubmit2}
      />
    </LocalizationProvider>
  );
};

export default injectIntl(DynamicForm);

error: Can not find utils in context. It looks like you forgot to wrap your component in LocalizationProvider, or pass dateAdapter prop directly.

@rvsia rvsia added bug Something isn't working MUI MUI pull request Help needed Investiage Possible bug with unknown cause labels Sep 5, 2023
@Hyperkid123 Hyperkid123 self-assigned this Sep 13, 2023
@Hyperkid123
Copy link
Member

@YashM10 Are there any additional steps to reproduce the issue? I am able to render the form with the date picker.

I used the schema you posted, and just added a parent IntlProvider context. That is the only extra thing added. Can you tell us what version of MUI, DDF, and the date pickers libraries you are using?

screenshot-localhost_8080-2023 09 13-13_41_28

@kelvingo935
Copy link

I'm having the same issue. I have tried directly using the DatePicker component in my code, and it's working fine. However, the problem occurs when I use DDF to render the form.

packages.json

    "@data-driven-forms/mui-component-mapper": "^3.21.4",
    "@data-driven-forms/react-form-renderer": "^3.21.4",
    "@mui/material": "^5.14.9",
    "@mui/x-date-pickers": "^6.13.0",

index.tsx (I have already wrapped with LocalizationProvider on the global app)

<React.StrictMode>
    <ThemeProvider theme={darkTheme}>
        <LocalizationProvider dateAdapter={AdapterLuxon}>
            <App />
        </LocalizationProvider>
    </ThemeProvider>
</React.StrictMode>

@YashM10
Copy link
Author

YashM10 commented Sep 14, 2023

@YashM10 Are there any additional steps to reproduce the issue? I am able to render the form with the date picker.

I used the schema you posted, and just added a parent IntlProvider context. That is the only extra thing added. Can you tell us what version of MUI, DDF, and the date pickers libraries you are using?

screenshot-localhost_8080-2023 09 13-13_41_28

So found out the issue here
The issue is because of the mui/x-date-pickers version.
I was using version 6.10.1 which gives the error, but on downgrading the version to 5.0.20, it seems to work but with the older version of date picker

@YashM10
Copy link
Author

YashM10 commented Sep 14, 2023

Also then we need to change the implementation of date pickers used everywhere else.

@Hyperkid123
Copy link
Member

OK Thanks for the details. I'll play around with the versions to reproduce the bug. I'll let you know ASAP..

@Hyperkid123
Copy link
Member

@kelvingo935 @YashM10 OK I think the issue is with a duplicate version of the date pickers dependencies. DDF has the date picker as a dependency, not a peer dependency which probably resulted in a module resolution conflict and the date picker was not able to reach the parent context of LocalizationProvider as it was technically imported from a different package (the explicitly installed in your apps) than the date picker component itself (nested dependency of DDF).

Moving the date pickers to peer dependency and relaxing the version restrictions should do the trick. I'll do some more testing to exactly replicate your use cases. If all goes well I'll open a PR soon.

Hyperkid123 added a commit that referenced this issue Sep 14, 2023
Resolves issues with duplicate dependencies on yarn and relaxes the
restriction of the package version.

Fixes: #1409
Hyperkid123 added a commit that referenced this issue Sep 14, 2023
Resolves issues with duplicate dependencies on yarn and relaxes the
restriction of the package version.

Fixes: #1409
Hyperkid123 added a commit that referenced this issue Sep 14, 2023
Resolves issues with duplicate dependencies on yarn and relaxes the
restriction of the package version.

Fixes: #1409
@Hyperkid123 Hyperkid123 reopened this Sep 14, 2023
@Hyperkid123
Copy link
Member

A new version should be released soon. I'll keep the issue opened until I get thumbs up from you.

@rvsia rvsia added the released label Sep 14, 2023
@YashM10
Copy link
Author

YashM10 commented Sep 14, 2023

Thank you for the update!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working Help needed Investiage Possible bug with unknown cause MUI MUI pull request released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants