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

[datetime2] fix(DateInput2): allow changing tz before selecting date #6016

Merged
merged 1 commit into from
Mar 13, 2023
Merged
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
4 changes: 2 additions & 2 deletions packages/datetime2/src/components/date-input2/dateInput2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ export const DateInput2: React.FC<DateInput2Props> = React.memo(function _DateIn

const handleTimezoneChange = React.useCallback(
(newTimezone: string) => {
if (valueAsDate !== null) {
setTimezoneValue(newTimezone);
setTimezoneValue(newTimezone);
if (valueAsDate != null) {
const newDateString = getIsoEquivalentWithUpdatedTimezone(valueAsDate, newTimezone, timePrecision);
props.onChange?.(newDateString, true);
}
Expand Down
80 changes: 54 additions & 26 deletions packages/datetime2/test/components/dateInput2Tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const DEFAULT_PROPS = {
popoverProps: {
usePortal: false,
},
showTimezoneSelect: true,
timePrecision: TimePrecision.SECOND,
};

Expand Down Expand Up @@ -468,6 +469,23 @@ describe("<DateInput2>", () => {
assert.isTrue(onChange.calledOnce);
assert.isTrue(isEqual(parseISO(onChange.firstCall.args[0]), DATE));
});

describe("allows changing timezone", () => {
it("before selecting a date", () => {
const wrapper = mount(<DateInput2 {...DEFAULT_PROPS} />, { attachTo: containerElement });
focusInput(wrapper);
clickTimezoneItem(wrapper, "Paris");
assertTimezoneIsSelected(wrapper, "GMT+1");
});

it("after selecting a date", () => {
const wrapper = mount(<DateInput2 {...DEFAULT_PROPS} />, { attachTo: containerElement });
focusInput(wrapper);
clickCalendarDay(wrapper, 1);
clickTimezoneItem(wrapper, "Paris");
assertTimezoneIsSelected(wrapper, "GMT+1");
});
});
});

describe("controlled usage", () => {
Expand All @@ -482,32 +500,6 @@ describe("<DateInput2>", () => {
assert.doesNotThrow(() => mount(<DateInput2 {...DEFAULT_PROPS_CONTROLLED} value={null} />));
});

describe("when changing timezone", () => {
it("calls onChange with the updated ISO string", () => {
const wrapper = mount(<DateInput2 {...DEFAULT_PROPS_CONTROLLED} />);
clickTimezoneItem(wrapper, "Paris");
assert.isTrue(onChange.calledOnce);
assert.strictEqual(onChange.firstCall.args[0], "2021-11-29T10:30:00+01:00");
});

it("formats the returned ISO string according to timePrecision", () => {
const wrapper = mount(
<DateInput2 {...DEFAULT_PROPS_CONTROLLED} timePrecision={TimePrecision.MINUTE} />,
);
clickTimezoneItem(wrapper, "Paris");
assert.isTrue(onChange.calledOnce);
assert.strictEqual(onChange.firstCall.args[0], "2021-11-29T10:30+01:00");
});

it("updates the displayed timezone", () => {
const wrapper = mount(<DateInput2 {...DEFAULT_PROPS_CONTROLLED} />);
clickTimezoneItem(wrapper, "New York");
const tzTag = wrapper.find(Tag);
// date-fns does not know about Daylight Saving Time in Node.js
assert.strictEqual(tzTag.text(), "EST");
});
});

it("changing the time calls onChange with the updated ISO string", () => {
const wrapper = mount(<DateInput2 {...DEFAULT_PROPS_CONTROLLED} />, { attachTo: containerElement });
focusInput(wrapper);
Expand Down Expand Up @@ -673,6 +665,37 @@ describe("<DateInput2>", () => {
const wrapper = mount(<DateInput2 {...DEFAULT_PROPS_CONTROLLED} locale="de" value={DATE2_VALUE} />);
assert.strictEqual(wrapper.find(InputGroup).prop("value"), DATE2_UI_STR_DE);
});

describe("when changing timezone", () => {
it("calls onChange with the updated ISO string", () => {
const wrapper = mount(<DateInput2 {...DEFAULT_PROPS_CONTROLLED} />, { attachTo: containerElement });
clickTimezoneItem(wrapper, "Paris");
assert.isTrue(onChange.calledOnce);
assert.strictEqual(onChange.firstCall.args[0], "2021-11-29T10:30:00+01:00");
});

it("formats the returned ISO string according to timePrecision", () => {
const wrapper = mount(
<DateInput2 {...DEFAULT_PROPS_CONTROLLED} timePrecision={TimePrecision.MINUTE} />,
{ attachTo: containerElement },
);
clickTimezoneItem(wrapper, "Paris");
assert.isTrue(onChange.calledOnce);
assert.strictEqual(onChange.firstCall.args[0], "2021-11-29T10:30+01:00");
});

it("updates the displayed timezone", () => {
const wrapper = mount(<DateInput2 {...DEFAULT_PROPS_CONTROLLED} />, { attachTo: containerElement });
clickTimezoneItem(wrapper, "Paris");
assertTimezoneIsSelected(wrapper, "GMT+1");
});

it("before selecting a date (initial value={null})", () => {
const wrapper = mount(<DateInput2 {...DEFAULT_PROPS} value={null} />, { attachTo: containerElement });
clickTimezoneItem(wrapper, "Paris");
assertTimezoneIsSelected(wrapper, "GMT+1");
});
});
});

describe("date formatting", () => {
Expand Down Expand Up @@ -802,6 +825,11 @@ describe("<DateInput2>", () => {
);
}
}

function assertTimezoneIsSelected(wrapper: ReactWrapper<DateInput2Props>, tzCode: string) {
const tzTag = wrapper.find(Tag);
assert.strictEqual(tzTag.text(), tzCode);
}
});

/**
Expand Down