question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't load @mui/x-date-pickers as a custom component

See original GitHub issue

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

Screenshot 2022-05-16 at 01 05 33

Expected behavior 🤔

It renders like https://mui.com/x/react-date-pickers/date-picker/#basic-usage

Screenshot 2022-05-16 at 01 08 19

Steps to reproduce 🕹

Steps:

  1. Open https://master--toolpad.mui.com/_toolpad/app/cl1c2j0l512939zo6a43575en/editor/codeComponents/cl37w530600003f6agke0m1cv

or

  1. Create a custom component with:
import * as React from "react";
import { createComponent } from "@mui/toolpad-core";
import { TextField } from "@mui/material";
import {
  AdapterDateFns,
  LocalizationProvider,
  DatePicker,
} from "https://esm.sh/@mui/x-date-pickers@5.0.0-alpha.3";

export interface DatePickerRootProps {
  msg: string;
}

function DatePickerRoot({ msg }: DatePickerRootProps) {
  const [value, setValue] = React.useState<Date | null>(null);

  return (
    <LocalizationProvider dateAdapter={AdapterDateFns}>
      <DatePicker
        label="Basic example"
        value={value}
        onChange={(newValue) => {
          setValue(newValue);
        }}
        renderInput={(params) => <TextField {...params} />}
      />
    </LocalizationProvider>
  );
}

DatePickerRoot.defaultProps = {
  msg: "Hello world!",
};

export default createComponent(DatePickerRoot, {
  argTypes: {
    msg: { typeDef: { type: "string" } },
  },
});

Context 🔦

I need a date picker for my license key generator:

Screenshot 2022-05-16 at 01 06 47

https://master--toolpad.mui.com/_toolpad/app/cl1c2j0l512939zo6a43575en/editor/pages/cl2kfa28r00013f69amqv1prv

Your environment 🌎

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Janpotcommented, Jul 19, 2022

Yep, this illustrates well what we discussed in our last retro. Components imported from urls will have to depend on react. In this case esm.sh rewrites the import to one that lives on the CDN itself (see https://cdn.esm.sh/v78/@mui/x-date-pickers@5.0.0-alpha.3/es2022/x-date-pickers.js). So it brings in its own instance of the react module. It’s currently behaving as expected. We could look into what should be the behavior if your imported code tries to import a bare module identifier, but it’s going to raise questions about versioning and backwards compatibility.

Regarding @mui/... packages, besides providing their components in the visual editor, I think we should also allow importing them using bare imports in custom components

edit:

another example:

import * as React from "react";
import { createComponent } from "@mui/toolpad-core";
import PhoneInput from "https://esm.sh/react-phone-number-input";

export interface ExternalProps {
  date: string;
}

function External({ date }: ExternalProps) {
  return <PhoneInput placeholder="Enter phone number" />;
}

export default createComponent(External, {
  argTypes: {},
});
0reactions
oliviertassinaricommented, Oct 12, 2022

@Janpot Thanks, I have used this version in the end:

import * as React from "react";
import { createComponent } from "@mui/toolpad-core";
import TextField from "@mui/material/TextField";
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
import { LocalizationProvider } from "@mui/x-date-pickers/LocalizationProvider";
import { DatePicker } from "@mui/x-date-pickers/DatePicker";
import dayjs from "dayjs";

interface BasicDatePickerProps {
  value: string;
  label: string;
  disabled: boolean;
  onChange: (newValue: string) => void;
}

function BasicDatePicker({
  value,
  label,
  disabled,
  onChange = () => {},
}: BasicDatePickerProps) {
  return (
    <LocalizationProvider dateAdapter={AdapterDayjs}>
      <DatePicker
        label={label}
        disabled={disabled}
        value={value ? dayjs(value) : null}
        onChange={onChange}
        renderInput={(params) => (
          <TextField fullWidth size="small" {...params} />
        )}
      />
    </LocalizationProvider>
  );
}

export default createComponent(BasicDatePicker, {
  argTypes: {
    label: {
      typeDef: { type: "string" },
      defaultValue: "Label",
    },
    disabled: {
      typeDef: { type: "boolean" },
    },
    value: {
      typeDef: { type: "string" },
      onChangeProp: "onChange",
      onChangeHandler: (newValue: dayjs.Dayjs) => {
        return newValue?.toDate();
      },
      defaultValue: "",
      defaultValueProp: "defaultValue",
    },
  },
});

https://master--toolpad.mui.com/_toolpad/app/cl4hla83p01949xoizo5uxf2a/codeComponents/g823nzp

Read more comments on GitHub >

github_iconTop Results From Across the Web

Date picker, Time picker React components
Date pickers and Time pickers allow selecting a single value from a pre-determined set. On mobile, pickers are best suited for display in...
Read more >
material ui lab date picker, Can't resolve '@ ...
This works for me very well npm install @mui/lab. In Material UI v5 you need to install @mui/lab.My dependencies are here :
Read more >
Date picker component
v-date-picker is a fully featured date selection component that lets users select a date, or range of dates. # Usage. Date pickers come...
Read more >
React 17 Material-UI Datepicker and Timepicker Example ...
For using the Datepicker component, we also need to install the Material UI ... Import MuiPickersUtilsProvider and KeyboardDatePicker from ...
Read more >
The Ultimate MUI v5 DatePicker and TimePicker Tutorial (Plus ...
MUI recently changed the import location for the DatePicker component. It is now imported from @mui/x-date-pickers . Here are the imports needed ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found