Skip to content

Commit 4e3009b

Browse files
authored
[DateInput]: Remove defaultProps and consolidate datetime constants (#7516)
1 parent c524693 commit 4e3009b

File tree

10 files changed

+72
-85
lines changed

10 files changed

+72
-85
lines changed

packages/datetime/src/components/date-input/dateInput.tsx

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ import { getDefaultDateFnsFormat } from "../../common/dateFnsFormatUtils";
3535
import { useDateFnsLocale } from "../../common/dateFnsLocaleUtils";
3636
import type { ReactDayPickerSingleProps } from "../../common/reactDayPickerProps";
3737
import { DatePicker } from "../date-picker/datePicker";
38-
import { DatePickerUtils } from "../date-picker/datePickerUtils";
38+
import { INVALID_DATE_MESSAGE, LOCALE, MAX_DATE, MIN_DATE, OUT_OF_RANGE_MESSAGE } from "../dateConstants";
3939
import type { DatePickerShortcut } from "../shortcuts/shortcuts";
4040
import { TimezoneSelect } from "../timezone-select/timezoneSelect";
4141

42-
import type { DateInputDefaultProps, DateInputProps, DateInputPropsWithDefaults } from "./dateInputProps";
42+
import type { DateInputProps } from "./dateInputProps";
4343
import { useDateFormatter } from "./useDateFormatter";
4444
import { useDateParser } from "./useDateParser";
4545

@@ -51,50 +51,40 @@ const timezoneSelectButtonProps: Partial<ButtonProps> = {
5151
outlined: true,
5252
};
5353

54-
export const DATEINPUT_DEFAULT_PROPS: DateInputDefaultProps = {
55-
closeOnSelection: true,
56-
disabled: false,
57-
invalidDateMessage: "Invalid date",
58-
locale: "en-US",
59-
maxDate: DatePickerUtils.getDefaultMaxDate(),
60-
minDate: DatePickerUtils.getDefaultMinDate(),
61-
outOfRangeMessage: "Out of range",
62-
reverseMonthAndYearMenus: false,
63-
};
64-
6554
/**
6655
* Date input component.
6756
*
6857
* @see https://blueprintjs.com/docs/#datetime/date-input
6958
*/
7059
export const DateInput: React.FC<DateInputProps> = memo(function DateInput(props) {
7160
const {
72-
closeOnSelection,
61+
closeOnSelection = true,
7362
dateFnsFormat,
7463
dateFnsLocaleLoader,
7564
defaultTimezone,
7665
defaultValue,
77-
disabled,
66+
disabled = false,
7867
disableTimezoneSelect,
7968
fill,
8069
inputProps = {},
81-
invalidDateMessage,
82-
locale: localeOrCode,
83-
maxDate,
84-
minDate,
70+
invalidDateMessage = INVALID_DATE_MESSAGE,
71+
locale: localeOrCode = LOCALE,
72+
maxDate = MAX_DATE,
73+
minDate = MIN_DATE,
8574
onChange,
8675
onError,
8776
onTimezoneChange,
88-
outOfRangeMessage,
77+
outOfRangeMessage = OUT_OF_RANGE_MESSAGE,
8978
popoverProps = {},
9079
popoverRef,
9180
rightElement,
81+
reverseMonthAndYearMenus = false,
9282
showTimezoneSelect,
9383
timePrecision,
9484
timezone: controlledTimezone,
9585
value,
9686
...datePickerProps
97-
} = props as DateInputPropsWithDefaults;
87+
} = props;
9888

9989
const locale = useDateFnsLocale(localeOrCode, dateFnsLocaleLoader);
10090
const placeholder = getPlaceholder(props);
@@ -562,11 +552,8 @@ export const DateInput: React.FC<DateInputProps> = memo(function DateInput(props
562552
/>
563553
);
564554
});
565-
DateInput.displayName = `${DISPLAYNAME_PREFIX}.DateInput`;
566555

567-
// TODO: Removing `defaultProps` here breaks tests. Investigate why.
568-
// eslint-disable-next-line @typescript-eslint/no-deprecated
569-
DateInput.defaultProps = DATEINPUT_DEFAULT_PROPS;
556+
DateInput.displayName = `${DISPLAYNAME_PREFIX}.DateInput`;
570557

571558
/** Gets the input `placeholder` value from props, using default values if undefined */
572559
function getPlaceholder(props: DateInputProps): string | undefined {

packages/datetime/src/components/date-input/dateInputProps.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,3 @@ export interface DateInputProps
171171
/** An ISO string representing the selected time. */
172172
value?: string | null;
173173
}
174-
175-
export type DateInputDefaultProps = Required<
176-
Pick<
177-
DateInputProps,
178-
| "closeOnSelection"
179-
| "disabled"
180-
| "invalidDateMessage"
181-
| "locale"
182-
| "maxDate"
183-
| "minDate"
184-
| "outOfRangeMessage"
185-
| "reverseMonthAndYearMenus"
186-
>
187-
>;
188-
189-
export type DateInputPropsWithDefaults = Omit<DateInputProps, keyof DateInputDefaultProps> & DateInputDefaultProps;

packages/datetime/src/components/date-input/useDateFormatter.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import { useCallback } from "react";
2020
import { DateUtils } from "../../common";
2121
import { getDateFnsFormatter, getDefaultDateFnsFormat } from "../../common/dateFnsFormatUtils";
2222
import { getLocaleCodeFromProps } from "../../common/dateFnsLocaleProps";
23+
import { INVALID_DATE_MESSAGE, MAX_DATE, MIN_DATE, OUT_OF_RANGE_MESSAGE } from "../dateConstants";
2324

24-
import type { DateInputProps, DateInputPropsWithDefaults } from "./dateInputProps";
25+
import type { DateInputProps } from "./dateInputProps";
2526

2627
/**
2728
* Create a date string parser function based on a given locale.
@@ -34,13 +35,13 @@ export function useDateFormatter(props: DateInputProps, locale: Locale | undefin
3435
dateFnsFormat,
3536
locale: localeFromProps,
3637
formatDate,
37-
invalidDateMessage,
38-
maxDate,
39-
minDate,
40-
outOfRangeMessage,
38+
invalidDateMessage = INVALID_DATE_MESSAGE,
39+
maxDate = MAX_DATE,
40+
minDate = MIN_DATE,
41+
outOfRangeMessage = OUT_OF_RANGE_MESSAGE,
4142
timePickerProps,
4243
timePrecision,
43-
} = props as DateInputPropsWithDefaults;
44+
} = props;
4445

4546
return useCallback(
4647
(date: Date | undefined) => {

packages/datetime/src/components/date-input/useDateParser.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ import { useCallback } from "react";
1919

2020
import { getDateFnsParser, getDefaultDateFnsFormat } from "../../common/dateFnsFormatUtils";
2121
import { getLocaleCodeFromProps } from "../../common/dateFnsLocaleProps";
22+
import { INVALID_DATE, INVALID_DATE_MESSAGE, OUT_OF_RANGE_MESSAGE } from "../dateConstants";
2223

23-
import type { DateInputProps, DateInputPropsWithDefaults } from "./dateInputProps";
24-
25-
const INVALID_DATE = new Date(undefined!);
24+
import type { DateInputProps } from "./dateInputProps";
2625

2726
/**
2827
* Create a date string parser function based on a given locale.
@@ -33,13 +32,13 @@ const INVALID_DATE = new Date(undefined!);
3332
export function useDateParser(props: DateInputProps, locale: Locale | undefined) {
3433
const {
3534
dateFnsFormat,
36-
invalidDateMessage,
35+
invalidDateMessage = INVALID_DATE_MESSAGE,
3736
locale: localeFromProps,
38-
outOfRangeMessage,
37+
outOfRangeMessage = OUT_OF_RANGE_MESSAGE,
3938
parseDate,
4039
timePickerProps,
4140
timePrecision,
42-
} = props as DateInputPropsWithDefaults;
41+
} = props;
4342

4443
return useCallback(
4544
(dateString: string): Date | null => {

packages/datetime/src/components/date-picker/datePicker.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { Button, DISPLAYNAME_PREFIX, Divider } from "@blueprintjs/core";
2222

2323
import { Classes, type DateRange, DateUtils, Errors, TimezoneUtils } from "../../common";
2424
import { dayPickerClassNameOverrides } from "../../common/classes";
25+
import { LOCALE, MAX_DATE, MIN_DATE } from "../dateConstants";
2526
import { DateFnsLocalizedComponent } from "../dateFnsLocalizedComponent";
2627
import { DatePickerDropdown } from "../react-day-picker/datePickerDropdown";
2728
import { IconLeft, IconRight } from "../react-day-picker/datePickerNavIcons";
@@ -31,7 +32,6 @@ import { TimePicker } from "../time-picker/timePicker";
3132
import { DatePickerProvider } from "./datePickerContext";
3233
import type { DatePickerProps } from "./datePickerProps";
3334
import type { DatePickerState } from "./datePickerState";
34-
import { DatePickerUtils } from "./datePickerUtils";
3535

3636
export type { DatePickerProps };
3737

@@ -46,9 +46,9 @@ export class DatePicker extends DateFnsLocalizedComponent<DatePickerProps, DateP
4646
clearButtonText: "Clear",
4747
dayPickerProps: {},
4848
highlightCurrentDay: false,
49-
locale: "en-US",
50-
maxDate: DatePickerUtils.getDefaultMaxDate(),
51-
minDate: DatePickerUtils.getDefaultMinDate(),
49+
locale: LOCALE,
50+
maxDate: MAX_DATE,
51+
minDate: MIN_DATE,
5252
reverseMonthAndYearMenus: false,
5353
shortcuts: false,
5454
showActionsBar: false,
@@ -228,11 +228,7 @@ export class DatePicker extends DateFnsLocalizedComponent<DatePickerProps, DateP
228228
}
229229

230230
const { selectedShortcutIndex } = this.state;
231-
const {
232-
maxDate = DatePickerUtils.getDefaultMaxDate(),
233-
minDate = DatePickerUtils.getDefaultMinDate(),
234-
timePrecision,
235-
} = this.props;
231+
const { maxDate = MAX_DATE, minDate = MIN_DATE, timePrecision } = this.props;
236232
// Reuse the existing date range shortcuts and only care about start date
237233
const dateRangeShortcuts: DateRangeShortcut[] | true =
238234
shortcuts === true

packages/datetime/src/components/date-range-input/dateRangeInput.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,15 @@ import {
3535
import { Classes, type DateRange, DateUtils, Errors, type NonNullDateRange } from "../../common";
3636
import { getDateFnsFormatter, getDateFnsParser, getDefaultDateFnsFormat } from "../../common/dateFnsFormatUtils";
3737
import { getLocaleCodeFromProps } from "../../common/dateFnsLocaleProps";
38-
import { DatePickerUtils } from "../date-picker/datePickerUtils";
3938
import { DateRangePicker } from "../date-range-picker/dateRangePicker";
39+
import {
40+
INVALID_DATE_MESSAGE,
41+
LOCALE,
42+
MAX_DATE,
43+
MIN_DATE,
44+
OUT_OF_RANGE_MESSAGE,
45+
OVERLAPPING_DATES_MESSAGE,
46+
} from "../dateConstants";
4047
import { DateFnsLocalizedComponent } from "../dateFnsLocalizedComponent";
4148
import type { DateRangeShortcut } from "../shortcuts/shortcuts";
4249

@@ -93,12 +100,12 @@ export class DateRangeInput extends DateFnsLocalizedComponent<DateRangeInputProp
93100
dayPickerProps: {},
94101
disabled: false,
95102
endInputProps: {},
96-
invalidDateMessage: "Invalid date",
97-
locale: "en-US",
98-
maxDate: DatePickerUtils.getDefaultMaxDate(),
99-
minDate: DatePickerUtils.getDefaultMinDate(),
100-
outOfRangeMessage: "Out of range",
101-
overlappingDatesMessage: "Overlapping dates",
103+
invalidDateMessage: INVALID_DATE_MESSAGE,
104+
locale: LOCALE,
105+
maxDate: MAX_DATE,
106+
minDate: MIN_DATE,
107+
outOfRangeMessage: OUT_OF_RANGE_MESSAGE,
108+
overlappingDatesMessage: OVERLAPPING_DATES_MESSAGE,
102109
popoverProps: {},
103110
selectAllOnFocus: false,
104111
shortcuts: true,

packages/datetime/src/components/date-range-picker/dateRangePicker.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { DateRangeSelectionStrategy } from "../../common/dateRangeSelectionStrat
2626
import { combineModifiers, HOVERED_RANGE_MODIFIER } from "../../common/dayPickerModifiers";
2727
import { MonthAndYear } from "../../common/monthAndYear";
2828
import { DatePickerProvider } from "../date-picker/datePickerContext";
29-
import { DatePickerUtils } from "../date-picker/datePickerUtils";
29+
import { LOCALE, MAX_DATE, MIN_DATE } from "../dateConstants";
3030
import { DateFnsLocalizedComponent } from "../dateFnsLocalizedComponent";
3131
import { DatePickerShortcutMenu, type DateRangeShortcut } from "../shortcuts/shortcuts";
3232
import { TimePicker } from "../time-picker/timePicker";
@@ -50,9 +50,9 @@ export class DateRangePicker extends DateFnsLocalizedComponent<DateRangePickerPr
5050
allowSingleDayRange: false,
5151
contiguousCalendarMonths: true,
5252
dayPickerProps: {},
53-
locale: "en-US",
54-
maxDate: DatePickerUtils.getDefaultMaxDate(),
55-
minDate: DatePickerUtils.getDefaultMinDate(),
53+
locale: LOCALE,
54+
maxDate: MAX_DATE,
55+
minDate: MIN_DATE,
5656
reverseMonthAndYearMenus: false,
5757
shortcuts: true,
5858
singleMonthOnly: false,
@@ -204,12 +204,7 @@ export class DateRangePicker extends DateFnsLocalizedComponent<DateRangePickerPr
204204
}
205205

206206
const { selectedShortcutIndex } = this.state;
207-
const {
208-
allowSingleDayRange,
209-
maxDate = DatePickerUtils.getDefaultMaxDate(),
210-
minDate = DatePickerUtils.getDefaultMinDate(),
211-
timePrecision,
212-
} = this.props;
207+
const { allowSingleDayRange, maxDate = MAX_DATE, minDate = MIN_DATE, timePrecision } = this.props;
213208
return [
214209
<DatePickerShortcutMenu
215210
key="shortcuts"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* !
2+
* (c) Copyright 2025 Palantir Technologies Inc. All rights reserved.
3+
*/
4+
5+
import { DatePickerUtils } from "./date-picker/datePickerUtils";
6+
7+
export const LOCALE = "en-US";
8+
9+
export const MAX_DATE = DatePickerUtils.getDefaultMaxDate();
10+
export const MIN_DATE = DatePickerUtils.getDefaultMinDate();
11+
12+
export const INVALID_DATE_MESSAGE = "Invalid date";
13+
export const OUT_OF_RANGE_MESSAGE = "Out of range";
14+
export const OVERLAPPING_DATES_MESSAGE = "Overlapping dates";
15+
16+
export const INVALID_DATE = new Date(undefined!);

packages/datetime/test/common/dateFormatPropsTests.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { format } from "date-fns";
77

88
import { getFormattedDateString } from "../../src/common/dateFormatProps";
99
import { Months } from "../../src/common/months";
10+
import { OUT_OF_RANGE_MESSAGE } from "../../src/components/dateConstants";
1011

1112
const formatDate = (date: Date) => format(date, "yyyy-MM-dd");
1213

@@ -39,7 +40,7 @@ describe("DateFormatProps", () => {
3940
const testDate = new Date(2025, Months.DECEMBER, 31);
4041
const minDate = new Date(2025, Months.DECEMBER, 1);
4142
const maxDate = new Date(2025, Months.DECEMBER, 30);
42-
const outOfRangeMessage = "OUT OF RANGE";
43+
const outOfRangeMessage = OUT_OF_RANGE_MESSAGE;
4344

4445
expect(getFormattedDateString(testDate, { formatDate, maxDate, minDate, outOfRangeMessage })).to.equal(
4546
outOfRangeMessage,

packages/datetime/test/components/dateInputTests.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ import {
3636
} from "../../src";
3737
import { DefaultDateFnsFormats, getDateFnsFormatter } from "../../src/common/dateFnsFormatUtils";
3838
import { TIMEZONE_ITEMS } from "../../src/common/timezoneItems";
39-
import { DateInput, DATEINPUT_DEFAULT_PROPS, type DateInputProps } from "../../src/components/date-input/dateInput";
39+
import { DateInput, type DateInputProps } from "../../src/components/date-input/dateInput";
4040
import { DatePicker } from "../../src/components/date-picker/datePicker";
41+
import { INVALID_DATE_MESSAGE, LOCALE } from "../../src/components/dateConstants";
4142
import { loadDateFnsLocaleFake } from "../common/loadDateFnsLocaleFake";
4243

4344
const NEW_YORK_TIMEZONE = TIMEZONE_ITEMS.find(item => item.label === "New York")!;
@@ -455,7 +456,7 @@ describe("<DateInput>", () => {
455456
});
456457

457458
it("typing in an invalid date displays the error message and calls onError with Date(undefined)", () => {
458-
const invalidDateMessage = "INVALID DATE";
459+
const invalidDateMessage = INVALID_DATE_MESSAGE;
459460
const onError = sinon.spy();
460461
const wrapper = mount(
461462
<DateInput
@@ -669,7 +670,7 @@ describe("<DateInput>", () => {
669670
focusInput(wrapper);
670671
changeInput(wrapper, "4/77/2016");
671672
blurInput(wrapper);
672-
assert.strictEqual(wrapper.find(InputGroup).prop("value"), DATEINPUT_DEFAULT_PROPS.invalidDateMessage);
673+
assert.strictEqual(wrapper.find(InputGroup).prop("value"), INVALID_DATE_MESSAGE);
673674
});
674675

675676
it("text input does not show error styling until user is done typing and blurs the input", () => {
@@ -780,7 +781,7 @@ describe("<DateInput>", () => {
780781
describe("with formatDate & parseDate defined", () => {
781782
const formatDate = sinon.stub().returns("custom date");
782783
const parseDate = sinon.stub().returns(today);
783-
const localeCode = "en-US";
784+
const localeCode = LOCALE;
784785
const FORMATTING_PROPS: DateInputProps = {
785786
dateFnsLocaleLoader: DEFAULT_PROPS.dateFnsLocaleLoader,
786787
formatDate,
@@ -819,7 +820,7 @@ describe("<DateInput>", () => {
819820
});
820821
changeInput(wrapper, "invalid");
821822
blurInput(wrapper);
822-
assert.strictEqual(wrapper.find("input").prop("value"), DATEINPUT_DEFAULT_PROPS.invalidDateMessage);
823+
assert.strictEqual(wrapper.find("input").prop("value"), INVALID_DATE_MESSAGE);
823824
});
824825
});
825826

0 commit comments

Comments
 (0)