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] Replace the onOpen() and onClose() methods of usePickerContext() with a single setOpen() method #15701

Merged
merged 10 commits into from
Dec 4, 2024
Merged
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;
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved
/**
* `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) => {
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