|
| 1 | +import type { Meta, StoryObj } from "@storybook/react"; |
| 2 | +import { useForm, useFieldArray } from "react-hook-form"; |
| 3 | + |
| 4 | +import { BadgeContainer } from "../../../stories/utils"; |
| 5 | +import { Switch } from "@/components/ui/switch"; |
| 6 | +import { Input } from "@/components/ui/input"; |
| 7 | +import { Button } from "@/components/ui/button"; |
| 8 | +import { FormField } from "@/components/ui/form"; |
| 9 | + |
| 10 | +const meta = { |
| 11 | + title: "Modules/Transferrable", |
| 12 | + component: Component, |
| 13 | + parameters: { |
| 14 | + layout: "centered", |
| 15 | + }, |
| 16 | +} satisfies Meta<typeof Component>; |
| 17 | + |
| 18 | +export default meta; |
| 19 | +type Story = StoryObj<typeof meta>; |
| 20 | + |
| 21 | +export const AllVariants: Story = { |
| 22 | + args: {}, |
| 23 | +}; |
| 24 | + |
| 25 | +function Component() { |
| 26 | + |
| 27 | + return ( |
| 28 | + <div className="flex min-h-screen flex-col gap-6 bg-background p-6 text-foregroun"> |
| 29 | + <Transferrable /> |
| 30 | + </div> |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +function Transferrable() { |
| 35 | + const { handleSubmit, watch, formState: { errors }, control } = useForm({ |
| 36 | + defaultValues: { |
| 37 | + transferAllowlist: [{ address: "" }], |
| 38 | + limitTransfers: false, |
| 39 | + } |
| 40 | + }); |
| 41 | + const { fields, append, prepend, remove, swap, move, insert } = useFieldArray({ |
| 42 | + control, // control props comes from useForm (optional: if you are using FormProvider) |
| 43 | + name: "transferAllowlist", // unique name for your Field Array |
| 44 | + }); |
| 45 | + |
| 46 | + const onSubmit = (data: any) => { |
| 47 | + } |
| 48 | + |
| 49 | + const limitTransfers = watch("limitTransfers"); |
| 50 | + |
| 51 | + return ( |
| 52 | + <form className="flex flex-col p-6 border rounded-xl gap-y-4" onSubmit={handleSubmit(onSubmit)}> |
| 53 | + <div className="flex justify-between"> |
| 54 | + <h1 className="text-2xl font-bold">Transferrable</h1> |
| 55 | + |
| 56 | + <div className="flex gap-4 items-center"> |
| 57 | + <p>Limit Transfers</p> |
| 58 | + <FormField |
| 59 | + control={control} |
| 60 | + name="limitTransfers" |
| 61 | + render={({ field }) => ( |
| 62 | + <Switch |
| 63 | + checked={field.value} |
| 64 | + onCheckedChange={field.onChange} |
| 65 | + /> |
| 66 | + )} |
| 67 | + /> |
| 68 | + </div> |
| 69 | + </div> |
| 70 | + |
| 71 | + <div className={`flex flex-col gap-y-4 ${limitTransfers ? "opacity-50" : ""}`}> |
| 72 | + <p>Allow the following accounts to transfer assets</p> |
| 73 | + <div className="flex flex-col gap-y-6"> |
| 74 | + <div className="flex flex-col gap-y-2"> |
| 75 | + {fields.map((field, index) => ( |
| 76 | + <div className="flex gap-2 items-center" key={field.id}> |
| 77 | + <FormField |
| 78 | + control={control} |
| 79 | + name={`transferAllowlist.${index}.address`} |
| 80 | + disabled={limitTransfers} |
| 81 | + render={({ field: { name, ..._field } }) => ( |
| 82 | + <Input placeholder="0x..." name={`transferAllowlist.${index}.address`} {..._field} /> |
| 83 | + )} |
| 84 | + /> |
| 85 | + <Button variant="outline" onClick={() => remove(index)} disabled={limitTransfers}> |
| 86 | + Remove |
| 87 | + </Button> |
| 88 | + </div> |
| 89 | + ))} |
| 90 | + </div> |
| 91 | + <div className="flex flex-col justify-center items-center"> |
| 92 | + <Button variant="outline" onClick={() => append({ address: "" })} disabled={limitTransfers}> |
| 93 | + Add Address |
| 94 | + </Button> |
| 95 | + <Button variant="ghost" size="sm" className="text-xs hover:bg-inherit" disabled={limitTransfers}> |
| 96 | + Upload CSV |
| 97 | + </Button> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + </div> |
| 101 | + |
| 102 | + <div className="flex flex-col items-end gap-y-2"> |
| 103 | + <Button type="submit" className="self-end" > |
| 104 | + Submit |
| 105 | + </Button> |
| 106 | + {Object.keys(errors).length > 0 && ( |
| 107 | + <p className="text-red-500 font-light"> |
| 108 | + An has occured, please contact support |
| 109 | + </p> |
| 110 | + )} |
| 111 | + </div> |
| 112 | + </form > |
| 113 | + ) |
| 114 | +} |
| 115 | + |
0 commit comments