mirror of
https://github.com/supabase/supabase.git
synced 2026-05-06 08:56:46 -04:00
0facd341a6
## Problem We used to have a `_Shadcn_` suffix for all the shadcn form components because we also had `formik` form components. This is not needed anymore. ## Solution - Remove the suffix - Update all usages
92 lines
2.7 KiB
TypeScript
92 lines
2.7 KiB
TypeScript
'use client'
|
|
|
|
import { zodResolver } from '@hookform/resolvers/zod'
|
|
import { format } from 'date-fns'
|
|
import { CalendarIcon } from 'lucide-react'
|
|
import { useForm } from 'react-hook-form'
|
|
import { toast } from 'sonner'
|
|
import {
|
|
Button,
|
|
Calendar,
|
|
Form,
|
|
FormControl,
|
|
FormDescription,
|
|
FormField,
|
|
FormItem,
|
|
FormLabel,
|
|
FormMessage,
|
|
Popover_Shadcn_,
|
|
PopoverContent_Shadcn_,
|
|
PopoverTrigger_Shadcn_,
|
|
} from 'ui'
|
|
import { z } from 'zod'
|
|
|
|
import { cn } from '@/lib/utils'
|
|
|
|
const FormSchema = z.object({
|
|
dob: z.date({
|
|
required_error: 'A date of birth is required.',
|
|
}),
|
|
})
|
|
|
|
export default function CalendarForm() {
|
|
const form = useForm<z.infer<typeof FormSchema>>({
|
|
resolver: zodResolver(FormSchema),
|
|
})
|
|
|
|
function onSubmit(data: z.infer<typeof FormSchema>) {
|
|
toast('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 onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
|
|
<FormField
|
|
control={form.control}
|
|
name="dob"
|
|
render={({ field }) => (
|
|
<FormItem className="flex flex-col">
|
|
<FormLabel>Date of birth</FormLabel>
|
|
<Popover_Shadcn_>
|
|
<PopoverTrigger_Shadcn_ asChild>
|
|
<FormControl>
|
|
<Button
|
|
type={'default'}
|
|
size="small"
|
|
className={cn(
|
|
'w-[240px] justify-start',
|
|
!field.value && 'text-muted-foreground'
|
|
)}
|
|
icon={<CalendarIcon className="ml-auto opacity-50" />}
|
|
>
|
|
{field.value ? format(field.value, 'PPP') : <span>Pick a date</span>}
|
|
</Button>
|
|
</FormControl>
|
|
</PopoverTrigger_Shadcn_>
|
|
<PopoverContent_Shadcn_ className="w-auto p-0" align="start" side="right">
|
|
<Calendar
|
|
mode="single"
|
|
selected={field.value}
|
|
onSelect={field.onChange}
|
|
disabled={(date) => date > new Date() || date < new Date('1900-01-01')}
|
|
initialFocus
|
|
/>
|
|
</PopoverContent_Shadcn_>
|
|
</Popover_Shadcn_>
|
|
<FormDescription>Your date of birth is used to calculate your age.</FormDescription>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<Button htmlType="submit">Submit</Button>
|
|
</form>
|
|
</Form>
|
|
)
|
|
}
|