Skip to content

Commit 6872212

Browse files
committed
Fix the problems with custom dates
1 parent 5221164 commit 6872212

File tree

2 files changed

+49
-35
lines changed

2 files changed

+49
-35
lines changed

src/components/Input.tsx

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -68,48 +68,60 @@ 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+
if (parsed.length === 2) {
81+
const startDate = parseFormattedDate(parsed[0], displayFormat);
82+
const endDate = parseFormattedDate(parsed[1], displayFormat);
83+
84+
if (
85+
dateIsValid(startDate.toDate()) &&
86+
dateIsValid(endDate.toDate()) &&
87+
startDate.isBefore(endDate)
88+
) {
89+
dates.push(startDate.format(DATE_FORMAT));
90+
dates.push(endDate.format(DATE_FORMAT));
91+
}
92+
} else {
93+
// TODO: Handle the case where there is separator in the date format or no separator at all
94+
}
95+
}
96+
97+
if (dates[0]) {
8998
changeDatepickerValue(
9099
{
91-
startDate: start,
92-
endDate: end
100+
startDate: dates[0],
101+
endDate: dates[1] || dates[0]
93102
},
94103
e.target
95104
);
96-
if (!asSingle) changeDayHover(dayjs(end).add(-1, "day").format(DATE_FORMAT));
97-
else changeDayHover(start);
98-
hideDatepicker();
105+
if (dates[1]) changeDayHover(dayjs(dates[1]).add(-1, "day").format(DATE_FORMAT));
106+
else changeDayHover(dates[0]);
107+
}
108+
109+
changeInputText(e.target.value);
110+
},
111+
[asSingle, displayFormat, separator, changeDatepickerValue, changeDayHover, changeInputText]
112+
);
113+
114+
const handleInputKeyDown = useCallback(
115+
(e: React.KeyboardEvent<HTMLInputElement>) => {
116+
if (e.key === "Enter") {
117+
const input = inputRef.current;
99118
if (input) {
100119
input.blur();
101120
}
121+
hideDatepicker();
102122
}
103-
changeInputText(e.target.value);
104123
},
105-
[
106-
changeDatepickerValue,
107-
changeDayHover,
108-
changeInputText,
109-
hideDatepicker,
110-
displayFormat,
111-
asSingle
112-
]
124+
[hideDatepicker]
113125
);
114126

115127
const renderToggleIcon = useCallback(
@@ -266,6 +278,7 @@ const Input: React.FC<Props> = (e: Props) => {
266278
autoComplete="off"
267279
role="presentation"
268280
onChange={handleInputChange}
281+
onKeyDown={handleInputKeyDown}
269282
/>
270283

271284
<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)