{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ds-form-builder",
  "title": "Form Builder",
  "description": "Gera um formulário validado a partir de um schema zod (validação) e uma lista de campos (rótulo, tipo de widget, opções) — react-hook-form + Field/FieldGroup.",
  "dependencies": [
    "react-hook-form",
    "zod",
    "@hookform/resolvers"
  ],
  "registryDependencies": [
    "@lema-ds/field",
    "@lema-ds/button",
    "@lema-ds/input",
    "@lema-ds/textarea",
    "@lema-ds/select",
    "@lema-ds/switch",
    "@lema-ds/checkbox",
    "@lema-ds/spinner"
  ],
  "files": [
    {
      "path": "components/ds/ds-form-builder.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { zodResolver } from \"@hookform/resolvers/zod\"\nimport { Controller, useForm, type FieldValues } from \"react-hook-form\"\nimport type { z } from \"zod\"\n\nimport { cn } from \"@/lib/utils\"\nimport { Button } from \"@/components/ui/button\"\nimport { Input } from \"@/components/ui/input\"\nimport { Textarea } from \"@/components/ui/textarea\"\nimport { Switch } from \"@/components/ui/switch\"\nimport { Checkbox } from \"@/components/ui/checkbox\"\nimport { Spinner } from \"@/components/ui/spinner\"\nimport {\n  Select,\n  SelectContent,\n  SelectGroup,\n  SelectItem,\n  SelectTrigger,\n  SelectValue,\n} from \"@/components/ui/select\"\nimport {\n  Field,\n  FieldContent,\n  FieldDescription,\n  FieldError,\n  FieldGroup,\n  FieldLabel,\n} from \"@/components/ui/field\"\n\n// ── Types ──\n\nexport type FormFieldType =\n  | \"text\"\n  | \"email\"\n  | \"password\"\n  | \"number\"\n  | \"textarea\"\n  | \"select\"\n  | \"checkbox\"\n  | \"switch\"\n\nexport interface FormFieldOption {\n  label: string\n  value: string\n}\n\nexport interface FormFieldConfig {\n  name: string\n  label: string\n  /** @default \"text\" */\n  type?: FormFieldType\n  placeholder?: string\n  description?: string\n  /** Required for `type: \"select\"`. */\n  options?: FormFieldOption[]\n}\n\nexport interface FormBuilderProps {\n  /** Validation only — field rendering comes from `fields`, not schema introspection. */\n  schema: z.ZodType<FieldValues, FieldValues>\n  fields: FormFieldConfig[]\n  defaultValues?: FieldValues\n  onSubmit: (values: FieldValues) => void | Promise<void>\n  submitLabel?: string\n  loading?: boolean\n  className?: string\n}\n\n// ── Variants ──\n\nexport const formBuilderVariants = undefined\n\n// ── Component ──\n\n/**\n * Generates a validated form from a zod `schema` (validation) plus a\n * `fields` list (rendering meta — label, widget type, options). Built on\n * `react-hook-form` + the project's `Field`/`FieldGroup` primitives, so\n * every field gets the standard `data-invalid`/`aria-invalid`/`FieldError`\n * wiring for free.\n */\nexport function FormBuilder({\n  schema,\n  fields,\n  defaultValues,\n  onSubmit,\n  submitLabel = \"Submit\",\n  loading = false,\n  className,\n}: FormBuilderProps) {\n  const {\n    control,\n    register,\n    handleSubmit,\n    formState: { errors, isSubmitting },\n  } = useForm({\n    resolver: zodResolver(schema),\n    defaultValues,\n  })\n\n  const busy = loading || isSubmitting\n\n  return (\n    <form\n      data-slot=\"form-builder\"\n      className={cn(\"w-full\", className)}\n      onSubmit={handleSubmit(onSubmit)}\n      noValidate\n    >\n      <FieldGroup>\n        {fields.map((field) => {\n          const fieldName = field.name\n          const error = errors[field.name]\n          const errorMessage =\n            error && typeof error.message === \"string\"\n              ? error.message\n              : undefined\n\n          if (field.type === \"select\") {\n            return (\n              <Controller\n                key={field.name}\n                control={control}\n                name={fieldName}\n                render={({ field: rhf }) => (\n                  <Field data-invalid={!!errorMessage}>\n                    <FieldLabel htmlFor={field.name}>{field.label}</FieldLabel>\n                    <Select\n                      value={(rhf.value as string) ?? \"\"}\n                      onValueChange={rhf.onChange}\n                    >\n                      <SelectTrigger\n                        id={field.name}\n                        className=\"w-full\"\n                        aria-invalid={!!errorMessage}\n                      >\n                        <SelectValue placeholder={field.placeholder} />\n                      </SelectTrigger>\n                      <SelectContent>\n                        <SelectGroup>\n                          {field.options?.map((option) => (\n                            <SelectItem key={option.value} value={option.value}>\n                              {option.label}\n                            </SelectItem>\n                          ))}\n                        </SelectGroup>\n                      </SelectContent>\n                    </Select>\n                    {field.description && !errorMessage && (\n                      <FieldDescription>{field.description}</FieldDescription>\n                    )}\n                    <FieldError\n                      errors={\n                        errorMessage ? [{ message: errorMessage }] : undefined\n                      }\n                    />\n                  </Field>\n                )}\n              />\n            )\n          }\n\n          if (field.type === \"checkbox\" || field.type === \"switch\") {\n            const ToggleControl = field.type === \"switch\" ? Switch : Checkbox\n            return (\n              <Controller\n                key={field.name}\n                control={control}\n                name={fieldName}\n                render={({ field: rhf }) => (\n                  <Field orientation=\"horizontal\" data-invalid={!!errorMessage}>\n                    <ToggleControl\n                      id={field.name}\n                      checked={!!rhf.value}\n                      onCheckedChange={rhf.onChange}\n                      aria-invalid={!!errorMessage}\n                    />\n                    <FieldContent>\n                      <FieldLabel htmlFor={field.name}>\n                        {field.label}\n                      </FieldLabel>\n                      {field.description && !errorMessage && (\n                        <FieldDescription>{field.description}</FieldDescription>\n                      )}\n                      <FieldError\n                        errors={\n                          errorMessage ? [{ message: errorMessage }] : undefined\n                        }\n                      />\n                    </FieldContent>\n                  </Field>\n                )}\n              />\n            )\n          }\n\n          const InputControl = field.type === \"textarea\" ? Textarea : Input\n\n          return (\n            <Field key={field.name} data-invalid={!!errorMessage}>\n              <FieldLabel htmlFor={field.name}>{field.label}</FieldLabel>\n              <InputControl\n                id={field.name}\n                type={\n                  field.type === \"textarea\" ? undefined : (field.type ?? \"text\")\n                }\n                placeholder={field.placeholder}\n                aria-invalid={!!errorMessage}\n                {...register(fieldName, {\n                  valueAsNumber: field.type === \"number\",\n                })}\n              />\n              {field.description && !errorMessage && (\n                <FieldDescription>{field.description}</FieldDescription>\n              )}\n              <FieldError\n                errors={errorMessage ? [{ message: errorMessage }] : undefined}\n              />\n            </Field>\n          )\n        })}\n\n        <Field orientation=\"horizontal\">\n          <Button type=\"submit\" disabled={busy}>\n            {busy && <Spinner data-icon=\"inline-start\" />}\n            {submitLabel}\n          </Button>\n        </Field>\n      </FieldGroup>\n    </form>\n  )\n}\n",
      "type": "registry:ui"
    }
  ],
  "type": "registry:ui"
}
