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] Add new method onToggleOpening to usePickerContext() #15701

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 1 addition & 9 deletions docs/data/date-pickers/calendar-systems/AdapterHijri.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ function ButtonDateTimeField(props) {
props: internalProps,
});

const handleTogglePicker = (event) => {
if (pickerContext.open) {
pickerContext.onClose(event);
} else {
pickerContext.onOpen(event);
}
};

const valueStr = value == null ? parsedFormat : value.format(format);

return (
Expand All @@ -62,7 +54,7 @@ function ButtonDateTimeField(props) {
variant="outlined"
color={hasValidationError ? 'error' : 'primary'}
ref={InputProps?.ref}
onClick={handleTogglePicker}
onClick={pickerContext.onToggleOpening}
>
{label ? `${label}: ${valueStr}` : valueStr}
</Button>
Expand Down
10 changes: 1 addition & 9 deletions docs/data/date-pickers/calendar-systems/AdapterHijri.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,6 @@ function ButtonDateTimeField(props: DateTimePickerFieldProps) {
props: internalProps,
});

const handleTogglePicker = (event: React.UIEvent) => {
if (pickerContext.open) {
pickerContext.onClose(event);
} else {
pickerContext.onOpen(event);
}
};

const valueStr = value == null ? parsedFormat : value.format(format);

return (
Expand All @@ -66,7 +58,7 @@ function ButtonDateTimeField(props: DateTimePickerFieldProps) {
variant="outlined"
color={hasValidationError ? 'error' : 'primary'}
ref={InputProps?.ref}
onClick={handleTogglePicker}
onClick={pickerContext.onToggleOpening}
>
{label ? `${label}: ${valueStr}` : valueStr}
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,6 @@ const BrowserSingleInputDateRangeField = React.forwardRef((props, ref) => {
const { slots, slotProps, ...other } = props;

const pickerContext = usePickerContext();
const handleTogglePicker = (event) => {
if (pickerContext.open) {
pickerContext.onClose(event);
} else {
pickerContext.onOpen(event);
}
};

const textFieldProps = useSlotProps({
elementType: 'input',
Expand All @@ -106,7 +99,7 @@ const BrowserSingleInputDateRangeField = React.forwardRef((props, ref) => {
...textFieldProps.InputProps,
endAdornment: (
<InputAdornment position="end">
<IconButton onClick={handleTogglePicker}>
<IconButton onClick={pickerContext.onToggleOpening}>
<DateRangeIcon />
</IconButton>
</InputAdornment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,6 @@ const BrowserSingleInputDateRangeField = React.forwardRef(
const { slots, slotProps, ...other } = props;

const pickerContext = usePickerContext();
const handleTogglePicker = (event: React.UIEvent) => {
if (pickerContext.open) {
pickerContext.onClose(event);
} else {
pickerContext.onOpen(event);
}
};

const textFieldProps: typeof props = useSlotProps({
elementType: 'input',
Expand All @@ -134,7 +127,7 @@ const BrowserSingleInputDateRangeField = React.forwardRef(
...textFieldProps.InputProps,
endAdornment: (
<InputAdornment position="end">
<IconButton onClick={handleTogglePicker}>
<IconButton onClick={pickerContext.onToggleOpening}>
<DateRangeIcon />
</IconButton>
</InputAdornment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,6 @@ const JoySingleInputDateRangeField = React.forwardRef((props, ref) => {
const { slots, slotProps, ...other } = props;

const pickerContext = usePickerContext();
const handleTogglePicker = (event) => {
if (pickerContext.open) {
pickerContext.onClose(event);
} else {
pickerContext.onOpen(event);
}
};

const textFieldProps = useSlotProps({
elementType: FormControl,
Expand All @@ -105,7 +98,7 @@ const JoySingleInputDateRangeField = React.forwardRef((props, ref) => {
ref={ref}
endDecorator={
<IconButton
onClick={handleTogglePicker}
onClick={pickerContext.onToggleOpening}
variant="plain"
color="neutral"
sx={{ marginLeft: 2.5 }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,6 @@ const JoySingleInputDateRangeField = React.forwardRef(
const { slots, slotProps, ...other } = props;

const pickerContext = usePickerContext();
const handleTogglePicker = (event: React.UIEvent) => {
if (pickerContext.open) {
pickerContext.onClose(event);
} else {
pickerContext.onOpen(event);
}
};

const textFieldProps: JoySingleInputDateRangeFieldProps = useSlotProps({
elementType: FormControl,
Expand All @@ -136,7 +129,7 @@ const JoySingleInputDateRangeField = React.forwardRef(
ref={ref}
endDecorator={
<IconButton
onClick={handleTogglePicker}
onClick={pickerContext.onToggleOpening}
variant="plain"
color="neutral"
sx={{ marginLeft: 2.5 }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ function ButtonDateField(props) {
props: internalProps,
});

const handleTogglePicker = (event) => {
if (pickerContext.open) {
pickerContext.onClose(event);
} else {
pickerContext.onOpen(event);
}
};

const valueStr = value == null ? parsedFormat : value.format(format);

return (
Expand All @@ -50,7 +42,7 @@ function ButtonDateField(props) {
variant="outlined"
color={hasValidationError ? 'error' : 'primary'}
ref={InputProps?.ref}
onClick={handleTogglePicker}
onClick={pickerContext.onToggleOpening}
>
{label ? `${label}: ${valueStr}` : valueStr}
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ function ButtonDateField(props: DatePickerFieldProps) {
props: internalProps,
});

const handleTogglePicker = (event: React.UIEvent) => {
if (pickerContext.open) {
pickerContext.onClose(event);
} else {
pickerContext.onOpen(event);
}
};

const valueStr = value == null ? parsedFormat : value.format(format);

return (
Expand All @@ -54,7 +46,7 @@ function ButtonDateField(props: DatePickerFieldProps) {
variant="outlined"
color={hasValidationError ? 'error' : 'primary'}
ref={InputProps?.ref}
onClick={handleTogglePicker}
onClick={pickerContext.onToggleOpening}
>
{label ? `${label}: ${valueStr}` : valueStr}
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ function ButtonDateRangeField(props) {
props: internalProps,
});

const handleTogglePicker = (event) => {
if (pickerContext.open) {
pickerContext.onClose(event);
} else {
pickerContext.onOpen(event);
}
};

const formattedValue = (value ?? [null, null])
.map((date) => (date == null ? parsedFormat : date.format(format)))
.join(' – ');
Expand All @@ -54,7 +46,7 @@ function ButtonDateRangeField(props) {
variant="outlined"
color={hasValidationError ? 'error' : 'primary'}
ref={InputProps?.ref}
onClick={handleTogglePicker}
onClick={pickerContext.onToggleOpening}
>
{label ? `${label}: ${formattedValue}` : formattedValue}
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ function ButtonDateRangeField(props: DateRangePickerFieldProps) {
props: internalProps,
});

const handleTogglePicker = (event: React.UIEvent) => {
if (pickerContext.open) {
pickerContext.onClose(event);
} else {
pickerContext.onOpen(event);
}
};

const formattedValue = (value ?? [null, null])
.map((date: Dayjs) => (date == null ? parsedFormat : date.format(format)))
.join(' – ');
Expand All @@ -58,7 +50,7 @@ function ButtonDateRangeField(props: DateRangePickerFieldProps) {
variant="outlined"
color={hasValidationError ? 'error' : 'primary'}
ref={InputProps?.ref}
onClick={handleTogglePicker}
onClick={pickerContext.onToggleOpening}
>
{label ? `${label}: ${formattedValue}` : formattedValue}
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ function ReadOnlyDateField(props) {
props: internalProps,
});

const handleTogglePicker = (event) => {
if (pickerContext.open) {
pickerContext.onClose(event);
} else {
pickerContext.onOpen(event);
}
};

return (
<TextField
{...other}
Expand All @@ -46,7 +38,7 @@ function ReadOnlyDateField(props) {
sx: { cursor: 'pointer', '& *': { cursor: 'inherit' } },
}}
error={hasValidationError}
onClick={handleTogglePicker}
onClick={pickerContext.onToggleOpening}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ function ReadOnlyDateField(props: DatePickerFieldProps) {
props: internalProps,
});

const handleTogglePicker = (event: React.UIEvent) => {
if (pickerContext.open) {
pickerContext.onClose(event);
} else {
pickerContext.onOpen(event);
}
};

return (
<TextField
{...other}
Expand All @@ -50,7 +42,7 @@ function ReadOnlyDateField(props: DatePickerFieldProps) {
sx: { cursor: 'pointer', '& *': { cursor: 'inherit' } },
}}
error={hasValidationError}
onClick={handleTogglePicker}
onClick={pickerContext.onToggleOpening}
/>
);
}
Expand Down
10 changes: 1 addition & 9 deletions docs/src/modules/components/overview/mainDemo/PickerButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ function ButtonDateField(props: DatePickerFieldProps) {
props: internalProps,
});

const handleTogglePicker = (event: React.UIEvent) => {
if (pickerContext.open) {
pickerContext.onClose(event);
} else {
pickerContext.onOpen(event);
}
};

const valueStr = value == null ? parsedFormat : value.format(format);

return (
Expand All @@ -43,7 +35,7 @@ function ButtonDateField(props: DatePickerFieldProps) {
fullWidth
color={hasValidationError ? 'error' : 'primary'}
ref={InputProps?.ref}
onClick={handleTogglePicker}
onClick={pickerContext.onToggleOpening}
>
{label ? `${label}: ${valueStr}` : valueStr}
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ export interface PickerContextValue {
* @param {React.UIEvent} event The DOM event that triggered the change.
*/
onClose: (event: React.UIEvent) => void;
/**
* Close the picker if it's open, open it if it's closed.
* @param {React.UIEvent} event The DOM event that triggered the change.
*/
onToggleOpening: (event: React.UIEvent) => void;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about naming the method (renaming onOpen and onClose accordingly) a bit differently to have a usual onClick={handleClick} scenario? 🤔

  • handleOpenToggle
  • handleToggleOpen
  • toggleOpenState
  • togglePickerOpen

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onOpen and onClose are present in v7 so it's a small BC.
But if you think it's cleaner I'm fine doing it.

I'm not a fan of the handleXXX when they are provided by a hook (or as a prop).
I use them when creating variables like:

const handleTogglePicker = () => { ... }

return <button onClick={handleTogglePicker}>Open me</button>

But when it comes from a button it feels off to me:

const { handleTogglePicker } = usePickerFakeContext();

return <button onClick={handleTogglePicker}>Open me</button>

Not sure why 😆
Maybe because handleXXX is a nomenclature used only for event handler and nothing prevents the user to use the methods from the hook in something else (in an effect, when a server request loads etc...).

For this kind of hook, if we don't want to onXXX, I would take to prefer a naming like openPicker, closePicker and togglePickerOpening for example.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great insights, especially regarding the possibility of this function being called in other places than the even handler. 👌

onOpen and onClose are present in v7 so it's a small BC.

They are and didn't even cross my eyes until you started working on this change, then it became a bit more clear, that we can improve clarity here. 🙈
But they are present in the valueResponse.
Couldn't we update the naming only in the context? 🤔

I would not mind having openPicker, closePicker, and togglePickerOpen(ing) (the opening word here sounds slightly strange 🙈 🤷 ).
P.S. Have we explored going for setPickerOpen instead of onOpen and onClose?
Noticed such an approch here:
https://github.com/adobe/react-spectrum/blob/e94e36431149b6d3e9ed5900de30b538c0110584/packages/%40react-stately/datepicker/src/useDatePickerState.ts#L55-L56

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we update the naming only in the context? 🤔

But the context was introduced in a later v7 release I think 😬
So renaming them is a BC
But probably almost nobody is using it for now, if we want to break it, let's do it before we document it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P.S. Have we explored going for setPickerOpen instead of onOpen and onClose?

That's yet another nomenclature indeed.

Copy link
Member Author

@flaviendelangle flaviendelangle Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about the following:

  • setOpen
  • openPicker
  • closePicker

We keep the 3 methods for maximal flexibility, we don't have any "handle" or "on" prefix.
And IMHO setOpen is cleared that it's like a setState that takes the new state, setPickerOpen is slightly less clear to me (I would expect it to just open the picker).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the context was introduced in a later v7 release I think 😬
So renaming them is a BC

Oh shoot, I forgot that we introduced it in v7 as well. 🙈 🥷
Yeah, I'm all for refining the naming and not documenting too much on v7.
Maybe we can also skimp on having a codemod for the renaming. 😆

  • setOpen
  • openPicker
  • closePicker

Didn't we plan to add a togglePickerOpen function to ease the simple use-case examples?
I had in mind if we considered having setOpen instead of openPicker and closePicker combination. 🤔

Copy link
Member Author

@flaviendelangle flaviendelangle Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, I got mixed up with my needs on the composition API here...

So if we start from the beginning, to modify the opening state of the picker, there are 4 potential methods:

  1. Open
  2. Closes
  3. Toggles
  4. Sets the provided value

Right now the context has 1. and 2.
I'm proposing to add 3. because it makes all the demos of custom fields easier to read.
And we will need 4. in the composition API because most modal / popover component take a prop like onOpenChange (Base UI and React Aria both do).

I had in mind if we considered having setOpen instead of openPicker and closePicker combination. 🤔

So if I understand your comment correctly, you are proposing having 3 and 4 and dropping 1 and 2.
In which case maybe toggleOpen and setOpen would be good names.

/**
* `true` if the picker is open, `false` otherwise.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import useEnhancedEffect from '@mui/utils/useEnhancedEffect';
import useEventCallback from '@mui/utils/useEventCallback';
import { PickerOwnerState } from '../../../models';
import { PickerValueManager, UsePickerValueResponse } from './usePickerValue.types';
import {
Expand Down Expand Up @@ -67,6 +68,14 @@ export function usePickerProvider<TValue extends PickerValidValue>(
const utils = useUtils();
const orientation = usePickerOrientation(views, props.orientation);

const handleTogglePicker = useEventCallback((event: React.UIEvent) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be an action exposed by usePickerValue but it has more implications (all actions are forwarded to PickersPopper for now.
Until the actions are always accessed through the state, I propose to keep it here to avoid problems.

if (pickerValueResponse.open) {
pickerValueResponse.actions.onClose(event);
} else {
pickerValueResponse.actions.onOpen(event);
}
});

const ownerState = React.useMemo<PickerOwnerState>(
() => ({
isPickerValueEmpty: valueManager.areValuesEqual(
Expand Down Expand Up @@ -96,6 +105,7 @@ export function usePickerProvider<TValue extends PickerValidValue>(
() => ({
onOpen: pickerValueResponse.actions.onOpen,
onClose: pickerValueResponse.actions.onClose,
onToggleOpening: handleTogglePicker,
open: pickerValueResponse.open,
disabled: props.disabled ?? false,
readOnly: props.readOnly ?? false,
Expand All @@ -105,6 +115,7 @@ export function usePickerProvider<TValue extends PickerValidValue>(
[
pickerValueResponse.actions.onOpen,
pickerValueResponse.actions.onClose,
handleTogglePicker,
pickerValueResponse.open,
variant,
orientation,
Expand Down
Loading