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] Use props.referenceDate timezone when props.value and props.defaultValue are not defined (@flaviendelangle) #15544

Merged
merged 4 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ const DateRangeCalendar = React.forwardRef(function DateRangeCalendar<
name: 'DateRangeCalendar',
timezone: timezoneProp,
value: valueProp,
referenceDate,
defaultValue,
onChange,
valueManager: rangeValueManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const useMultiInputDateRangeField = <
const {
value: valueProp,
defaultValue,
referenceDate,
format,
formatDensity,
shouldRespectLeadingZeros,
Expand All @@ -65,6 +66,7 @@ export const useMultiInputDateRangeField = <
timezone: timezoneProp,
value: valueProp,
defaultValue,
referenceDate,
onChange,
valueManager: rangeValueManager,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const useMultiInputDateTimeRangeField = <
const {
value: valueProp,
defaultValue,
referenceDate,
format,
formatDensity,
shouldRespectLeadingZeros,
Expand All @@ -65,6 +66,7 @@ export const useMultiInputDateTimeRangeField = <
timezone: timezoneProp,
value: valueProp,
defaultValue,
referenceDate,
onChange,
valueManager: rangeValueManager,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const useMultiInputTimeRangeField = <
const {
value: valueProp,
defaultValue,
referenceDate,
format,
formatDensity,
shouldRespectLeadingZeros,
Expand All @@ -67,6 +68,7 @@ export const useMultiInputTimeRangeField = <
defaultValue,
onChange,
valueManager: rangeValueManager,
referenceDate,
});

const { validationError, getValidationErrorForNewValue } = useValidation({
Expand Down
1 change: 1 addition & 0 deletions packages/x-date-pickers/src/DateCalendar/DateCalendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export const DateCalendar = React.forwardRef(function DateCalendar<TDate extends
timezone: timezoneProp,
value: valueProp,
defaultValue,
referenceDate: referenceDateProp,
onChange,
valueManager: singleItemValueManager,
});
Expand Down
1 change: 1 addition & 0 deletions packages/x-date-pickers/src/DigitalClock/DigitalClock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export const DigitalClock = React.forwardRef(function DigitalClock<TDate extends
timezone: timezoneProp,
value: valueProp,
defaultValue,
referenceDate: referenceDateProp,
onChange,
valueManager: singleItemValueManager,
});
Expand Down
3 changes: 2 additions & 1 deletion packages/x-date-pickers/src/MonthCalendar/MonthCalendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ export const MonthCalendar = React.forwardRef(function MonthCalendar<TDate exten
timezone: timezoneProp,
value: valueProp,
defaultValue,
onChange: onChange as (value: TDate | null) => void,
referenceDate: referenceDateProp,
onChange,
valueManager: singleItemValueManager,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export const MultiSectionDigitalClock = React.forwardRef(function MultiSectionDi
timezone: timezoneProp,
value: valueProp,
defaultValue,
referenceDate: referenceDateProp,
onChange,
valueManager: singleItemValueManager,
});
Expand Down
1 change: 1 addition & 0 deletions packages/x-date-pickers/src/TimeClock/TimeClock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export const TimeClock = React.forwardRef(function TimeClock<TDate extends Picke
timezone: timezoneProp,
value: valueProp,
defaultValue,
referenceDate: referenceDateProp,
onChange,
valueManager: singleItemValueManager,
});
Expand Down
3 changes: 2 additions & 1 deletion packages/x-date-pickers/src/YearCalendar/YearCalendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ export const YearCalendar = React.forwardRef(function YearCalendar<TDate extends
timezone: timezoneProp,
value: valueProp,
defaultValue,
onChange: onChange as (value: TDate | null) => void,
referenceDate: referenceDateProp,
onChange,
valueManager: singleItemValueManager,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export const useFieldState = <
timezone: timezoneProp,
value: valueProp,
defaultValue,
referenceDate: referenceDateProp,
onChange,
valueManager,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export const usePickerValue = <
defaultValue: inDefaultValue,
closeOnSelect = wrapperVariant === 'desktop',
timezone: timezoneProp,
referenceDate,
} = props;

const { current: defaultValue } = React.useRef(inDefaultValue);
Expand Down Expand Up @@ -225,6 +226,7 @@ export const usePickerValue = <
timezone: timezoneProp,
value: inValueWithoutRenderTimezone,
defaultValue,
referenceDate,
onChange,
valueManager,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,10 @@ export interface UsePickerValueNonStaticProps {
export interface UsePickerValueProps<TValue, TError>
extends UsePickerValueBaseProps<TValue, TError>,
UsePickerValueNonStaticProps,
TimezoneProps {}
TimezoneProps {
// We don't add JSDoc here because we want the `referenceDate` JSDoc to be the one from the view which has more context.
referenceDate?: PickerValidDate;
}

export interface UsePickerValueParams<
TValue,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import * as React from 'react';
import { expect } from 'chai';
import { screen } from '@mui/internal-test-utils';
import { PickersTimezone, PickerValidDate } from '@mui/x-date-pickers/models';
import { createPickerRenderer } from 'test/utils/pickers';
import { useValueWithTimezone } from './useValueWithTimezone';
import { singleItemValueManager } from '../utils/valueManagers';

describe('useValueWithTimezone', () => {
const { render, adapter } = createPickerRenderer({
clock: 'fake',
adapterName: 'luxon',
});

function runTest(params: {
timezone: PickersTimezone | undefined;
value: PickerValidDate | null | undefined;
defaultValue: PickerValidDate | null | undefined;
referenceDate: PickerValidDate | undefined;
expectedTimezone: PickersTimezone;
}) {
const { expectedTimezone, ...other } = params;

function TestComponent(props: typeof other) {
const { timezone } = useValueWithTimezone({
...props,
valueManager: singleItemValueManager,
onChange: () => {},
});

return <div data-testid="result">{timezone}</div>;
}

render(<TestComponent {...other} />);

expect(screen.getByTestId('result').textContent).to.equal(expectedTimezone);
}

it('should use the timezone parameter when provided', () => {
runTest({
timezone: 'America/New_York',
value: undefined,
defaultValue: undefined,
referenceDate: undefined,
expectedTimezone: 'America/New_York',
});
});

it('should use the timezone parameter over the value parameter when both are provided', () => {
runTest({
timezone: 'America/New_York',
value: adapter.date(undefined, 'Europe/Paris'),
defaultValue: undefined,
referenceDate: undefined,
expectedTimezone: 'America/New_York',
});
});

it('should use the value parameter when provided', () => {
runTest({
timezone: undefined,
value: adapter.date(undefined, 'America/New_York'),
defaultValue: undefined,
referenceDate: undefined,
expectedTimezone: 'America/New_York',
});
});

it('should use the value parameter over the defaultValue parameter when both are provided', () => {
runTest({
timezone: undefined,
value: adapter.date(undefined, 'America/New_York'),
defaultValue: adapter.date(undefined, 'Europe/Paris'),
referenceDate: undefined,
expectedTimezone: 'America/New_York',
});
});

it('should use the defaultValue parameter when provided', () => {
runTest({
timezone: undefined,
value: undefined,
defaultValue: adapter.date(undefined, 'America/New_York'),
referenceDate: undefined,
expectedTimezone: 'America/New_York',
});
});

it('should use the referenceDate parameter when provided', () => {
runTest({
timezone: undefined,
value: undefined,
defaultValue: undefined,
referenceDate: adapter.date(undefined, 'America/New_York'),
expectedTimezone: 'America/New_York',
});
});

it('should use the "default" timezone is there is no way to deduce the user timezone', () => {
runTest({
timezone: undefined,
value: undefined,
defaultValue: undefined,
referenceDate: undefined,
expectedTimezone: 'default',
});
});
});
54 changes: 37 additions & 17 deletions packages/x-date-pickers/src/internals/hooks/useValueWithTimezone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@ export const useValueWithTimezone = <
timezone: timezoneProp,
value: valueProp,
defaultValue,
referenceDate,
onChange,
valueManager,
}: {
timezone: PickersTimezone | undefined;
value: TValue | undefined;
defaultValue: TValue | undefined;
onChange: TChange | undefined;
valueManager: PickerValueManager<TValue, TDate, any>;
}) => {
const utils = useUtils<TDate>();
}: UseValueWithTimezoneParameters<TValue, TChange>) => {
const utils = useUtils();

const firstDefaultValue = React.useRef(defaultValue);
const inputValue = valueProp ?? firstDefaultValue.current ?? valueManager.emptyValue;
Expand All @@ -45,7 +40,16 @@ export const useValueWithTimezone = <
return valueManager.setTimezone(utils, inputTimezone, newValue);
});

