-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[pickers] How to migrate custom DateRangePicker from v5 to v6? #12057
Comments
Quick update here, I got an almost working version of my
I still got a bunch of errors and warnings because I'm spreading |
Hi @lildesert, There is a section in the docs about how to replace the default Field with a browser field: custom field From your code example it looks like the ref is not transported correctly and you are trying to assign the const DateInput = React.forwardRef((props: DateInputProps, ref: React.Ref<HTMLDivElement>) => {
const { id, error, ...textFieldProps } = props;
const { inputRef, ...fieldProps } = useDateField<Date, typeof textFieldProps>(textFieldProps);
return (
<div className="flex flex-col gap-2 w-full" ref={ref} id={id}>
<div className="flex items-center h-10 px-5 w-full rounded-input focus-within:ring-2 focus-within:ring-inset focus-within:ring-primary ring-1 ring-inset ring-gray-300 shadow-sm">
<input
ref={inputRef}
id={id}
{...fieldProps}
className="px-0 h-8 block w-full text-gray-900 placeholder:text-gray-400 border-0 focus:ring-0 focus:outline-none sm:leading-6 disabled:cursor-not-allowed disabled:border-gray-200 disabled:bg-gray-50 disabled:text-gray-500"
/>
{fieldProps.InputProps?.endAdornment}
</div>
{error && <p className="text-sm text-red-600">{error}</p>}
</div>
);
}); Did that help you? PS: For the future please provide a live example in codesandbox or stackblitz. Its way easier for us to help you then! 🙇🏼 |
Hi @michelengelen, thanks for your help! My FieldDate now looks like this
Sorry for not providing a codesandbox but the Remix / Mui / Tailwind setup could be a bit difficult. I still have an issue with the SSR of Mui into Remix but I'll probably open another issue if I don't find a solution. |
How did we do @lildesert? |
Hello @lildesert, we are happy that you are using and enjoying the Pickers package. 🙌 In regards to your question, let's first start with a suggestion - consider wrapping your whole application in a I've tried playing around a bit and the following seemed like the least intrusive overriding changing only what is relevant: import Box from '@mui/material/Box';
import { TextFieldProps } from '@mui/material/TextField';
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
import { DatePicker as MuiDatePicker } from '@mui/x-date-pickers/DatePicker';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
// import { formatDateToISO8601ForBackend, isValidDate } from '@teamout/utils';
import React, { HTMLAttributes, useEffect, useState } from 'react';
// import { FieldWrapper } from './FieldWrapper';
export const DateInput: React.FC<
TextFieldProps & {
error?: string;
}
> = ({ error, ...other }) => {
const { inputRef, inputProps, InputProps, size, ownerState, focused, sx, ...rest } = other;
return (
<Box
className="flex flex-col gap-2 w-full"
data-e2e="date-picker"
ref={InputProps?.ref}
// consume `sx` to benefit from the clearable button hiding behavior
sx={sx}
>
<div className="flex items-center h-10 px-5 w-full rounded-input focus-within:ring-2 focus-within:ring-inset focus-within:ring-primary ring-1 ring-inset ring-gray-300 shadow-sm">
<input
ref={inputRef}
{...inputProps}
className="px-0 h-8 block w-full text-gray-900 placeholder:text-gray-400 border-0 focus:ring-0 focus:outline-none sm:leading-6 disabled:cursor-not-allowed disabled:border-gray-200 disabled:bg-gray-50 disabled:text-gray-500"
{...rest}
/>
{InputProps?.endAdornment}
</div>
{error && <p className="text-sm text-red-600">{error}</p>}
</Box>
);
};
export const FieldDate: React.FC<{
name: string;
label?: string;
minDate?: Date;
maxDate?: Date;
error?: string;
defaultValue?: Date;
required?: boolean;
className?: string;
}> = ({ name, label, minDate, maxDate, error, defaultValue, required, className }) => {
const [innerValue, setInnerValue] = useState<Date | null>(defaultValue ?? null);
useEffect(() => {
setInnerValue(defaultValue ?? null);
}, [defaultValue]);
return (
<div
// label={label}
// error={error}
// required={required}
className={className}
>
<LocalizationProvider dateAdapter={AdapterDateFns}>
<MuiDatePicker
value={innerValue}
onChange={(newValue) => setInnerValue(newValue)}
minDate={minDate}
maxDate={maxDate}
slotProps={{ field: { clearable: true } }}
slots={{ textField: DateInput }}
/>
</LocalizationProvider>
<input
type="hidden"
name={name}
value={innerValue ? innerValue.toISOString() : ''}
// value={isValidDate(innerValue) ? formatDateToISO8601ForBackend(innerValue) : ''}
/>
</div>
);
}; Things to note:
|
The problem in depth
Hey there!
I started to work on a migration from mui-x v5 to mui-x v6 and I'm having some trouble updating my code and keeping all my existing customizations.
I use Mui exclusively for the DatePicker and DateRangePicker which are the best available for a React app IMO.
My goal is to integrate these components into our UI component library and customize a bit their colors and aspects using Tailwind.
Maybe I didn't put enough time into it but I find it really difficult to achieve this goal.
Here's my current component code using v5
FieldDate.tsx
DateRangeField.tsx
I also use a theme to customize the colors inside the calendar
Since I use remix.run, I followed the example on this repo to integrate Mui & emotion with Remix: https://github.com/mui/material-ui/tree/master/examples/material-ui-remix-ts
(I'm just not using
<CssBaseline />
since we don't need it)By the way I'm running into another problem with Emotion SSR since on some page load we can see some kind of SVG sprite blinking into the screen then disappear.
I tried to follow the v6 docs and use the
slots
props of the components but I'm not making good progress so far. I don't know if you could help or guide me through this but it would be very appreciated.Thanks a lot!
By the way you can find an example of how we use the date range picker on this page https://flights.teamout.com/
Your environment
`npx @mui/envinfo`
Mostly using Chrome
Search keywords: date range picker migrate
Order ID: 68974
The text was updated successfully, but these errors were encountered: