Skip to content

Commit 66df4dc

Browse files
committed
🔨 Properly set the dateFormat prop in the DatePicker component to handle both null and undefined case
1 parent 184e047 commit 66df4dc

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

‎src/index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,6 @@ export default class DatePicker extends Component<
425425

426426
getInputValue = (): string => {
427427
const {
428-
dateFormat = DatePicker.defaultProps.dateFormat,
429428
locale,
430429
startDate,
431430
endDate,
@@ -435,6 +434,9 @@ export default class DatePicker extends Component<
435434
selectsRange,
436435
value,
437436
} = this.props;
437+
const dateFormat =
438+
this.props.dateFormat ?? DatePicker.defaultProps.dateFormat;
439+
438440
const { inputValue } = this.state;
439441

440442
if (typeof value === "string") {
@@ -1296,8 +1298,9 @@ export default class DatePicker extends Component<
12961298
};
12971299

12981300
renderAriaLiveRegion = () => {
1299-
const { dateFormat = DatePicker.defaultProps.dateFormat, locale } =
1300-
this.props;
1301+
const { locale } = this.props;
1302+
const dateFormat =
1303+
this.props.dateFormat ?? DatePicker.defaultProps.dateFormat;
13011304
const isContainsTime =
13021305
this.props.showTimeInput || this.props.showTimeSelect;
13031306
const longDateFormat = isContainsTime ? "PPPPp" : "PPPP";

‎src/test/datepicker_test.test.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4504,4 +4504,14 @@ describe("DatePicker", () => {
45044504
expect(calendarAfterEsc).toBeFalsy();
45054505
});
45064506
});
4507+
4508+
describe("dateFormat", () => {
4509+
it("should use the default dateFormat if dateFormat prop is not provided", () => {
4510+
const { container } = render(
4511+
<DatePicker selected={new Date("2025-07-17")} showDateSelect />,
4512+
);
4513+
const input = safeQuerySelector(container, "input") as HTMLInputElement;
4514+
expect(input?.value).toBe("07/17/2025");
4515+
});
4516+
});
45074517
});

0 commit comments

Comments
 (0)