Skip to content

Commit ea10120

Browse files
committed
Parse custom formats using input
1 parent 88b33d7 commit ea10120

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

src/components/Input.tsx

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React, { useCallback, useContext, useEffect, useRef } from "react";
33

44
import { BORDER_COLOR, RING_COLOR } from "../constants";
55
import DatepickerContext from "../contexts/DatepickerContext";
6-
import { dateIsValid } from "../helpers";
6+
import { dateIsValid, parseFormattedDate } from "../helpers";
77

88
import ToggleButton from "./ToggleButton";
99

@@ -66,22 +66,24 @@ const Input: React.FC<Props> = (e: Props) => {
6666
const handleInputChange = useCallback(
6767
(e: React.ChangeEvent<HTMLInputElement>) => {
6868
const inputValue = e.target.value;
69-
const start = `${inputValue.slice(0, 4)}-${inputValue.slice(5, 7)}-${inputValue.slice(
70-
8,
71-
10
72-
)}`;
73-
const end = `${inputValue.slice(13, 17)}-${inputValue.slice(18, 20)}-${inputValue.slice(
74-
21,
75-
inputValue.length
76-
)}`;
69+
70+
const start = parseFormattedDate(inputValue.slice(0, 10), displayFormat).format(
71+
"YYYY-MM-DD"
72+
);
73+
const end = asSingle
74+
? start
75+
: parseFormattedDate(inputValue.slice(11, inputValue.length), displayFormat).format(
76+
"YYYY-MM-DD"
77+
);
78+
7779
const input = inputRef?.current;
7880

7981
if (
8082
start.length === 10 &&
8183
end.length === 10 &&
8284
dateIsValid(new Date(start)) &&
8385
dateIsValid(new Date(end)) &&
84-
dayjs(start).isBefore(end)
86+
(dayjs(start).isBefore(end) || asSingle)
8587
) {
8688
changeDatepickerValue(
8789
{
@@ -90,15 +92,23 @@ const Input: React.FC<Props> = (e: Props) => {
9092
},
9193
e.target
9294
);
93-
changeDayHover(dayjs(end).add(-1, "day").format("YYYY-MM-DD"));
95+
if (!asSingle) changeDayHover(dayjs(end).add(-1, "day").format("YYYY-MM-DD"));
96+
else changeDayHover(start);
9497
hideDatepicker();
9598
if (input) {
9699
input.blur();
97100
}
98101
}
99102
changeInputText(e.target.value);
100103
},
101-
[changeDatepickerValue, changeDayHover, changeInputText, hideDatepicker]
104+
[
105+
changeDatepickerValue,
106+
changeDayHover,
107+
changeInputText,
108+
hideDatepicker,
109+
displayFormat,
110+
asSingle
111+
]
102112
);
103113

104114
// UseEffects && UseLayoutEffect

src/helpers/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import dayjs from "dayjs";
2+
import customParseFormat from "dayjs/plugin/customParseFormat";
23
import weekday from "dayjs/plugin/weekday";
34

45
dayjs.extend(weekday);
6+
dayjs.extend(customParseFormat);
57

68
export function classNames(...classes: (false | null | undefined | string)[]) {
79
return classes.filter(Boolean).join(" ");
@@ -69,6 +71,10 @@ export function formatDate(date: dayjs.Dayjs, format = "YYYY-MM-DD") {
6971
return date.format(format);
7072
}
7173

74+
export function parseFormattedDate(date: string, format = "YYYY-MM-DD") {
75+
return dayjs(date, format);
76+
}
77+
7278
export function getFirstDayInMonth(date: string | dayjs.Dayjs) {
7379
return {
7480
ddd: formatDate(dayjs(date).startOf("month"), "ddd"),

0 commit comments

Comments
 (0)