Skip to content

Commit 184e047

Browse files
committed
♻️ Refactor the input value get logic to a seperate function
- Move the input value get logic to a seperate function - Refactor the nested conditional statements to if else conditions for readability
1 parent ec4ce2c commit 184e047

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

src/index.tsx

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,41 @@ export default class DatePicker extends Component<
423423
};
424424
};
425425

426+
getInputValue = (): string => {
427+
const {
428+
dateFormat = DatePicker.defaultProps.dateFormat,
429+
locale,
430+
startDate,
431+
endDate,
432+
selected,
433+
selectedDates,
434+
selectsMultiple,
435+
selectsRange,
436+
value,
437+
} = this.props;
438+
const { inputValue } = this.state;
439+
440+
if (typeof value === "string") {
441+
return value;
442+
} else if (typeof inputValue === "string") {
443+
return inputValue;
444+
} else if (selectsRange) {
445+
return safeDateRangeFormat(startDate, endDate, {
446+
dateFormat,
447+
locale,
448+
});
449+
} else if (selectsMultiple) {
450+
return safeMultipleDatesFormat(selectedDates ?? [], {
451+
dateFormat,
452+
locale,
453+
});
454+
}
455+
return safeDateFormat(selected, {
456+
dateFormat,
457+
locale,
458+
});
459+
};
460+
426461
resetHiddenStatus = (): void => {
427462
this.setState({
428463
...this.state,
@@ -1338,33 +1373,12 @@ export default class DatePicker extends Component<
13381373

13391374
const customInput = this.props.customInput || <input type="text" />;
13401375
const customInputRef = this.props.customInputRef || "ref";
1341-
const { dateFormat = DatePicker.defaultProps.dateFormat, locale } =
1342-
this.props;
1343-
const inputValue =
1344-
typeof this.props.value === "string"
1345-
? this.props.value
1346-
: typeof this.state.inputValue === "string"
1347-
? this.state.inputValue
1348-
: this.props.selectsRange
1349-
? safeDateRangeFormat(this.props.startDate, this.props.endDate, {
1350-
dateFormat,
1351-
locale,
1352-
})
1353-
: this.props.selectsMultiple
1354-
? safeMultipleDatesFormat(this.props.selectedDates ?? [], {
1355-
dateFormat,
1356-
locale,
1357-
})
1358-
: safeDateFormat(this.props.selected, {
1359-
dateFormat,
1360-
locale,
1361-
});
13621376

13631377
return cloneElement(customInput, {
13641378
[customInputRef]: (input: HTMLElement | null) => {
13651379
this.input = input;
13661380
},
1367-
value: inputValue,
1381+
value: this.getInputValue(),
13681382
onBlur: this.handleBlur,
13691383
onChange: this.handleChange,
13701384
onClick: this.onInputClick,

0 commit comments

Comments
 (0)