|
| 1 | +import { Button } from "@/components/ui/button"; |
| 2 | +import { |
| 3 | + FormControl, |
| 4 | + FormField, |
| 5 | + FormItem, |
| 6 | + FormMessage, |
| 7 | +} from "@/components/ui/form"; |
| 8 | +import { Input } from "@/components/ui/input"; |
| 9 | +import { BanIcon, PlusIcon, Trash2Icon } from "lucide-react"; |
| 10 | +import { |
| 11 | + type ArrayPath, |
| 12 | + type FieldValues, |
| 13 | + type UseFormReturn, |
| 14 | + useFieldArray, |
| 15 | +} from "react-hook-form"; |
| 16 | + |
| 17 | +type OptionalPropertiesInput = { |
| 18 | + [key: string]: string | number; |
| 19 | +}; |
| 20 | + |
| 21 | +interface IPropertyFieldValues extends FieldValues { |
| 22 | + attributes?: OptionalPropertiesInput; |
| 23 | +} |
| 24 | + |
| 25 | +export function PropertiesFormControl< |
| 26 | + TFieldValues extends IPropertyFieldValues, |
| 27 | +>({ |
| 28 | + form, |
| 29 | +}: { |
| 30 | + form: UseFormReturn<TFieldValues>; |
| 31 | +}) { |
| 32 | + const { fields, append, remove, replace } = useFieldArray({ |
| 33 | + control: form.control, |
| 34 | + name: "attributes" as ArrayPath<TFieldValues>, |
| 35 | + }); |
| 36 | + |
| 37 | + return ( |
| 38 | + <div className="flex flex-col gap-4"> |
| 39 | + <div className="flex items-center justify-between gap-2"> |
| 40 | + <p>Attributes</p> |
| 41 | + <Button |
| 42 | + className="flex items-center gap-2" |
| 43 | + variant="destructive" |
| 44 | + size="sm" |
| 45 | + // biome-ignore lint/suspicious/noExplicitAny: FIXME |
| 46 | + onClick={() => |
| 47 | + form.setValue("attributes", [{ trait_type: "", value: "" } as any]) |
| 48 | + } |
| 49 | + > |
| 50 | + Reset |
| 51 | + <BanIcon className="size-4" /> |
| 52 | + </Button> |
| 53 | + </div> |
| 54 | + |
| 55 | + <div className="flex flex-col gap-3"> |
| 56 | + {/* Addresses */} |
| 57 | + {fields.map((fieldItem, index) => ( |
| 58 | + <div className="flex items-start gap-3" key={fieldItem.id}> |
| 59 | + <FormField |
| 60 | + control={form.control} |
| 61 | + name={`attributes.${index}.trait_type`} |
| 62 | + render={({ field }) => ( |
| 63 | + <FormItem className="grow"> |
| 64 | + <FormControl> |
| 65 | + <Input placeholder="0x..." {...field} /> |
| 66 | + </FormControl> |
| 67 | + <FormMessage /> |
| 68 | + </FormItem> |
| 69 | + )} |
| 70 | + /> |
| 71 | + |
| 72 | + <FormField |
| 73 | + control={form.control} |
| 74 | + name={`attributes.${index}.value`} |
| 75 | + render={({ field }) => ( |
| 76 | + <FormItem className="grow"> |
| 77 | + <FormControl> |
| 78 | + <Input placeholder="0x..." {...field} /> |
| 79 | + </FormControl> |
| 80 | + <FormMessage /> |
| 81 | + </FormItem> |
| 82 | + )} |
| 83 | + /> |
| 84 | + |
| 85 | + <Button |
| 86 | + variant="outline" |
| 87 | + className="!text-destructive-text bg-background" |
| 88 | + onClick={() => remove(index)} |
| 89 | + > |
| 90 | + <Trash2Icon className="size-4" /> |
| 91 | + </Button> |
| 92 | + </div> |
| 93 | + ))} |
| 94 | + </div> |
| 95 | + |
| 96 | + <div className="flex flex-row gap-2"> |
| 97 | + <Button |
| 98 | + size="sm" |
| 99 | + className="flex items-center gap-2" |
| 100 | + onClick={() => |
| 101 | + // biome-ignore lint/suspicious/noExplicitAny: FIXME |
| 102 | + append({ trait_type: undefined, value: undefined } as any) |
| 103 | + } |
| 104 | + > |
| 105 | + <PlusIcon className="size-5" /> |
| 106 | + Add Row |
| 107 | + </Button> |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + ); |
| 111 | +} |
0 commit comments