| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- import { z } from 'zod'
- import { useForm } from 'react-hook-form'
- import { zodResolver } from '@hookform/resolvers/zod'
- import { showSubmittedData } from '@/lib/show-submitted-data'
- import { Button } from '@/components/ui/button'
- import {
- Form,
- FormControl,
- FormField,
- FormItem,
- FormLabel,
- FormMessage,
- } from '@/components/ui/form'
- import { Input } from '@/components/ui/input'
- import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
- import {
- Sheet,
- SheetClose,
- SheetContent,
- SheetDescription,
- SheetFooter,
- SheetHeader,
- SheetTitle,
- } from '@/components/ui/sheet'
- import { SelectDropdown } from '@/components/select-dropdown'
- import { type Task } from '../data/schema'
- type TaskMutateDrawerProps = {
- open: boolean
- onOpenChange: (open: boolean) => void
- currentRow?: Task
- }
- const formSchema = z.object({
- title: z.string().min(1, 'Title is required.'),
- status: z.string().min(1, 'Please select a status.'),
- label: z.string().min(1, 'Please select a label.'),
- priority: z.string().min(1, 'Please choose a priority.'),
- })
- type TaskForm = z.infer<typeof formSchema>
- export function TasksMutateDrawer({
- open,
- onOpenChange,
- currentRow,
- }: TaskMutateDrawerProps) {
- const isUpdate = !!currentRow
- const form = useForm<TaskForm>({
- resolver: zodResolver(formSchema),
- defaultValues: currentRow ?? {
- title: '',
- status: '',
- label: '',
- priority: '',
- },
- })
- const onSubmit = (data: TaskForm) => {
- // do something with the form data
- onOpenChange(false)
- form.reset()
- showSubmittedData(data)
- }
- return (
- <Sheet
- open={open}
- onOpenChange={(v) => {
- onOpenChange(v)
- form.reset()
- }}
- >
- <SheetContent className='flex flex-col'>
- <SheetHeader className='text-start'>
- <SheetTitle>{isUpdate ? 'Update' : 'Create'} Task</SheetTitle>
- <SheetDescription>
- {isUpdate
- ? 'Update the task by providing necessary info.'
- : 'Add a new task by providing necessary info.'}
- Click save when you're done.
- </SheetDescription>
- </SheetHeader>
- <Form {...form}>
- <form
- id='tasks-form'
- onSubmit={form.handleSubmit(onSubmit)}
- className='flex-1 space-y-6 overflow-y-auto px-4'
- >
- <FormField
- control={form.control}
- name='title'
- render={({ field }) => (
- <FormItem>
- <FormLabel>Title</FormLabel>
- <FormControl>
- <Input {...field} placeholder='Enter a title' />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name='status'
- render={({ field }) => (
- <FormItem>
- <FormLabel>Status</FormLabel>
- <SelectDropdown
- defaultValue={field.value}
- onValueChange={field.onChange}
- placeholder='Select dropdown'
- items={[
- { label: 'In Progress', value: 'in progress' },
- { label: 'Backlog', value: 'backlog' },
- { label: 'Todo', value: 'todo' },
- { label: 'Canceled', value: 'canceled' },
- { label: 'Done', value: 'done' },
- ]}
- />
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name='label'
- render={({ field }) => (
- <FormItem className='relative'>
- <FormLabel>Label</FormLabel>
- <FormControl>
- <RadioGroup
- onValueChange={field.onChange}
- defaultValue={field.value}
- className='flex flex-col space-y-1'
- >
- <FormItem className='flex items-center'>
- <FormControl>
- <RadioGroupItem value='documentation' />
- </FormControl>
- <FormLabel className='font-normal'>
- Documentation
- </FormLabel>
- </FormItem>
- <FormItem className='flex items-center'>
- <FormControl>
- <RadioGroupItem value='feature' />
- </FormControl>
- <FormLabel className='font-normal'>Feature</FormLabel>
- </FormItem>
- <FormItem className='flex items-center'>
- <FormControl>
- <RadioGroupItem value='bug' />
- </FormControl>
- <FormLabel className='font-normal'>Bug</FormLabel>
- </FormItem>
- </RadioGroup>
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name='priority'
- render={({ field }) => (
- <FormItem className='relative'>
- <FormLabel>Priority</FormLabel>
- <FormControl>
- <RadioGroup
- onValueChange={field.onChange}
- defaultValue={field.value}
- className='flex flex-col space-y-1'
- >
- <FormItem className='flex items-center'>
- <FormControl>
- <RadioGroupItem value='high' />
- </FormControl>
- <FormLabel className='font-normal'>High</FormLabel>
- </FormItem>
- <FormItem className='flex items-center'>
- <FormControl>
- <RadioGroupItem value='medium' />
- </FormControl>
- <FormLabel className='font-normal'>Medium</FormLabel>
- </FormItem>
- <FormItem className='flex items-center'>
- <FormControl>
- <RadioGroupItem value='low' />
- </FormControl>
- <FormLabel className='font-normal'>Low</FormLabel>
- </FormItem>
- </RadioGroup>
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- </form>
- </Form>
- <SheetFooter className='gap-2'>
- <SheetClose asChild>
- <Button variant='outline'>Close</Button>
- </SheetClose>
- <Button form='tasks-form' type='submit'>
- Save changes
- </Button>
- </SheetFooter>
- </SheetContent>
- </Sheet>
- )
- }
|