"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import * as z from "zod";
import * as React from "react";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { toast } from "@/components/ui/use-toast";
import { LoadingButton } from "@/components/ui/loading-button";
import { InlineCode } from "@/components/ui/inline-code";
import { DateTimePicker } from "@/components/ui/datetime-picker";
const FormSchema = z.object({
datetime: z.date().optional(),
});
const DEFAULT_VALUE = {
datetime: undefined,
};
const DatetimePickerForm = () => {
const form = useForm<z.infer<typeof FormSchema>>({
defaultValues: DEFAULT_VALUE,
resolver: zodResolver(FormSchema),
});
const [loading, setLoading] = React.useState(false);
function onSubmit(data: z.infer<typeof FormSchema>) {
setLoading(true);
setTimeout(() => {
setLoading(false);
toast({
title: "Your submitted data",
description: (
<>
<p className="text-red-600">
It is a <InlineCode>Date</InlineCode> object, the{" "}
<InlineCode>JSON.stringify</InlineCode> will show 0+ timezone. You
need to parse to your timezone to match your needs.
</p>
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
<code className="text-white">
{JSON.stringify(data, null, 2)}
</code>
</pre>
</>
),
});
}, 500);
}
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="w-2/3 space-y-6">
<FormField
control={form.control}
name="datetime"
render={({ field }) => (
<FormItem className="flex w-72 flex-col gap-2">
<FormLabel htmlFor="datetime">Date time</FormLabel>
<FormControl>
<DateTimePicker value={field.value} onChange={field.onChange} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<LoadingButton loading={loading} type="submit">
Submit
</LoadingButton>
</form>
</Form>
);
};
export default DatetimePickerForm;