| 1 | "use client"; |
| 2 | |
| 3 | import { zodResolver } from |
| 4 | import { Controller, useForm |
| 5 | |
| 6 | import { Button } |
| 7 | import { |
| 8 | Field, |
| 9 | FieldError, |
| 10 | FieldGroup, |
| 11 | FieldLabel, |
| 12 | } from "@/components/ui/field"; |
| 13 | import { Input } |
| 14 | import { Spinner } |
| 15 | import type { IssueLabel |
| 16 | import { type CreateLabelInput, |
| 17 | |
| 18 | const DEFAULT_COLOR = " |
| 19 | |
| 20 | /** Create and edit share every field, so both go through this form. Colors are stored as 6 hex chars; the native picker is the only place `#` appears. */ |
| 21 | export function LabelForm({ |
| 22 | id, |
| 23 | label, |
| 24 | submitText, |
| 25 | pending, |
| 26 | onSubmit, |
| 27 | onCancel, |
| 28 | }: { |
| 29 | id: string |
| 30 | label?: IssueLabel |
| 31 | submitText: string |
| 32 | pending: boolean |
| 33 | onSubmit: (values |
| 34 | onCancel: () |
| 35 | }) { |
| 36 | const { |
| 37 | control, |
| 38 | register, |
| 39 | handleSubmit, |
| 40 | formState: { errors |
| 41 | } = useForm<CreateLabelInput |
| 42 | resolver: zodResolver(createLabelSchema |
| 43 | defaultValues: { |
| 44 | name: label?.name |
| 45 | description: label?.description |
| 46 | color: label?.color |
| 47 | }, |
| 48 | }); |
| 49 | |
| 50 | return ( |
| 51 | <form onSubmit={handleSubmit |
| 52 | <FieldGroup className=" |
| 53 | <Field> |
| 54 | <FieldLabel htmlFor={ |
| 55 | <Input |
| 56 | id={`${id |
| 57 | placeholder="bug" |
| 58 | autoComplete="off" |
| 59 | maxLength={50} |
| 60 | aria-invalid={Boolean(errors |
| 61 | {...register("name |
| 62 | /> |
| 63 | <FieldError errors={ |
| 64 | </Field> |
| 65 | |
| 66 | <Field> |
| 67 | <FieldLabel htmlFor={ |
| 68 | Description |
| 69 | <span className="font-normal text-muted-foreground |
| 70 | (optional) |
| 71 | </span> |
| 72 | </FieldLabel> |
| 73 | <Input |
| 74 | id={`${id |
| 75 | placeholder="Something is not working" |
| 76 | autoComplete="off" |
| 77 | maxLength={100} |
| 78 | aria-invalid={Boolean(errors |
| 79 | {...register("description |
| 80 | /> |
| 81 | <FieldError errors={ |
| 82 | </Field> |
| 83 | |
| 84 | <Field orientation=" |
| 85 | <FieldLabel htmlFor={ |
| 86 | <Controller |
| 87 | control={control} |
| 88 | name="color" |
| 89 | render={({ field |
| 90 | <input |
| 91 | id={`${id |
| 92 | type="color" |
| 93 | className="h-9 w-14 cursor-pointer rounded-md border bg-transparent p-1" |
| 94 | ref={field.ref |
| 95 | value={`#${field |
| 96 | onBlur={field.onBlur |
| 97 | onChange={(event) |
| 98 | field.onChange(event |
| 99 | } |
| 100 | /> |
| 101 | )} |
| 102 | /> |
| 103 | <FieldError errors={ |
| 104 | </Field> |
| 105 | |
| 106 | <div className="flex justify-end gap-2 |
| 107 | <Button type=" |
| 108 | Cancel |
| 109 | </Button> |
| 110 | <Button type=" |
| 111 | {pending && <Spinner |
| 112 | {submitText} |
| 113 | </Button> |
| 114 | </div> |
| 115 | </FieldGroup> |
| 116 | </form> |
| 117 | ); |
| 118 | } |