Skip to content

Commit

Permalink
[pickers] Use context to pass props from the picker to the field (mui…
Browse files Browse the repository at this point in the history
…#16042)

Signed-off-by: Flavien DELANGLE <flaviendelangle@gmail.com>
Co-authored-by: Lukas Tyla <llukas.tyla@gmail.com>
  • Loading branch information
flaviendelangle and LukasTy authored Jan 10, 2025
1 parent 6412b8c commit 9e07e0f
Show file tree
Hide file tree
Showing 57 changed files with 539 additions and 500 deletions.
13 changes: 7 additions & 6 deletions docs/data/date-pickers/calendar-systems/AdapterHijri.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const cacheRtl = createCache({

function ButtonDateTimeField(props) {
const { internalProps, forwardedProps } = useSplitFieldProps(props, 'date');
const { value, timezone, format } = internalProps;
const {
InputProps,
slotProps,
Expand All @@ -37,16 +36,18 @@ function ButtonDateTimeField(props) {
} = forwardedProps;

const pickerContext = usePickerContext();

const parsedFormat = useParsedFormat(internalProps);
const parsedFormat = useParsedFormat();
const { hasValidationError } = useValidation({
validator: validateDate,
value,
timezone,
value: pickerContext.value,
timezone: pickerContext.timezone,
props: internalProps,
});

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

return (
<Button
Expand Down
13 changes: 7 additions & 6 deletions docs/data/date-pickers/calendar-systems/AdapterHijri.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const cacheRtl = createCache({

function ButtonDateTimeField(props: DateTimePickerFieldProps) {
const { internalProps, forwardedProps } = useSplitFieldProps(props, 'date');
const { value, timezone, format } = internalProps;
const {
InputProps,
slotProps,
Expand All @@ -41,16 +40,18 @@ function ButtonDateTimeField(props: DateTimePickerFieldProps) {
} = forwardedProps;

const pickerContext = usePickerContext();

const parsedFormat = useParsedFormat(internalProps);
const parsedFormat = useParsedFormat();
const { hasValidationError } = useValidation({
validator: validateDate,
value,
timezone,
value: pickerContext.value,
timezone: pickerContext.timezone,
props: internalProps,
});

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

return (
<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,12 @@ const BrowserTextField = React.forwardRef((props, ref) => {
const BrowserMultiInputDateRangeField = React.forwardRef((props, ref) => {
const {
slotProps,
value,
format,
onChange,
readOnly,
disabled,
shouldDisableDate,
minDate,
maxDate,
disableFuture,
disablePast,
selectedSections,
onSelectedSectionsChange,
className,
unstableStartFieldRef,
unstableEndFieldRef,
Expand All @@ -111,18 +105,12 @@ const BrowserMultiInputDateRangeField = React.forwardRef((props, ref) => {

const fieldResponse = useMultiInputDateRangeField({
sharedProps: {
value,
format,
onChange,
readOnly,
disabled,
shouldDisableDate,
minDate,
maxDate,
disableFuture,
disablePast,
selectedSections,
onSelectedSectionsChange,
enableAccessibleFieldDOMStructure: true,
},
startTextFieldProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,12 @@ const BrowserMultiInputDateRangeField = React.forwardRef(
(props: BrowserMultiInputDateRangeFieldProps, ref: React.Ref<HTMLDivElement>) => {
const {
slotProps,
value,
format,
onChange,
readOnly,
disabled,
shouldDisableDate,
minDate,
maxDate,
disableFuture,
disablePast,
selectedSections,
onSelectedSectionsChange,
className,
unstableStartFieldRef,
unstableEndFieldRef,
Expand All @@ -150,18 +144,12 @@ const BrowserMultiInputDateRangeField = React.forwardRef(
MultiInputFieldSlotTextFieldProps
>({
sharedProps: {
value,
format,
onChange,
readOnly,
disabled,
shouldDisableDate,
minDate,
maxDate,
disableFuture,
disablePast,
selectedSections,
onSelectedSectionsChange,
enableAccessibleFieldDOMStructure: true,
},
startTextFieldProps,
Expand Down
6 changes: 2 additions & 4 deletions docs/data/date-pickers/custom-field/JoyV6Field.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import { unstable_useDateField as useDateField } from '@mui/x-date-pickers/DateF
const joyTheme = extendJoyTheme();

const JoyDateField = React.forwardRef((props, ref) => {
const fieldResponse = useDateField({
...props,
enableAccessibleFieldDOMStructure: false,
});
const fieldResponse = useDateField(props);

const {
// Should be ignored
Expand Down Expand Up @@ -64,6 +61,7 @@ const JoyDatePicker = React.forwardRef((props, ref) => {
<DatePicker
ref={ref}
{...props}
enableAccessibleFieldDOMStructure={false}
slots={{ ...props.slots, field: JoyDateField }}
/>
);
Expand Down
6 changes: 2 additions & 4 deletions docs/data/date-pickers/custom-field/JoyV6Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ const joyTheme = extendJoyTheme();

const JoyDateField = React.forwardRef(
(props: DatePickerFieldProps<false>, ref: React.Ref<HTMLDivElement>) => {
const fieldResponse = useDateField<false, typeof props>({
...props,
enableAccessibleFieldDOMStructure: false,
});
const fieldResponse = useDateField<false, typeof props>(props);

const {
// Should be ignored
Expand Down Expand Up @@ -73,6 +70,7 @@ const JoyDatePicker = React.forwardRef(
<DatePicker
ref={ref}
{...props}
enableAccessibleFieldDOMStructure={false}
slots={{ ...props.slots, field: JoyDateField }}
/>
);
Expand Down
13 changes: 1 addition & 12 deletions docs/data/date-pickers/custom-field/JoyV6MultiInputRangeField.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,12 @@ const MultiInputJoyDateRangeFieldSeparator = styled(
const JoyMultiInputDateRangeField = React.forwardRef((props, ref) => {
const {
slotProps,
value,
format,
onChange,
readOnly,
disabled,
shouldDisableDate,
minDate,
maxDate,
disableFuture,
disablePast,
selectedSections,
onSelectedSectionsChange,
className,
unstableStartFieldRef,
unstableEndFieldRef,
Expand All @@ -135,18 +129,12 @@ const JoyMultiInputDateRangeField = React.forwardRef((props, ref) => {

const fieldResponse = useMultiInputDateRangeField({
sharedProps: {
value,
format,
onChange,
readOnly,
disabled,
shouldDisableDate,
minDate,
maxDate,
disableFuture,
disablePast,
selectedSections,
onSelectedSectionsChange,
enableAccessibleFieldDOMStructure: false,
},
startTextFieldProps,
Expand All @@ -169,6 +157,7 @@ const JoyDateRangePicker = React.forwardRef((props, ref) => {
<DateRangePicker
ref={ref}
{...props}
enableAccessibleFieldDOMStructure={false}
slots={{ ...props?.slots, field: JoyMultiInputDateRangeField }}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,12 @@ const JoyMultiInputDateRangeField = React.forwardRef(
(props: JoyMultiInputDateRangeFieldProps, ref: React.Ref<HTMLDivElement>) => {
const {
slotProps,
value,
format,
onChange,
readOnly,
disabled,
shouldDisableDate,
minDate,
maxDate,
disableFuture,
disablePast,
selectedSections,
onSelectedSectionsChange,
className,
unstableStartFieldRef,
unstableEndFieldRef,
Expand All @@ -176,18 +170,12 @@ const JoyMultiInputDateRangeField = React.forwardRef(
MultiInputFieldSlotTextFieldProps
>({
sharedProps: {
value,
format,
onChange,
readOnly,
disabled,
shouldDisableDate,
minDate,
maxDate,
disableFuture,
disablePast,
selectedSections,
onSelectedSectionsChange,
enableAccessibleFieldDOMStructure: false,
},
startTextFieldProps,
Expand All @@ -212,6 +200,7 @@ const JoyDateRangePicker = React.forwardRef(
<DateRangePicker
ref={ref}
{...props}
enableAccessibleFieldDOMStructure={false}
slots={{ ...props?.slots, field: JoyMultiInputDateRangeField }}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ export const DateRangeIcon = createSvgIcon(
const joyTheme = extendJoyTheme();

const JoySingleInputDateRangeField = React.forwardRef((props, ref) => {
const fieldResponse = useSingleInputDateRangeField({
...props,
enableAccessibleFieldDOMStructure: false,
});
const fieldResponse = useSingleInputDateRangeField(props);

const {
// Should be ignored
Expand Down Expand Up @@ -78,6 +75,7 @@ const JoySingleInputDateRangePicker = React.forwardRef((props, ref) => {
<DateRangePicker
{...props}
ref={ref}
enableAccessibleFieldDOMStructure={false}
slots={{ ...props.slots, field: JoySingleInputDateRangeField }}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ type JoySingleInputDateRangeFieldComponent = ((

const JoySingleInputDateRangeField = React.forwardRef(
(props: DateRangePickerFieldProps<false>, ref: React.Ref<HTMLDivElement>) => {
const fieldResponse = useSingleInputDateRangeField<false, typeof props>({
...props,
enableAccessibleFieldDOMStructure: false,
});
const fieldResponse = useSingleInputDateRangeField<false, typeof props>(props);

const {
// Should be ignored
Expand Down Expand Up @@ -92,6 +89,7 @@ const JoySingleInputDateRangePicker = React.forwardRef(
<DateRangePicker
{...props}
ref={ref}
enableAccessibleFieldDOMStructure={false}
slots={{ ...props.slots, field: JoySingleInputDateRangeField }}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import Stack from '@mui/material/Stack';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DatePicker } from '@mui/x-date-pickers/DatePicker';
import { useSplitFieldProps } from '@mui/x-date-pickers/hooks';
import { usePickerContext, useSplitFieldProps } from '@mui/x-date-pickers/hooks';
import { useValidation, validateDate } from '@mui/x-date-pickers/validation';

function AutocompleteField(props) {
const { internalProps, forwardedProps } = useSplitFieldProps(props, 'date');
const { value, timezone, onChange } = internalProps;
const { forwardedProps, internalProps } = useSplitFieldProps(props, 'date');
const { timezone, value, setValue } = usePickerContext();
const {
InputProps,
slotProps,
Expand Down Expand Up @@ -85,7 +85,7 @@ function AutocompleteField(props) {
}}
value={value}
onChange={(_, newValue) => {
onChange(newValue, {
setValue(newValue, {
validationError: getValidationErrorForNewValue(newValue),
});
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
DatePickerFieldProps,
DatePickerProps,
} from '@mui/x-date-pickers/DatePicker';
import { useSplitFieldProps } from '@mui/x-date-pickers/hooks';
import { usePickerContext, useSplitFieldProps } from '@mui/x-date-pickers/hooks';
import { useValidation, validateDate } from '@mui/x-date-pickers/validation';

interface AutocompleteFieldProps extends DatePickerFieldProps {
Expand All @@ -21,8 +21,8 @@ interface AutocompleteFieldProps extends DatePickerFieldProps {
}

function AutocompleteField(props: AutocompleteFieldProps) {
const { internalProps, forwardedProps } = useSplitFieldProps(props, 'date');
const { value, timezone, onChange } = internalProps;
const { forwardedProps, internalProps } = useSplitFieldProps(props, 'date');
const { timezone, value, setValue } = usePickerContext();
const {
InputProps,
slotProps,
Expand Down Expand Up @@ -96,7 +96,7 @@ function AutocompleteField(props: AutocompleteFieldProps) {
}}
value={value}
onChange={(_, newValue) => {
onChange(newValue, {
setValue(newValue, {
validationError: getValidationErrorForNewValue(newValue),
});
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {

function ButtonDateField(props) {
const { internalProps, forwardedProps } = useSplitFieldProps(props, 'date');
const { value, timezone, format } = internalProps;
const {
InputProps,
slotProps,
Expand All @@ -25,16 +24,18 @@ function ButtonDateField(props) {
} = forwardedProps;

const pickerContext = usePickerContext();

const parsedFormat = useParsedFormat(internalProps);
const parsedFormat = useParsedFormat();
const { hasValidationError } = useValidation({
validator: validateDate,
value,
timezone,
value: pickerContext.value,
timezone: pickerContext.timezone,
props: internalProps,
});

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

return (
<Button
Expand Down
Loading

0 comments on commit 9e07e0f

Please sign in to comment.