Skip to content

Commit

Permalink
[pickers] Support date-fns v4 (mui#14673)
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Tyla <llukas.tyla@gmail.com>
Co-authored-by: Flavien DELANGLE <flaviendelangle@gmail.com>
  • Loading branch information
2 people authored and Arthur Balduini committed Sep 30, 2024
1 parent 11cfda2 commit 0745673
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 20 deletions.
6 changes: 3 additions & 3 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ module.exports = function getBabelConfig(api) {

if (process.env.NODE_ENV === 'test') {
plugins.push(['@babel/plugin-transform-export-namespace-from']);
// We replace `date-fns` imports with an aliased `date-fns@v3` version installed as `date-fns-v3` for tests.
// We replace `date-fns` imports with an aliased `date-fns@v4` version installed as `date-fns-v4` for tests.
// The plugin is patched to only run on `AdapterDateFnsV3.ts`.
// TODO: remove when we upgrade to date-fns v3 by default.
// TODO: remove when we upgrade to date-fns v4 by default.
plugins.push([
'babel-plugin-replace-imports',
{
test: /date-fns/i,
replacer: 'date-fns-v3',
replacer: 'date-fns-v4',
// This option is provided by the `patches/babel-plugin-replace-imports@1.0.2.patch` patch
filenameIncludes: 'src/AdapterDateFnsV3/',
},
Expand Down
10 changes: 5 additions & 5 deletions docs/data/date-pickers/adapters-locale/adapters-locale.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ import 'dayjs/locale/de';
For `date-fns`, import the locale and pass it to `LocalizationProvider`:

:::info
Both `date-fns` major versions (v2.x and v3.x) are supported.
We support `date-fns` package v2.x, v3.x, and v4.x major versions.

A single adapter cannot work for both `date-fns` v2.x and v3.x, because the way functions are exported has been changed in v3.x.
A single adapter cannot work for all `date-fns` versions, because the way functions are exported has been changed in v3.x.

To use `date-fns` v3.x, you will have to import the adapter from `@mui/x-date-pickers/AdapterDateFnsV3` instead of `@mui/x-date-pickers/AdapterDateFns`.
To use `date-fns` v3.x or v4.x, you need to import the adapter from `@mui/x-date-pickers/AdapterDateFnsV3` instead of `@mui/x-date-pickers/AdapterDateFns`.
:::

```tsx
// with date-fns v2.x
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
// with date-fns v3.x
// with date-fns v3.x or v4.x
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFnsV3';
// with date-fns v2.x
import de from 'date-fns/locale/de';
// with date-fns v3.x
// with date-fns v3.x or v4.x
import { de } from 'date-fns/locale/de';

<LocalizationProvider dateAdapter={AdapterDateFns} adapterLocale={de}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function PickersRenderingInstructions() {
`import { ${adapterName} } from '${componentPackage}/${adapterName}'`,
...(libraryUsed === 'date-fns'
? [
'// If you are using date-fns v3.x, please import the v3 adapter',
'// If you are using date-fns v3.x or v4.x, please import the v3 adapter',
`import { ${adapterName} } from '${componentPackage}/AdapterDateFnsV3'`,
]
: []),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
"cross-env": "^7.0.3",
"danger": "^12.3.3",
"date-fns-jalali-v3": "npm:date-fns-jalali@3.6.0-1",
"date-fns-v3": "npm:date-fns@3.6.0",
"date-fns-v4": "npm:date-fns@4.1.0",
"eslint": "^8.57.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-typescript": "^18.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/x-date-pickers-pro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"@emotion/styled": "^11.8.1",
"@mui/material": "^5.15.14 || ^6.0.0",
"@mui/system": "^5.15.14 || ^6.0.0",
"date-fns": "^2.25.0 || ^3.2.0",
"date-fns": "^2.25.0 || ^3.2.0 || ^4.0.0",
"date-fns-jalali": "^2.13.0-0 || ^3.2.0-0",
"dayjs": "^1.10.7",
"luxon": "^3.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/x-date-pickers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@emotion/styled": "^11.8.1",
"@mui/material": "^5.15.14 || ^6.0.0",
"@mui/system": "^5.15.14 || ^6.0.0",
"date-fns": "^2.25.0 || ^3.2.0",
"date-fns": "^2.25.0 || ^3.2.0 || ^4.0.0",
"date-fns-jalali": "^2.13.0-0 || ^3.2.0-0",
"dayjs": "^1.10.7",
"luxon": "^3.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class AdapterDateFns
if (typeof addDays !== 'function') {
throw new Error(
[
'MUI: The `date-fns` package v3.x is not compatible with this adapter.',
'MUI: This adapter is only compatible with `date-fns` v2.x package versions.',
'Please, install v2.x of the package or use the `AdapterDateFnsV3` instead.',
].join('\n'),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class AdapterDateFns
throw new Error(
[
`MUI: The \`date-fns\` package v2.x is not compatible with this adapter.`,
'Please, install v3.x of the package or use the `AdapterDateFns` instead.',
'Please, install v3.x or v4.x of the package or use the `AdapterDateFns` instead.',
].join('\n'),
);
}
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0745673

Please sign in to comment.