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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pickers] Support date-fns v4 #14673

Merged
merged 5 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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.

Check warning on line 44 in docs/data/date-pickers/adapters-locale/adapters-locale.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Google.We] Try to avoid using first-person plural like 'We'. Raw Output: {"message": "[Google.We] Try to avoid using first-person plural like 'We'.", "location": {"path": "docs/data/date-pickers/adapters-locale/adapters-locale.md", "range": {"start": {"line": 44, "column": 1}}}, "severity": "WARNING"}

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 will have to import the adapter from `@mui/x-date-pickers/AdapterDateFnsV3` instead of `@mui/x-date-pickers/AdapterDateFns`.

Check warning on line 48 in docs/data/date-pickers/adapters-locale/adapters-locale.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Google.Will] Avoid using 'will'. Raw Output: {"message": "[Google.Will] Avoid using 'will'.", "location": {"path": "docs/data/date-pickers/adapters-locale/adapters-locale.md", "range": {"start": {"line": 48, "column": 37}}}, "severity": "WARNING"}
LukasTy marked this conversation as resolved.
Show resolved Hide resolved
:::

```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.

Loading