Skip to content

Commit a186869

Browse files
Merge pull request #5743 from qburst/refactor/date-input-value
♻️ Refactor the input value get logic
2 parents 64c6109 + b0ac0ae commit a186869

File tree

3 files changed

+57
-47
lines changed

3 files changed

+57
-47
lines changed

package.json

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,9 @@
2424
"./dist/*.css": "./dist/*.css",
2525
"./src/stylesheets/*.scss": "./src/stylesheets/*.scss"
2626
},
27-
"files": [
28-
"*.md",
29-
"dist",
30-
"lib",
31-
"es",
32-
"src"
33-
],
34-
"sideEffects": [
35-
"**/*.css"
36-
],
37-
"keywords": [
38-
"react",
39-
"datepicker",
40-
"calendar",
41-
"date",
42-
"react-component"
43-
],
27+
"files": ["*.md", "dist", "lib", "es", "src"],
28+
"sideEffects": ["**/*.css"],
29+
"keywords": ["react", "datepicker", "calendar", "date", "react-component"],
4430
"repository": {
4531
"type": "git",
4632
"url": "git://github.com/Hacker0x01/react-datepicker.git"
@@ -140,10 +126,7 @@
140126
"prepare": "husky"
141127
},
142128
"lint-staged": {
143-
"*.{js,jsx,ts,tsx,json,css,scss,md}": [
144-
"prettier --write",
145-
"git add"
146-
]
129+
"*.{js,jsx,ts,tsx,json,css,scss,md}": ["prettier --write", "git add"]
147130
},
148131
"packageManager": "[email protected]"
149132
}

src/index.tsx

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,45 @@ export class DatePicker extends Component<DatePickerProps, DatePickerState> {
422422
};
423423
};
424424

425+
getInputValue = (): string => {
426+
const {
427+
locale,
428+
startDate,
429+
endDate,
430+
rangeSeparator,
431+
selected,
432+
selectedDates,
433+
selectsMultiple,
434+
selectsRange,
435+
value,
436+
} = this.props;
437+
const dateFormat =
438+
this.props.dateFormat ?? DatePicker.defaultProps.dateFormat;
439+
440+
const { inputValue } = this.state;
441+
442+
if (typeof value === "string") {
443+
return value;
444+
} else if (typeof inputValue === "string") {
445+
return inputValue;
446+
} else if (selectsRange) {
447+
return safeDateRangeFormat(startDate, endDate, {
448+
dateFormat,
449+
locale,
450+
rangeSeparator,
451+
});
452+
} else if (selectsMultiple) {
453+
return safeMultipleDatesFormat(selectedDates ?? [], {
454+
dateFormat,
455+
locale,
456+
});
457+
}
458+
return safeDateFormat(selected, {
459+
dateFormat,
460+
locale,
461+
});
462+
};
463+
425464
resetHiddenStatus = (): void => {
426465
this.setState({
427466
...this.state,
@@ -1268,8 +1307,9 @@ export class DatePicker extends Component<DatePickerProps, DatePickerState> {
12681307
};
12691308

12701309
renderAriaLiveRegion = () => {
1271-
const { dateFormat = DatePicker.defaultProps.dateFormat, locale } =
1272-
this.props;
1310+
const { locale } = this.props;
1311+
const dateFormat =
1312+
this.props.dateFormat ?? DatePicker.defaultProps.dateFormat;
12731313
const isContainsTime =
12741314
this.props.showTimeInput || this.props.showTimeSelect;
12751315
const longDateFormat = isContainsTime ? "PPPPp" : "PPPP";
@@ -1345,35 +1385,12 @@ export class DatePicker extends Component<DatePickerProps, DatePickerState> {
13451385

13461386
const customInput = this.props.customInput || <input type="text" />;
13471387
const customInputRef = this.props.customInputRef || "ref";
1348-
const { dateFormat = DatePicker.defaultProps.dateFormat, locale } =
1349-
this.props;
1350-
1351-
const inputValue =
1352-
typeof this.props.value === "string"
1353-
? this.props.value
1354-
: typeof this.state.inputValue === "string"
1355-
? this.state.inputValue
1356-
: this.props.selectsRange
1357-
? safeDateRangeFormat(this.props.startDate, this.props.endDate, {
1358-
dateFormat,
1359-
locale,
1360-
rangeSeparator: this.props.rangeSeparator,
1361-
})
1362-
: this.props.selectsMultiple
1363-
? safeMultipleDatesFormat(this.props.selectedDates ?? [], {
1364-
dateFormat,
1365-
locale,
1366-
})
1367-
: safeDateFormat(this.props.selected, {
1368-
dateFormat,
1369-
locale,
1370-
});
13711388

13721389
return cloneElement(customInput, {
13731390
[customInputRef]: (input: HTMLElement | null) => {
13741391
this.input = input;
13751392
},
1376-
value: inputValue,
1393+
value: this.getInputValue(),
13771394
onBlur: this.handleBlur,
13781395
onChange: this.handleChange,
13791396
onClick: this.onInputClick,

src/test/datepicker_test.test.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4521,4 +4521,14 @@ describe("DatePicker", () => {
45214521
expect(calendarAfterEsc).toBeFalsy();
45224522
});
45234523
});
4524+
4525+
describe("dateFormat", () => {
4526+
it("should use the default dateFormat if dateFormat prop is not provided", () => {
4527+
const { container } = render(
4528+
<DatePicker selected={new Date("2025-07-17")} showDateSelect />,
4529+
);
4530+
const input = safeQuerySelector(container, "input") as HTMLInputElement;
4531+
expect(input?.value).toBe("07/17/2025");
4532+
});
4533+
});
45244534
});

0 commit comments

Comments
 (0)