Skip to content

Commit

Permalink
[DateRangePicker] Allow date picker popover to be closed by esc key p…
Browse files Browse the repository at this point in the history
…ress (#7033)
  • Loading branch information
ggdouglas authored Oct 30, 2024
1 parent aa64811 commit 051361b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -630,10 +630,18 @@ export class DateRangeInput extends AbstractPureComponent<DateRangeInputProps, D
private handleInputKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
const isTabPressed = e.key === "Tab";
const isEnterPressed = e.key === "Enter";
const isEscapeKeyPressed = e.key === "Escape";
const isShiftPressed = e.shiftKey;

const { selectedStart, selectedEnd } = this.state;

if (isEscapeKeyPressed) {
this.startInputElement?.blur();
this.endInputElement?.blur();
this.setState({ isOpen: false, isStartInputFocused: false, isEndInputFocused: false });
return;
}

// order of JS events is our enemy here. when tabbing between fields,
// this handler will fire in the middle of a focus exchange when no
// field is currently focused. we work around this by referring to the
Expand Down
15 changes: 15 additions & 0 deletions packages/datetime/test/components/dateRangeInputTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,21 @@ describe("<DateRangeInput>", () => {
expect(root.state("isOpen"), "popover closed at end").to.be.false;
});

it("pressing Escape closes the popover", () => {
const { root } = wrap(<DateRangeInput {...DATE_FORMAT} value={[null, null]} />);
root.setState({ isOpen: true });

const startInput = getStartInput(root);
startInput.simulate("focus");

expect(root.state("isOpen")).to.be.true;

startInput.simulate("keydown", { key: "Escape" });

expect(root.state("isOpen")).to.be.false;
expect(isStartInputFocused(root)).to.be.false;
});

it("Clicking a date invokes onChange with the new date range and updates the input fields", () => {
const defaultValue = [START_DATE, null] as DateRange;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,18 @@ export class DateRangeInput3 extends DateFnsLocalizedComponent<DateRangeInput3Pr
private handleInputKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
const isTabPressed = e.key === "Tab";
const isEnterPressed = e.key === "Enter";
const isEscapeKeyPressed = e.key === "Escape";
const isShiftPressed = e.shiftKey;

const { selectedStart, selectedEnd } = this.state;

if (isEscapeKeyPressed) {
this.startInputElement?.blur();
this.endInputElement?.blur();
this.setState({ isOpen: false, isStartInputFocused: false, isEndInputFocused: false });
return;
}

// order of JS events is our enemy here. when tabbing between fields,
// this handler will fire in the middle of a focus exchange when no
// field is currently focused. we work around this by referring to the
Expand Down
15 changes: 15 additions & 0 deletions packages/datetime2/test/components/dateRangeInput3Tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2343,6 +2343,21 @@ describe("<DateRangeInput3>", () => {
assertDateRangesEqual(onChange.args[1][0], [START_STR, null]);
});

it("pressing Escape closes the popover", () => {
const { root } = wrap(<DateRangeInput3 {...DATE_FORMAT} value={[null, null]} />);
root.setState({ isOpen: true });

const startInput = getStartInput(root);
startInput.simulate("focus");

expect(root.state("isOpen")).to.be.true;

startInput.simulate("keydown", { key: "Escape" });

expect(root.state("isOpen")).to.be.false;
expect(isStartInputFocused(root)).to.be.false;
});

it("Clicking a date invokes onChange with the new date range and updates the input field text", () => {
const onChange = sinon.spy();
const { root, getDayElement } = wrap(
Expand Down

1 comment on commit 051361b

@svc-palantir-github
Copy link

Choose a reason for hiding this comment

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

[DateRangePicker] Allow date picker popover to be closed by esc key press (#7033)

Build artifact links for this commit: documentation | landing | table | demo

This is an automated comment from the deploy-preview CircleCI job.

Please sign in to comment.