Skip to content

Commit 82790be

Browse files
authored
Merge pull request #63 from t0m3k/parseCustomFormat
Parse custom formats when changing input text
2 parents 5603305 + 927f058 commit 82790be

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

src/components/Input.tsx

Lines changed: 23 additions & 13 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, DATE_FORMAT, 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

@@ -53,7 +53,7 @@ const Input: React.FC<Props> = (e: Props) => {
5353

5454
const border = BORDER_COLOR.focus[primaryColor as keyof typeof BORDER_COLOR.focus];
5555
const ring =
56-
RING_COLOR["second-focus"][primaryColor as keyof (typeof RING_COLOR)["second-focus"]];
56+
RING_COLOR["second-focus"][primaryColor as keyof typeof RING_COLOR["second-focus"]];
5757

5858
const defaultInputClassName = `relative transition-all duration-300 py-2.5 pl-4 pr-14 w-full border-gray-300 dark:bg-slate-800 dark:text-white/80 dark:border-slate-600 rounded-lg tracking-wide font-light text-sm placeholder-gray-400 bg-white focus:ring disabled:opacity-40 disabled:cursor-not-allowed ${border} ${ring}`;
5959

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

8082
if (
8183
start.length === 10 &&
8284
end.length === 10 &&
8385
dateIsValid(new Date(start)) &&
8486
dateIsValid(new Date(end)) &&
85-
dayjs(start).isBefore(end)
87+
(dayjs(start).isBefore(end) || asSingle)
8688
) {
8789
changeDatepickerValue(
8890
{
@@ -91,15 +93,23 @@ const Input: React.FC<Props> = (e: Props) => {
9193
},
9294
e.target
9395
);
94-
changeDayHover(dayjs(end).add(-1, "day").format(DATE_FORMAT));
96+
if (!asSingle) changeDayHover(dayjs(end).add(-1, "day").format(DATE_FORMAT));
97+
else changeDayHover(start);
9598
hideDatepicker();
9699
if (input) {
97100
input.blur();
98101
}
99102
}
100103
changeInputText(e.target.value);
101104
},
102-
[changeDatepickerValue, changeDayHover, changeInputText, hideDatepicker]
105+
[
106+
changeDatepickerValue,
107+
changeDayHover,
108+
changeInputText,
109+
hideDatepicker,
110+
displayFormat,
111+
asSingle
112+
]
103113
);
104114

105115
const renderToggleIcon = useCallback(

src/helpers/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import dayjs from "dayjs";
2+
import customParseFormat from "dayjs/plugin/customParseFormat";
3+
import weekday from "dayjs/plugin/weekday";
4+
5+
dayjs.extend(weekday);
6+
dayjs.extend(customParseFormat);
27

38
import { DATE_FORMAT, LANGUAGE } from "../constants";
49

@@ -68,6 +73,10 @@ export function formatDate(date: dayjs.Dayjs, format = DATE_FORMAT) {
6873
return date.format(format);
6974
}
7075

76+
export function parseFormattedDate(date: string, format = "YYYY-MM-DD") {
77+
return dayjs(date, format);
78+
}
79+
7180
export function getFirstDayInMonth(date: string | dayjs.Dayjs) {
7281
return {
7382
ddd: formatDate(dayjs(date).startOf("month"), "ddd"),

0 commit comments

Comments
 (0)