Skip to content

Commit 138c308

Browse files
authored
Merge pull request #132 from t0m3k/customDatesParsingError
Custom dates parsing error
2 parents 5221164 + afd1112 commit 138c308

File tree

2 files changed

+55
-35
lines changed

2 files changed

+55
-35
lines changed

src/components/Input.tsx

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -68,48 +68,66 @@ const Input: React.FC<Props> = (e: Props) => {
6868
(e: React.ChangeEvent<HTMLInputElement>) => {
6969
const inputValue = e.target.value;
7070

71-
const start = parseFormattedDate(inputValue.slice(0, 10), displayFormat).format(
72-
DATE_FORMAT
73-
);
74-
const end = asSingle
75-
? start
76-
: parseFormattedDate(inputValue.slice(11, inputValue.length), displayFormat).format(
77-
DATE_FORMAT
78-
);
79-
80-
const input = inputRef?.current;
81-
82-
if (
83-
start.length === 10 &&
84-
end.length === 10 &&
85-
dateIsValid(new Date(start)) &&
86-
dateIsValid(new Date(end)) &&
87-
(dayjs(start).isBefore(end) || asSingle)
88-
) {
71+
const dates = [];
72+
73+
if (asSingle) {
74+
const date = parseFormattedDate(inputValue, displayFormat);
75+
if (dateIsValid(date.toDate())) {
76+
dates.push(date.format(DATE_FORMAT));
77+
}
78+
} else {
79+
const parsed = inputValue.split(separator);
80+
81+
let startDate = null;
82+
let endDate = null;
83+
84+
if (parsed.length === 2) {
85+
startDate = parseFormattedDate(parsed[0], displayFormat);
86+
endDate = parseFormattedDate(parsed[1], displayFormat);
87+
} else {
88+
const middle = Math.floor(inputValue.length / 2);
89+
startDate = parseFormattedDate(inputValue.slice(0, middle), displayFormat);
90+
endDate = parseFormattedDate(inputValue.slice(middle), displayFormat);
91+
}
92+
93+
if (
94+
dateIsValid(startDate.toDate()) &&
95+
dateIsValid(endDate.toDate()) &&
96+
startDate.isBefore(endDate)
97+
) {
98+
dates.push(startDate.format(DATE_FORMAT));
99+
dates.push(endDate.format(DATE_FORMAT));
100+
}
101+
}
102+
103+
if (dates[0]) {
89104
changeDatepickerValue(
90105
{
91-
startDate: start,
92-
endDate: end
106+
startDate: dates[0],
107+
endDate: dates[1] || dates[0]
93108
},
94109
e.target
95110
);
96-
if (!asSingle) changeDayHover(dayjs(end).add(-1, "day").format(DATE_FORMAT));
97-
else changeDayHover(start);
98-
hideDatepicker();
111+
if (dates[1]) changeDayHover(dayjs(dates[1]).add(-1, "day").format(DATE_FORMAT));
112+
else changeDayHover(dates[0]);
113+
}
114+
115+
changeInputText(e.target.value);
116+
},
117+
[asSingle, displayFormat, separator, changeDatepickerValue, changeDayHover, changeInputText]
118+
);
119+
120+
const handleInputKeyDown = useCallback(
121+
(e: React.KeyboardEvent<HTMLInputElement>) => {
122+
if (e.key === "Enter") {
123+
const input = inputRef.current;
99124
if (input) {
100125
input.blur();
101126
}
127+
hideDatepicker();
102128
}
103-
changeInputText(e.target.value);
104129
},
105-
[
106-
changeDatepickerValue,
107-
changeDayHover,
108-
changeInputText,
109-
hideDatepicker,
110-
displayFormat,
111-
asSingle
112-
]
130+
[hideDatepicker]
113131
);
114132

115133
const renderToggleIcon = useCallback(
@@ -266,6 +284,7 @@ const Input: React.FC<Props> = (e: Props) => {
266284
autoComplete="off"
267285
role="presentation"
268286
onChange={handleInputChange}
287+
onKeyDown={handleInputKeyDown}
269288
/>
270289

271290
<button

src/contexts/DatepickerContext.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ interface DatepickerStore {
3030
changeDatepickerValue: (value: DateValueType, e?: HTMLInputElement | null | undefined) => void;
3131
showFooter?: boolean;
3232
placeholder?: string | null;
33-
separator?: string;
33+
separator: string;
3434
i18n: string;
3535
value: DateValueType;
3636
disabled?: boolean;
@@ -40,7 +40,7 @@ interface DatepickerStore {
4040
toggleIcon?: (open: boolean) => React.ReactNode;
4141
readOnly?: boolean;
4242
startWeekOn?: string | null;
43-
displayFormat?: string;
43+
displayFormat: string;
4444
minDate?: DateType | null;
4545
maxDate?: DateType | null;
4646
disabledDates?: DateRangeType[] | null;
@@ -94,7 +94,8 @@ const DatepickerContext = createContext<DatepickerStore>({
9494
startWeekOn: START_WEEK,
9595
toggleIcon: undefined,
9696
classNames: undefined,
97-
popoverDirection: undefined
97+
popoverDirection: undefined,
98+
separator: "~"
9899
});
99100

100101
export default DatepickerContext;

0 commit comments

Comments
 (0)