const timezoneToRender = timezoneProp ?? inputTimezone ?? 'default';
let timezoneToRender: PickersTimezone;
if (timezoneProp) {
timezoneToRender = timezoneProp;
} else if (inputTimezone) {
timezoneToRender = inputTimezone;
} else if (referenceDate) {
timezoneToRender = utils.getTimezone(referenceDate);
} else {
timezoneToRender = 'default';
}

const valueWithTimezoneToRender = React.useMemo(
() => valueManager.setTimezone(utils, timezoneToRender, inputValue),
Expand All @@ -72,16 +76,10 @@ export const useControlledValueWithTimezone = <
timezone: timezoneProp,
value: valueProp,
defaultValue,
referenceDate,
onChange: onChangeProp,
valueManager,
}: {
name: string;
timezone: PickersTimezone | undefined;
value: TValue | undefined;
defaultValue: TValue | undefined;
onChange: TChange | undefined;
valueManager: PickerValueManager<TValue, TDate, any>;
}) => {
}: UseControlledValueWithTimezoneParameters<TValue, TChange>) => {
const [valueWithInputTimezone, setValue] = useControlled({
name,
state: 'value',
Expand All @@ -98,7 +96,29 @@ export const useControlledValueWithTimezone = <
timezone: timezoneProp,
value: valueWithInputTimezone,
defaultValue: undefined,
referenceDate,
onChange,
valueManager,
});
};

interface UseValueWithTimezoneParameters<TValue, TChange extends (...params: any[]) => void> {
timezone: PickersTimezone | undefined;
value: TValue | undefined;
defaultValue: TValue | undefined;
/**
* The reference date as passed to `props.referenceDate`.
* It does not need to have its default value.
* This is only used to determine the timezone to use when `props.value` and `props.defaultValue` are not defined.
*/
referenceDate: PickerValidDate | undefined;
onChange: TChange | undefined;
valueManager: PickerValueManager<TValue, any>;
}

interface UseControlledValueWithTimezoneParameters<
TValue,
TChange extends (...params: any[]) => void,
> extends UseValueWithTimezoneParameters<TValue, TChange> {
name: string;
}