You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
memos/web/src/components/DateTimeInput.tsx

46 lines
1.3 KiB
TypeScript

2 months ago
import dayjs from "dayjs";
import { isEqual } from "lodash-es";
import toast from "react-hot-toast";
import { cn } from "@/utils";
2 months ago
// convert Date to datetime string.
const formatDate = (date: Date | undefined): string => {
return dayjs(date).format("M/D/YYYY, H:mm:ss");
};
interface Props {
value: Date | undefined;
originalValue: Date | undefined;
onChange: (date: Date) => void;
}
const DateTimeInput: React.FC<Props> = ({ value, originalValue, onChange }) => {
return (
<input
type="text"
className={cn(
"px-1 bg-transparent rounded text-xs transition-all",
"border-transparent outline-none focus:border-gray-300 dark:focus:border-zinc-700",
!isEqual(value, originalValue) && "border-gray-300 dark:border-zinc-700",
"border",
)}
2 months ago
defaultValue={formatDate(value)}
onBlur={(e) => {
const inputValue = e.target.value;
if (inputValue) {
const date = new Date(inputValue);
if (!isNaN(date.getTime())) {
onChange(date);
} else {
2 months ago
toast.error("Invalid datetime format. Use format: 12/31/2023, 23:59:59");
e.target.value = formatDate(value);
}
}
}}
2 months ago
placeholder="M/D/YYYY, H:mm:ss"
/>
);
};
export default DateTimeInput;