openstatusHQ / time-picker

A simple shadcn/ui TimePicker component
https://time.openstatus.dev
MIT License
594 stars 18 forks source link

Usage in form #6

Closed Olivier-OG closed 6 months ago

Olivier-OG commented 7 months ago

Anyone has an example of how to use this with a shadcn form?

jaomuro commented 6 months ago

Hi ✌

Here is an example you can use, hope it helps. You just need to adjust the imports.

import { format } from "date-fns";
import { CalendarIcon } from "@radix-ui/react-icons";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import { Calendar } from "@/components/ui/calendar";
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/components/ui/popover";
import { TimePickerDemo } from "@/components/time-picker-demo";
import { Form, FormField, FormItem, FormLabel } from "./ui/form";
import { z } from "zod";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";

import { toast } from "@/components/ui/use-toast";

const formShema = z.object({
  dateAndTime: z.date(),
});

type formSchemaType = z.infer<typeof formShema>;

export function DateTimePickerDemoWithFormShadcnUI() {
  const form = useForm<formSchemaType>({
    resolver: zodResolver(formShema),
  });

  function onSubmit(data: formSchemaType) {
    toast({
      title: "You submitted the following values:",
      description: (
        <pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
          <code className="text-white">{JSON.stringify(data, null, 2)}</code>
        </pre>
      ),
    });
  }

  return (
    <Form {...form}>
      <form
        className="flex flex-col space-y-4"
        onSubmit={form.handleSubmit(onSubmit)}
      >
        <FormField
          control={form.control}
          name="dateAndTime"
          render={({ field }) => (
            <FormItem className="flex flex-col">
              <FormLabel>Date and time</FormLabel>
              <Popover>
                <PopoverTrigger asChild>
                  <Button
                    variant={"outline"}
                    className={cn(
                      "w-[280px] justify-start text-left font-normal",
                      !field.value && "text-muted-foreground"
                    )}
                  >
                    <CalendarIcon className="mr-2 h-4 w-4" />
                    {field.value ? (
                      format(field.value, "PPP HH:mm:ss")
                    ) : (
                      <span>Pick a date</span>
                    )}
                  </Button>
                </PopoverTrigger>
                <PopoverContent className="w-auto p-0">
                  <Calendar
                    mode="single"
                    selected={field.value}
                    onSelect={field.onChange}
                    initialFocus
                  />
                  <div className="p-3 border-t border-border">
                    <TimePickerDemo
                      setDate={field.onChange}
                      date={field.value}
                    />
                  </div>
                </PopoverContent>
              </Popover>
            </FormItem>
          )}
        />
        <Button type="submit">Submit</Button>
      </form>
    </Form>
  );
}
mxkaske commented 6 months ago

Thanks @jaomuro for the example! 🙌 The main example on the website is powered by the shared snippet.