Skip to content

Commit 61346b3

Browse files
[pickers] Use usePickerContext() and usePickerActionsContext() instead of passing props to the shortcuts and toolbar slots (mui#15948)
1 parent dac7c3d commit 61346b3

File tree

59 files changed

+488
-397
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+488
-397
lines changed

docs/data/date-pickers/shortcuts/ChangeImportance.tsx

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import Stack from '@mui/material/Stack';
44
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
55
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
66
import { DatePicker } from '@mui/x-date-pickers/DatePicker';
7-
import {
8-
PickersShortcutsItem,
9-
PickerShortcutChangeImportance,
10-
} from '@mui/x-date-pickers/PickersShortcuts';
7+
import { PickersShortcutsItem } from '@mui/x-date-pickers/PickersShortcuts';
8+
import { PickerChangeImportance } from '@mui/x-date-pickers/models';
119
import ToggleButtonGroup from '@mui/material/ToggleButtonGroup';
1210
import ToggleButton from '@mui/material/ToggleButton';
1311

@@ -80,7 +78,7 @@ const shortcutsItems: PickersShortcutsItem<Dayjs | null>[] = [
8078

8179
export default function ChangeImportance() {
8280
const [changeImportance, setChangeImportance] =
83-
React.useState<PickerShortcutChangeImportance>('accept');
81+
React.useState<PickerChangeImportance>('accept');
8482

8583
return (
8684
<LocalizationProvider dateAdapter={AdapterDayjs}>

docs/data/date-pickers/shortcuts/CustomizedRangeShortcuts.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
99
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
1010
import { StaticDateRangePicker } from '@mui/x-date-pickers-pro/StaticDateRangePicker';
1111

12+
import { useIsValidValue, usePickerContext } from '@mui/x-date-pickers/hooks';
13+
1214
const shortcutsItems = [
1315
{
1416
label: 'This Week',
@@ -51,7 +53,9 @@ const shortcutsItems = [
5153
];
5254

5355
function CustomRangeShortcuts(props) {
54-
const { items, onChange, isValid, changeImportance = 'accept' } = props;
56+
const { items, changeImportance = 'accept' } = props;
57+
const isValid = useIsValidValue();
58+
const { setValue } = usePickerContext();
5559

5660
if (items == null || items.length === 0) {
5761
return null;
@@ -63,7 +67,7 @@ function CustomRangeShortcuts(props) {
6367
return {
6468
label: item.label,
6569
onClick: () => {
66-
onChange(newValue, changeImportance, item);
70+
setValue(newValue, { changeImportance, shortcut: item });
6771
},
6872
disabled: !isValid(newValue),
6973
};

docs/data/date-pickers/shortcuts/CustomizedRangeShortcuts.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
PickersShortcutsProps,
1414
} from '@mui/x-date-pickers/PickersShortcuts';
1515
import { DateRange } from '@mui/x-date-pickers-pro/models';
16+
import { useIsValidValue, usePickerContext } from '@mui/x-date-pickers/hooks';
1617

1718
const shortcutsItems: PickersShortcutsItem<DateRange<Dayjs>>[] = [
1819
{
@@ -56,7 +57,9 @@ const shortcutsItems: PickersShortcutsItem<DateRange<Dayjs>>[] = [
5657
];
5758

5859
function CustomRangeShortcuts(props: PickersShortcutsProps<DateRange<Dayjs>>) {
59-
const { items, onChange, isValid, changeImportance = 'accept' } = props;
60+
const { items, changeImportance = 'accept' } = props;
61+
const isValid = useIsValidValue<DateRange<Dayjs>>();
62+
const { setValue } = usePickerContext<DateRange<Dayjs>>();
6063

6164
if (items == null || items.length === 0) {
6265
return null;
@@ -68,7 +71,7 @@ function CustomRangeShortcuts(props: PickersShortcutsProps<DateRange<Dayjs>>) {
6871
return {
6972
label: item.label,
7073
onClick: () => {
71-
onChange(newValue, changeImportance, item);
74+
setValue(newValue, { changeImportance, shortcut: item });
7275
},
7376
disabled: !isValid(newValue),
7477
};

docs/data/migration/migration-pickers-v7/migration-pickers-v7.md

+125-8
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,19 @@ This change causes a few breaking changes:
364364
);
365365
```
366366

367+
- The component passed to the `layout` slot no longer receives the `value` prop, instead you can use the `usePickerContext` hook:
368+
369+
```diff
370+
+import { usePickerContext } from '@mui/x-date-pickers/hooks';
371+
372+
// This contains a small behavior change.
373+
// If the picker receives an invalid date,
374+
// the old value equals `null`.
375+
// the new value equals the invalid date received.
376+
-const { value } = props;
377+
+const { value } = usePickerContext();
378+
```
379+
367380
- The component passed to the `layout` slot no longer receives the `disabled` and `readOnly` props, instead you can use the `usePickerContext` hook:
368381

369382
```diff
@@ -376,7 +389,7 @@ This change causes a few breaking changes:
376389
+const { readOnly } = usePickerContext();
377390
```
378391

379-
- The component passed to the `layout` slot no longer receives an `isRtl` prop. If you need to access this information, you can use the `useRtl` hook from `@mui/system`:
392+
- The component passed to the `layout` slot no longer receives the `isRtl` prop. If you need to access this information, you can use the `useRtl` hook from `@mui/system`:
380393

381394
```diff
382395
+import { useRtl } from '@mui/system/RtlProvider';
@@ -398,7 +411,7 @@ This change causes a few breaking changes:
398411
+const isLandscape = orientation === 'landscape';
399412
```
400413

401-
- The component passed to the `layout` slot no longer receives a `wrapperVariant` prop, instead you can use the `usePickerContext` hook:
414+
- The component passed to the `layout` slot no longer receives the `wrapperVariant` prop, instead you can use the `usePickerContext` hook:
402415

403416
```diff
404417
+import { usePickerContext } from '@mui/x-date-pickers/hooks';
@@ -422,7 +435,8 @@ This change causes a few breaking changes:
422435
+const { onViewChange } = usePickerContext();
423436
```
424437

425-
- The component passed to the `layout` slot no longer receives the `onClear`, `onSetToday`, `onAccept`, `onCancel`, `onOpen`, `onClose` and `onDismiss` props, instead you can use the `usePickerActionsContext` or the `usePickerContext` hooks:
438+
- The component passed to the `layout` slot no longer receives the `onClear`, `onSetToday`, `onAccept`, `onCancel`, `onOpen`, `onClose` `onDismiss`, `onChange` and `onSelectShortcut` props.
439+
You can use the `usePickerActionsContext` or the `usePickerContext` hooks instead:
426440

427441
```diff
428442
+import { usePickerActionsContext } from '@mui/x-date-pickers/hooks';
@@ -446,7 +460,7 @@ This change causes a few breaking changes:
446460
+ setOpen(true);
447461
+}
448462

449-
-props.onClose();
463+
-const { onClose } = props;
450464
+const { setOpen } = usePickerActionsContext();
451465
+const onClose = event => {
452466
+ event.preventDefault();
@@ -462,6 +476,18 @@ This change causes a few breaking changes:
462476
-const { onDismiss } = props;
463477
+const { acceptValueChanges } = usePickerActionsContext();
464478
+const onDismiss = acceptValueChanges
479+
480+
-const { onChange } = props;
481+
-onChange(dayjs(), 'partial');
482+
-onChange(dayjs(), 'finish');
483+
+const { setValue } = usePickerActionsContext();
484+
+setValue(dayjs(), { changeImportance: 'set' });
485+
+setValue(dayjs(), { changeImportance: 'accept' });
486+
487+
-const { onSelectShortcut } = props;
488+
-onSelectShortcut(dayjs(), 'accept', myShortcut);
489+
+const { setValue } = usePickerActionsContext();
490+
+setValue(dayjs(), { changeImportance: 'accept', shortcut: myShortcut });
465491
```
466492

467493
:::success
@@ -471,6 +497,19 @@ This change causes a few breaking changes:
471497

472498
### Slot: `toolbar`
473499

500+
- The component passed to the `toolbar` slot no longer receives the `value` prop, instead you can use the `usePickerContext` hook:
501+
502+
```diff
503+
+import { usePickerContext } from '@mui/x-date-pickers/hooks';
504+
505+
// This contains a small behavior change.
506+
// If the picker receives an invalid date,
507+
// the old value would equal `null`.
508+
// the new value would equal the invalid date received.
509+
-const { value } = props;
510+
+const { value } = usePickerContext();
511+
```
512+
474513
- The component passed to the `toolbar` slot no longer receives the `disabled` and `readOnly` props, instead you can use the `usePickerContext` hook:
475514

476515
```diff
@@ -483,7 +522,17 @@ This change causes a few breaking changes:
483522
+const { readOnly } = usePickerContext();
484523
```
485524

486-
- The component passed to the `toolbar` slot no longer receives a `view`, `views` and `onViewChange` props, instead you can use the `usePickerContext` hook:
525+
- The component passed to the `toolbar` slot no longer receives the `isLandscape` prop, instead you can use the `usePickerContext` hook:
526+
527+
```diff
528+
+import { usePickerContext } from '@mui/x-date-pickers/hooks';
529+
530+
-const { isLandscape } = props;
531+
+const { orientation } = usePickerContext();
532+
+const isLandscape = orientation === 'landscape';
533+
```
534+
535+
- The component passed to the `toolbar` slot no longer receives the `view`, `views` and `onViewChange` props, instead you can use the `usePickerContext` hook:
487536

488537
```diff
489538
+import { usePickerContext } from '@mui/x-date-pickers/hooks';
@@ -498,9 +547,28 @@ This change causes a few breaking changes:
498547
+const { onViewChange } = usePickerContext();
499548
```
500549

550+
- The component passed to the `toolbar` slot no longer receives the `onChange` prop.
551+
You can use the `usePickerActionsContext` or the `usePickerContext` hooks instead:
552+
553+
```diff
554+
+import { usePickerActionsContext } from '@mui/x-date-pickers/hooks';
555+
556+
-const { onChange } = props;
557+
-onChange(dayjs(), 'partial');
558+
-onChange(dayjs(), 'finish');
559+
+const { setValue } = usePickerActionsContext();
560+
+setValue(dayjs(), { changeImportance: 'set' });
561+
+setValue(dayjs(), { changeImportance: 'accept' });
562+
```
563+
564+
:::success
565+
The `usePickerContext` also contain all the actions returned by `usePickerActionsContext`.
566+
The only difference is that `usePickerActionsContext` only contains variables with stable references that won't cause a re-render of your component.
567+
:::
568+
501569
### Slot: `tabs`
502570

503-
- The component passed to the `tabs` slot no longer receives a `view`, `views` and `onViewChange` props, instead you can use the `usePickerContext` hook:
571+
- The component passed to the `tabs` slot no longer receives the `view`, `views` and `onViewChange` props, instead you can use the `usePickerContext` hook:
504572

505573
```diff
506574
+import { usePickerContext } from '@mui/x-date-pickers/hooks';
@@ -517,7 +585,8 @@ This change causes a few breaking changes:
517585

518586
### Slot: `actionBar`
519587

520-
- The component passed to the `actionBar` slot no longer receives the `onClear`, `onSetToday`, `onAccept` and `onCancel` props. You can use the `usePickerActionsContext` or the `usePickerContext` hooks instead:
588+
- The component passed to the `actionBar` slot no longer receives the `onClear`, `onSetToday`, `onAccept` and `onCancel` props.
589+
You can use the `usePickerActionsContext` or the `usePickerContext` hooks instead:
521590

522591
```diff
523592
+import { usePickerActionsContext } from '@mui/x-date-pickers/hooks';
@@ -540,6 +609,44 @@ This change causes a few breaking changes:
540609
The only difference is that `usePickerActionsContext` only contains variables with stable references that won't cause a re-render of your component.
541610
:::
542611

612+
### Slot: `shortcuts`
613+
614+
- The component passed to the `shortcuts` slot no longer receives the `isLandscape` prop, instead you can use the `usePickerContext` hook:
615+
616+
```diff
617+
+import { usePickerContext } from '@mui/x-date-pickers/hooks';
618+
619+
-const { isLandscape } = props;
620+
+const { orientation } = usePickerContext();
621+
+const isLandscape = orientation === 'landscape';
622+
```
623+
624+
- The component passed to the `shortcuts` slot no longer receives the `onChange` prop.
625+
You can use the `usePickerActionsContext` or the `usePickerContext` hooks instead:
626+
627+
```diff
628+
-const { onChange } = props;
629+
-onChange(dayjs(), 'accept', myShortcut);
630+
+const { setValue } = usePickerActionsContext();
631+
+setValue(dayjs(), { changeImportance: 'accept', shortcut: myShortcut });
632+
```
633+
634+
:::success
635+
The `usePickerContext` also contain all the actions returned by `usePickerActionsContext`.
636+
The only difference is that `usePickerActionsContext` only contains variables with stable references that won't cause a re-render of your component.
637+
:::
638+
639+
- The component passed to the `shortcuts` slot no longer receives the `isValid` prop, instead you can use the `useIsValidValue` hook:
640+
641+
```diff
642+
+import { useIsValidValue } from '@mui/x-date-pickers/hooks';
643+
644+
-const { isValid } = props;
645+
-const isTodayValid = isValid(dayjs());
646+
+const isValidValue = useIsValidValue();
647+
+const isTodayValid = isValidValue(dayjs());
648+
```
649+
543650
## Renamed variables and types
544651

545652
The following variables and types have been renamed to have a coherent `Picker` / `Pickers` prefix:
@@ -586,7 +693,7 @@ The following variables and types have been renamed to have a coherent `Picker`
586693
+import { PickerValueType } from '@mui/x-date-pickers-pro';
587694
```
588695

589-
- `RangeFieldSection`
696+
- `RangeFieldSection`
590697

591698
```diff
592699
-import { RangeFieldSection } from '@mui/x-date-pickers-pro/models';
@@ -596,6 +703,16 @@ The following variables and types have been renamed to have a coherent `Picker`
596703
+import { FieldRangeSection } from '@mui/x-date-pickers-pro';
597704
```
598705

706+
- `PickerShortcutChangeImportance`
707+
708+
```diff
709+
-import { PickerShortcutChangeImportance } from '@mui/x-date-pickers/PickersShortcuts';
710+
-import { PickerShortcutChangeImportance } from '@mui/x-date-pickers';
711+
712+
+import { PickerChangeImportance } from '@mui/x-date-pickers/models';
713+
+import { PickerChangeImportance } from '@mui/x-date-pickers';
714+
```
715+
599716
## Hooks breaking changes
600717

601718
### `usePickerContext`

packages/x-date-pickers-pro/src/DateRangeCalendar/DateRangeCalendar.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ const DateRangeCalendar = React.forwardRef(function DateRangeCalendar(
292292
// This makes sure that `isWithinRange` works with any time in the start and end day.
293293
const valueDayRange = React.useMemo<PickerRangeValue>(
294294
() => [
295-
value[0] == null || !utils.isValid(value[0]) ? value[0] : utils.startOfDay(value[0]),
296-
value[1] == null || !utils.isValid(value[1]) ? value[1] : utils.endOfDay(value[1]),
295+
!utils.isValid(value[0]) ? value[0] : utils.startOfDay(value[0]),
296+
!utils.isValid(value[1]) ? value[1] : utils.endOfDay(value[1]),
297297
],
298298
[value, utils],
299299
);
@@ -386,7 +386,7 @@ const DateRangeCalendar = React.forwardRef(function DateRangeCalendar(
386386
const prevValue = React.useRef<PickerRangeValue | null>(null);
387387
React.useEffect(() => {
388388
const date = rangePosition === 'start' ? value[0] : value[1];
389-
if (!date || !utils.isValid(date)) {
389+
if (!utils.isValid(date)) {
390390
return;
391391
}
392392

0 commit comments

Comments
 (0)