{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "forms-02",
  "description": "A create form in a panel that slides in from the side, with errors from the server shown on the field.",
  "registryDependencies": [
    "button",
    "field",
    "input",
    "https://www.dashboardblocks.com/r/radix/option-select.json",
    "textarea",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/forms.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/forms/forms-02.tsx",
      "content": "'use client'\n\nimport {\n  BlockReady,\n  BlockStateProvider,\n  BlockStateRows,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  type FormErrors,\n  FormSheet,\n  isEmail,\n  useSimpleForm,\n} from '@/registry/components/dashboardblocks/forms'\nimport { OptionSelect } from '@/registry/components/dashboardblocks/option-select'\nimport { IconPlaceholder } from '@/registry/icons/icon-placeholder'\nimport { startTransition, useState } from 'react'\n\nimport { Button } from '@/components/ui/button'\nimport { Field, FieldError, FieldGroup, FieldLabel } from '@/components/ui/field'\nimport { Input } from '@/components/ui/input'\nimport { Textarea } from '@/components/ui/textarea'\n\ntype NewCustomer = {\n  company: string\n  email: string\n  name: string\n  notes: string\n  plan: string\n}\n\ninterface Customer {\n  email: string\n  name: string\n  plan: string\n}\n\ninterface Forms2Props extends BlockStateProps {\n  /** Leave out, or pass none, to show the empty state. */\n  customers?: Customer[]\n  onCreate?: (values: NewCustomer) => Promise<FormErrors<NewCustomer> | void>\n  plans: string[]\n  title: string\n}\n\nconst exampleProps: Forms2Props = {\n  customers: [\n    { email: 'jonas@northwind.io', name: 'Jonas Weber', plan: 'Pro' },\n    { email: 'priya@lumen.dev', name: 'Priya Nair', plan: 'Enterprise' },\n    { email: 'mateo@fieldnote.co', name: 'Mateo Silva', plan: 'Free' },\n  ],\n  plans: ['Free', 'Pro', 'Enterprise'],\n  title: 'Customers',\n}\n\nasync function createCustomer(values: NewCustomer) {\n  await new Promise((resolve) => setTimeout(resolve, 800))\n  if (values.email.toLowerCase() === 'taken@example.com') {\n    return { email: 'A customer with this email already exists.' }\n  }\n}\n\nfunction validate(values: NewCustomer): FormErrors<NewCustomer> {\n  return {\n    email: !values.email.trim()\n      ? 'Enter an email address.'\n      : !isEmail(values.email)\n        ? 'Enter an email address like name@example.com.'\n        : undefined,\n    name: values.name.trim() ? undefined : 'Enter a name.',\n  }\n}\n\nconst Forms2 = (props: Forms2Props) => {\n  const {\n    customers: initialCustomers = [],\n    onCreate = createCustomer,\n    plans,\n    errorMessage,\n    status,\n    title,\n  } = props\n  const [customers, setCustomers] = useState(initialCustomers)\n  const [open, setOpen] = useState(false)\n  const emptyValues: NewCustomer = {\n    company: '',\n    email: '',\n    name: '',\n    notes: '',\n    plan: plans[0] ?? '',\n  }\n  const form = useSimpleForm({\n    defaultValues: emptyValues,\n    onSubmit: async (values) => {\n      const errors = await onCreate(values)\n      if (errors) return errors\n      startTransition(() => {\n        setCustomers((current) => [\n          { email: values.email, name: values.name, plan: values.plan },\n          ...current,\n        ])\n        setOpen(false)\n        form.reset(emptyValues)\n      })\n    },\n    validate,\n  })\n\n  return (\n    <BlockStateProvider\n      errorMessage={errorMessage}\n      state={getBlockState(status, customers)}\n    >\n      <section className='@container flex flex-col gap-3'>\n        <header className='flex items-start justify-between gap-3'>\n          <div className='flex min-w-0 flex-col gap-0.5'>\n            <h2 className='font-semibold tracking-tight'>{title}</h2>\n            <BlockReady className='text-sm'>\n              <p className='text-muted-foreground text-sm'>\n                {customers.length} {customers.length === 1 ? 'customer' : 'customers'},\n                newest first.\n              </p>\n            </BlockReady>\n          </div>\n          <Button\n            variant='outline'\n            size='sm'\n            disabled={status !== undefined}\n            onClick={() => setOpen(true)}\n          >\n            <IconPlaceholder\n              lucide='PlusIcon'\n              tabler='IconPlus'\n              hugeicons='PlusSignIcon'\n              phosphor='PlusIcon'\n              remixicon='RiAddLine'\n              data-icon='inline-start'\n            />\n            New customer\n          </Button>\n        </header>\n        <div className='border-t'>\n          <BlockStateRows rows={4} />\n          <ul className='divide-y empty:hidden'>\n            {!status &&\n              customers.map((customer) => (\n                <li\n                  key={customer.email}\n                  className='flex items-center justify-between gap-3 py-2.5'\n                >\n                  <div className='flex min-w-0 flex-col'>\n                    <span className='truncate text-sm font-medium'>{customer.name}</span>\n                    <span className='text-muted-foreground truncate text-xs'>\n                      {customer.email}\n                    </span>\n                  </div>\n                  <span className='text-muted-foreground shrink-0 text-sm'>\n                    {customer.plan}\n                  </span>\n                </li>\n              ))}\n          </ul>\n        </div>\n        <FormSheet\n          open={open}\n          onOpenChange={setOpen}\n          onSubmit={(event) => void form.submit(event)}\n          title='New customer'\n          description='Add a customer to your workspace. You can invite them later.'\n          footer={\n            <>\n              <Button type='submit' disabled={form.isSubmitting}>\n                {form.isSubmitting ? 'Creating' : 'Create customer'}\n              </Button>\n              <Button type='button' variant='outline' onClick={() => setOpen(false)}>\n                Cancel\n              </Button>\n            </>\n          }\n        >\n          <FieldGroup>\n            <Field data-invalid={form.errors.name ? true : undefined}>\n              <FieldLabel htmlFor={form.getId('name')}>Name</FieldLabel>\n              <Input autoComplete='off' {...form.field('name')} />\n              <FieldError {...form.error('name')} />\n            </Field>\n            <Field data-invalid={form.errors.email ? true : undefined}>\n              <FieldLabel htmlFor={form.getId('email')}>Email</FieldLabel>\n              <Input type='email' autoComplete='off' {...form.field('email')} />\n              <FieldError {...form.error('email')} />\n            </Field>\n            <Field>\n              <FieldLabel htmlFor={form.getId('company')}>Company</FieldLabel>\n              <Input {...form.field('company')} />\n            </Field>\n            <Field>\n              <FieldLabel htmlFor={form.getId('plan')}>Plan</FieldLabel>\n              <OptionSelect\n                className='w-full'\n                options={plans.map((plan) => ({ label: plan, value: plan }))}\n                {...form.select('plan')}\n              />\n            </Field>\n            <Field>\n              <FieldLabel htmlFor={form.getId('notes')}>Notes</FieldLabel>\n              <Textarea rows={4} {...form.field('notes')} />\n            </Field>\n          </FieldGroup>\n        </FormSheet>\n      </section>\n    </BlockStateProvider>\n  )\n}\n\nexport { Forms2, exampleProps as forms2ExampleProps, type Forms2Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/forms/forms-02.tsx"
    }
  ],
  "meta": {
    "shows": "A create form in a panel that slides in from the side, with errors from the server shown on the field.",
    "size": "half",
    "usedWith": [
      "data-table-02",
      "page-header-04"
    ],
    "component": "Forms2",
    "props": "{ customers?: { email: string; name: string; plan: string }[]; onCreate?: (values: { company: string; email: string; name: string; notes: string; plan: string }) => Promise<void | { name?: string; email?: string; company?: string; notes?: string; plan?: string }>; plans: string[]; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
