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): clearing input calls onChange(null) #5409

Merged
merged 7 commits into from
Jul 7, 2022
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
14 changes: 8 additions & 6 deletions packages/datetime/src/dateInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ export interface IDateInputProps extends IDatePickerBaseProps, DateFormatProps,
inputProps?: InputGroupProps2;

/**
* Called when the user selects a new valid date through the `DatePicker` or by typing
* in the input. The second argument is true if the user clicked on a date in the
* calendar, changed the input value, or cleared the selection; it will be false if the date
* was changed by choosing a new month or year.
* Called when the user selects a new valid date through the DatePicker or by typing
* in the input.
*
* @param newDate Date or `null` (if the date is invalid or text input has been cleared)
* @param isUserChange `true` if the user clicked on a date in the calendar, changed the input value,
* or cleared the selection; `false` if the date was changed by changing the month or year.
*/
onChange?: (selectedDate: Date, isUserChange: boolean) => void;
onChange?: (selectedDate: Date | null, isUserChange: boolean) => void;

/**
* Called when the user finishes typing in a new date and the date causes an error state.
Expand Down Expand Up @@ -365,7 +367,7 @@ export class DateInput extends AbstractPureComponent2<DateInputProps, IDateInput

private handleInputBlur = (e: React.FocusEvent<HTMLInputElement>) => {
const { valueString } = this.state;
const date = this.parseDate(valueString);
const date: Date = this.parseDate(valueString);
if (
valueString.length > 0 &&
valueString !== getFormattedDateString(this.state.value, this.props) &&
Expand Down
13 changes: 10 additions & 3 deletions packages/datetime2/src/components/date-input2/dateInput2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ export interface DateInput2Props extends Omit<DateInputProps, "onChange" | "valu
/** The default timezone selected. Defaults to the user local timezone */
defaultTimezone?: string;

/** Callback invoked whenever the date or timezone has changed. ISO string */
onChange: (newDate: string, isUserChange?: boolean) => void;
/**
* Callback invoked whenever the date or timezone has changed.
*
* @param newDate ISO string or `null` (if the date is invalid or text input has been cleared)
* @param isUserChange `true` if the user clicked on a date in the calendar, changed the input value,
* or cleared the selection; `false` if the date was changed by changing the month or year.
*/
onChange: (newDate: string | null, isUserChange?: boolean) => void;

/** An ISO string mapping to the selected time. */
/** An ISO string representing the selected time. */
value: string | null;

/**
Expand Down Expand Up @@ -88,6 +94,7 @@ export const DateInput2: React.FC<DateInput2Props> = React.memo(function _DateIn
const handleDateChange = React.useCallback(
(newDate: Date | null, isUserChange: boolean) => {
if (newDate == null) {
onChange?.(newDate, isUserChange);
return;
}
const newDateString = getIsoEquivalentWithUpdatedTimezone(newDate, timezoneValue, timePrecision);
Expand Down
11 changes: 10 additions & 1 deletion packages/datetime2/test/components/dateInput2Tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { mount } from "enzyme";
import * as React from "react";
import * as sinon from "sinon";

import { Position } from "@blueprintjs/core";
import { InputGroup, Position } from "@blueprintjs/core";
import { DateInput, TimePrecision } from "@blueprintjs/datetime";

import { DateInput2, DateInput2Props, TimezoneSelect } from "../../src";
Expand Down Expand Up @@ -132,4 +132,13 @@ describe("<DateInput2>", () => {
assert.strictEqual(dateinput.prop("inputProps"), inputProps);
assert.strictEqual(dateinput.prop("popoverProps"), popoverProps);
});

it("Clearing the input invokes onChange with null", () => {
const wrapper = mount(<DateInput2 {...DEFAULT_PROPS} />);
wrapper
.find(InputGroup)
.find("input")
.simulate("change", { target: { value: "" } });
assert.isTrue(onChange.calledOnceWithExactly(null, true));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class DateInput2Example extends React.PureComponent<IExampleProps, DateIn
);
}

private handleDateChange = (date: string) => this.setState({ date });
private handleDateChange = (date: string | null) => this.setState({ date });

private handleFormatChange = (format: DateFormatProps) => this.setState({ format });
}