Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/components/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { DateType } from "../types";

import ToggleButton from "./ToggleButton";

const dateRegex = /^\d{4}[-/]\d{2}[-/]\d{2}$|^\d{2}[-/]\d{2}[-/]\d{4}$/;

const Input = () => {
// Context
const {
Expand Down Expand Up @@ -70,24 +72,31 @@ const Input = () => {
const dates: Date[] = [];

if (asSingle) {
const date = dateStringToDate(inputValue);
if (date) {
dates.push(date);
if (dateRegex.test(inputValue)) {
const date = dateStringToDate(inputValue);
if (date) {
dates.push(date);
}
}
} else {
const parsed = inputValue.split(separator);
const parsed = inputValue.split(separator).map(str => str.trim());

let startDate: DateType;
let endDate: DateType;
let startDate: DateType | undefined;
let endDate: DateType | undefined;

if (parsed.length === 2) {
dateStringToDate(parsed[0]);
startDate = dateStringToDate(parsed[0]);
endDate = dateStringToDate(parsed[1]);
if (dateRegex.test(parsed[0]) && dateRegex.test(parsed[1])) {
startDate = dateStringToDate(parsed[0]);
endDate = dateStringToDate(parsed[1]);
}
} else {
const middle = Math.floor(inputValue.length / 2);
startDate = dateStringToDate(inputValue.slice(0, middle));
endDate = dateStringToDate(inputValue.slice(middle));
const firstPart = inputValue.slice(0, middle);
const secondPart = inputValue.slice(middle);
if (dateRegex.test(firstPart) && dateRegex.test(secondPart)) {
startDate = dateStringToDate(firstPart);
endDate = dateStringToDate(secondPart);
}
}

if (startDate && endDate && dateIsBefore(startDate, endDate, "date")) {
Expand Down