Skip to content

Commit

Permalink
[pickers] Use usePickerContext() and usePickerActionsContext() to…
Browse files Browse the repository at this point in the history
… get the actions in the `actionBar` slot and in internal components (#15843)

Signed-off-by: Flavien DELANGLE <flaviendelangle@gmail.com>
Co-authored-by: Michel Engelen <32863416+michelengelen@users.noreply.github.com>
  • Loading branch information
flaviendelangle and michelengelen authored Dec 19, 2024
1 parent 878a538 commit 9a8aa05
Show file tree
Hide file tree
Showing 32 changed files with 377 additions and 311 deletions.
17 changes: 11 additions & 6 deletions docs/data/date-pickers/custom-components/ActionBarComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { StaticDatePicker } from '@mui/x-date-pickers/StaticDatePicker';

import { usePickerTranslations } from '@mui/x-date-pickers/hooks';
import {
usePickerActionsContext,
usePickerTranslations,
} from '@mui/x-date-pickers/hooks';

function CustomActionBar(props) {
const { onAccept, onClear, onCancel, onSetToday, actions, className } = props;
const { actions, className } = props;
const translations = usePickerTranslations();
const { clearValue, setValueToToday, acceptValueChanges, cancelValueChanges } =
usePickerActionsContext();
const [anchorEl, setAnchorEl] = React.useState(null);
const open = Boolean(anchorEl);
const id = useId();
Expand All @@ -28,7 +33,7 @@ function CustomActionBar(props) {
return (
<MenuItem
onClick={() => {
onClear();
clearValue();
setAnchorEl(null);
}}
key={actionType}
Expand All @@ -42,7 +47,7 @@ function CustomActionBar(props) {
<MenuItem
onClick={() => {
setAnchorEl(null);
onCancel();
cancelValueChanges();
}}
key={actionType}
>
Expand All @@ -55,7 +60,7 @@ function CustomActionBar(props) {
<MenuItem
onClick={() => {
setAnchorEl(null);
onAccept();
acceptValueChanges();
}}
key={actionType}
>
Expand All @@ -68,7 +73,7 @@ function CustomActionBar(props) {
<MenuItem
onClick={() => {
setAnchorEl(null);
onSetToday();
setValueToToday();
}}
key={actionType}
>
Expand Down
17 changes: 11 additions & 6 deletions docs/data/date-pickers/custom-components/ActionBarComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { StaticDatePicker } from '@mui/x-date-pickers/StaticDatePicker';
import { PickersActionBarProps } from '@mui/x-date-pickers/PickersActionBar';
import { usePickerTranslations } from '@mui/x-date-pickers/hooks';
import {
usePickerActionsContext,
usePickerTranslations,
} from '@mui/x-date-pickers/hooks';

function CustomActionBar(props: PickersActionBarProps) {
const { onAccept, onClear, onCancel, onSetToday, actions, className } = props;
const { actions, className } = props;
const translations = usePickerTranslations();
const { clearValue, setValueToToday, acceptValueChanges, cancelValueChanges } =
usePickerActionsContext();
const [anchorEl, setAnchorEl] = React.useState<HTMLButtonElement | null>(null);
const open = Boolean(anchorEl);
const id = useId();
Expand All @@ -28,7 +33,7 @@ function CustomActionBar(props: PickersActionBarProps) {
return (
<MenuItem
onClick={() => {
onClear();
clearValue();
setAnchorEl(null);
}}
key={actionType}
Expand All @@ -41,7 +46,7 @@ function CustomActionBar(props: PickersActionBarProps) {
<MenuItem
onClick={() => {
setAnchorEl(null);
onCancel();
cancelValueChanges();
}}
key={actionType}
>
Expand All @@ -53,7 +58,7 @@ function CustomActionBar(props: PickersActionBarProps) {
<MenuItem
onClick={() => {
setAnchorEl(null);
onAccept();
acceptValueChanges();
}}
key={actionType}
>
Expand All @@ -65,7 +70,7 @@ function CustomActionBar(props: PickersActionBarProps) {
<MenuItem
onClick={() => {
setAnchorEl(null);
onSetToday();
setValueToToday();
}}
key={actionType}
>
Expand Down
18 changes: 11 additions & 7 deletions docs/data/date-pickers/custom-layout/AddComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import ListItemText from '@mui/material/ListItemText';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { StaticDatePicker } from '@mui/x-date-pickers/StaticDatePicker';

import RestaurantIcon from '@mui/icons-material/Restaurant';
import {
usePickerLayout,
Expand All @@ -17,17 +16,22 @@ import {
PickersLayoutContentWrapper,
} from '@mui/x-date-pickers/PickersLayout';

import { usePickerActionsContext } from '@mui/x-date-pickers/hooks';

function ActionList(props) {
const { onAccept, onClear, onCancel, onSetToday } = props;
const { className } = props;
const { clearValue, setValueToToday, acceptValueChanges, cancelValueChanges } =
usePickerActionsContext();

const actions = [
{ text: 'Accept', method: onAccept },
{ text: 'Clear', method: onClear },
{ text: 'Cancel', method: onCancel },
{ text: 'Today', method: onSetToday },
{ text: 'Accept', method: acceptValueChanges },
{ text: 'Clear', method: clearValue },
{ text: 'Cancel', method: cancelValueChanges },
{ text: 'Today', method: setValueToToday },
];

return (
<List>
<List className={className}>
{actions.map(({ text, method }) => (
<ListItem key={text} disablePadding>
<ListItemButton onClick={method}>
Expand Down
19 changes: 12 additions & 7 deletions docs/data/date-pickers/custom-layout/AddComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import ListItemText from '@mui/material/ListItemText';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { StaticDatePicker } from '@mui/x-date-pickers/StaticDatePicker';
import { PickersActionBarProps } from '@mui/x-date-pickers/PickersActionBar';
import RestaurantIcon from '@mui/icons-material/Restaurant';
import {
PickersLayoutProps,
Expand All @@ -18,17 +17,23 @@ import {
PickersLayoutContentWrapper,
} from '@mui/x-date-pickers/PickersLayout';
import { DateView } from '@mui/x-date-pickers/models';
import { PickersActionBarProps } from '@mui/x-date-pickers/PickersActionBar';
import { usePickerActionsContext } from '@mui/x-date-pickers/hooks';

function ActionList(props: PickersActionBarProps) {
const { onAccept, onClear, onCancel, onSetToday } = props;
const { className } = props;
const { clearValue, setValueToToday, acceptValueChanges, cancelValueChanges } =
usePickerActionsContext();

const actions = [
{ text: 'Accept', method: onAccept },
{ text: 'Clear', method: onClear },
{ text: 'Cancel', method: onCancel },
{ text: 'Today', method: onSetToday },
{ text: 'Accept', method: acceptValueChanges },
{ text: 'Clear', method: clearValue },
{ text: 'Cancel', method: cancelValueChanges },
{ text: 'Today', method: setValueToToday },
];

return (
<List>
<List className={className}>
{actions.map(({ text, method }) => (
<ListItem key={text} disablePadding>
<ListItemButton onClick={method}>
Expand Down
15 changes: 10 additions & 5 deletions docs/data/date-pickers/custom-layout/MovingActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { pickersLayoutClasses } from '@mui/x-date-pickers/PickersLayout';
import { StaticDatePicker } from '@mui/x-date-pickers/StaticDatePicker';

import { usePickerActionsContext } from '@mui/x-date-pickers/hooks';

function ActionList(props) {
const { onAccept, onClear, onCancel, onSetToday, className } = props;
const { className } = props;
const { clearValue, setValueToToday, acceptValueChanges, cancelValueChanges } =
usePickerActionsContext();

const actions = [
{ text: 'Accept', method: onAccept },
{ text: 'Clear', method: onClear },
{ text: 'Cancel', method: onCancel },
{ text: 'Today', method: onSetToday },
{ text: 'Accept', method: acceptValueChanges },
{ text: 'Clear', method: clearValue },
{ text: 'Cancel', method: cancelValueChanges },
{ text: 'Today', method: setValueToToday },
];

return (
Expand Down
15 changes: 10 additions & 5 deletions docs/data/date-pickers/custom-layout/MovingActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { pickersLayoutClasses } from '@mui/x-date-pickers/PickersLayout';
import { StaticDatePicker } from '@mui/x-date-pickers/StaticDatePicker';
import { PickersActionBarProps } from '@mui/x-date-pickers/PickersActionBar';
import { usePickerActionsContext } from '@mui/x-date-pickers/hooks';

function ActionList(props: PickersActionBarProps) {
const { onAccept, onClear, onCancel, onSetToday, className } = props;
const { className } = props;
const { clearValue, setValueToToday, acceptValueChanges, cancelValueChanges } =
usePickerActionsContext();

const actions = [
{ text: 'Accept', method: onAccept },
{ text: 'Clear', method: onClear },
{ text: 'Cancel', method: onCancel },
{ text: 'Today', method: onSetToday },
{ text: 'Accept', method: acceptValueChanges },
{ text: 'Clear', method: clearValue },
{ text: 'Cancel', method: cancelValueChanges },
{ text: 'Today', method: setValueToToday },
];

return (
// Propagate the className such that CSS selectors can be applied
<List className={className}>
Expand Down
72 changes: 71 additions & 1 deletion docs/data/migration/migration-pickers-v7/migration-pickers-v7.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ This change causes a few breaking changes:
}
```

- The component passed to the `layout` slot no longer receives an `orientation` and the `isLandscape` props, instead you can use the `usePickerContext` hook:
- The component passed to the `layout` slot no longer receives the `orientation` and `isLandscape` props, instead you can use the `usePickerContext` hook:

```diff
-console.log(props.orientation);
Expand All @@ -415,6 +415,51 @@ This change causes a few breaking changes:
+console.log(variant);
```

- 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:

```diff
+import { usePickerActionsContext } from '@mui/x-date-pickers/hooks';

-const { onClear } = props;
+const { clearValue } = usePickerActionsContext();

-const { onSetToday } = props;
+const { setValueToToday } = usePickerActionsContext();

-const { onAccept } = props;
+const { acceptValueChanges } = usePickerActionsContext();

-const { onCancel } = props;
+const { cancelValueChanges } = usePickerActionsContext();

-const { onOpen } = props;
+const { setOpen } = usePickerActionsContext();
+const onOpen = event => {
+ event.preventDefault();
+ setOpen(true);
+}

-props.onClose();
+const { setOpen } = usePickerActionsContext();
+const onClose = event => {
+ event.preventDefault();
+ setOpen(false);
+}

// This contains a small behavior change.
// If the picker is not controlled and has a default value,
// opening it and calling `acceptValueChanges` without any change will call `onAccept` with the default value.
// Whereas before, opening it and calling `onDimiss` without any change would not have called `onAccept`.
-const { onDismiss } = props;
+const { acceptValueChanges } = usePickerActionsContext();
+const onDismiss = acceptValueChanges
```

:::success
The `usePickerContext` also contain all the actions returned by `usePickerActionsContext`.
The only difference is that `usePickerActionsContext` only contains variables with stable references that won't cause a re-render of your component.
:::

### Slot: `toolbar`

- The component passed to the `toolbar` slot no longer receives a `disabled` prop, instead you can use the `usePickerContext` hook:
Expand All @@ -435,6 +480,31 @@ This change causes a few breaking changes:
+console.log(readOnly);
```

### Slot: `actionBar`

- 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:

```diff
+import { usePickerActionsContext } from '@mui/x-date-pickers/hooks';

-const { onClear } = props;
+const { clearValue } = usePickerActionsContext();

-const { onSetToday } = props;
+const { setValueToToday } = usePickerActionsContext();

-const { onAccept } = props;
+const { acceptValueChanges } = usePickerActionsContext();

-const { onCancel } = props;
+const { cancelValueChanges } = usePickerActionsContext();
```

:::success
The `usePickerContext` also contain all the actions returned by `usePickerActionsContext`.
The only difference is that `usePickerActionsContext` only contains variables with stable references that won't cause a re-render of your component.
:::

## Renamed variables and types

The following variables and types have been renamed to have a coherent `Picker` / `Pickers` prefix:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ export const useDesktopRangePicker = <
}

const {
open,
actions,
layoutProps,
providerProps,
renderCurrentView,
Expand Down Expand Up @@ -127,7 +125,8 @@ export const useDesktopRangePicker = <
return;
}

actions.onDismiss();
// This direct access to `providerProps` will go away once the range fields stop having their views in a tooltip.
providerProps.privateContextValue.dismissViews();
});
};

Expand Down Expand Up @@ -170,8 +169,9 @@ export const useDesktopRangePicker = <
>({
variant: 'desktop',
fieldType,
open,
actions,
// These direct access to `providerProps` will go away once the range fields handle the picker opening
open: providerProps.contextValue.open,
setOpen: providerProps.contextValue.setOpen,
readOnly,
disableOpenPicker,
label,
Expand Down Expand Up @@ -215,8 +215,6 @@ export const useDesktopRangePicker = <
containerRef={popperRef}
anchorEl={anchorRef.current}
onBlur={handleBlur}
{...actions}
open={open}
slots={slots}
slotProps={slotProps}
shouldRestoreFocus={shouldRestoreFocus}
Expand Down
Loading

0 comments on commit 9a8aa05

Please sign in to comment.