import { useField, useSelector, type AnyFieldApi, type AnyFormApi, } from "@tanstack/react-form"; import { Option, Schema } from "effect"; import { Eye, EyeOff } from "lucide-react"; import { useRef, useState, type ReactNode } from "react"; import { Button } from "./components/button.js"; import { UploadQueue } from "./blocks/upload-queue.js"; import { Checkbox } from "./components/checkbox.js"; import { Combobox, ComboboxTrigger } from "./components/combobox.js"; import { Field, FieldSet, FormSection, NumberField, } from "./components/field.js"; import { CheckKey, CheckKeys, IconCheckbox, } from "./components/icon-checkbox.js"; import { IconRadio } from "./components/icon-radio.js"; import { Input } from "./components/input.js"; import { InputGroup, InputGroupAddon } from "./components/input-group.js"; import { RadioGroup, RadioGroupCard, RadioGroupItem, } from "./components/radio-group.js"; import { Select } from "./components/select.js"; import { Switch } from "./components/switch.js"; import { Textarea } from "./components/textarea.js"; import { VisuallyHidden } from "./components/visually-hidden.js"; import { fieldError } from "./form-error.js"; import type { FieldInput, FormValues, ResolvedField, ResolvedSection, } from "./form-schema.js"; import type { SchemaFormModel } from "./schema-form.js"; // TanStack hands field values back untyped; each control reads its own kind. const readText = Schema.decodeUnknownOption(Schema.String); const readFlag = Schema.decodeUnknownOption(Schema.Boolean); const readAmount = Schema.decodeUnknownOption(Schema.NullOr(Schema.Finite)); const readTexts = Schema.decodeUnknownOption(Schema.Array(Schema.String)); const filesSchema = Schema.Array(Schema.instanceOf(File)); const readFiles = Schema.decodeUnknownOption(filesSchema); const inputSchema = Schema.Union([ Schema.String, Schema.Finite, Schema.Boolean, Schema.Null, Schema.Undefined, Schema.Array(Schema.String), filesSchema, ]); const readInput = Schema.decodeUnknownOption(inputSchema); const text = (field: AnyFieldApi): string => Option.getOrElse(readText(field.state.value), () => ""); const flag = (field: AnyFieldApi): boolean => Option.getOrElse(readFlag(field.state.value), () => false); const amount = (field: AnyFieldApi): number | null => Option.getOrElse(readAmount(field.state.value), () => null); const texts = (field: AnyFieldApi): readonly string[] => Option.getOrElse(readTexts(field.state.value), (): readonly string[] => []); const files = (field: AnyFieldApi): readonly File[] => Option.getOrElse(readFiles(field.state.value), (): readonly File[] => []); const input = (field: AnyFieldApi): FieldInput => Option.getOrElse(readInput(field.state.value), (): FieldInput => null); /** What Field wires onto its control: id, description, error and required. */ type ControlProps = { id?: string; "aria-describedby"?: string; "aria-invalid"?: boolean | "true" | "false" | "grammar" | "spelling"; "aria-required"?: boolean | "true" | "false"; required?: boolean; }; type ControlState = { field: AnyFieldApi; spec: ResolvedField; locked: boolean; }; function SecretInput({ field, spec, locked, ...control }: ControlState & ControlProps) { const [shown, setShown] = useState(false); return ( { field.handleChange(event.currentTarget.value); }} /> ); } const inputTypes = { text: "text", email: "email", url: "url", tel: "tel" }; /** The picker supplies File objects; queue rows use only their metadata. */ function FileInput({ spec, field, locked, ...control }: ControlState & ControlProps) { const selected = files(field); const disabled = locked || spec.readOnly === true; const picker = useRef(null); return (
{ field.handleChange(Array.from(event.currentTarget.files ?? [])); // The queue owns the selection display, including resets and removal. // Clear the native picker so choosing the same file fires another change. event.currentTarget.value = ""; }} /> ({ id: String(index), name: file.name, preview: { kind: "file" }, progress: 0, progressLabel: `${String(file.size)} bytes`, statusLabel: "Selected", actions: disabled ? {} : { remove: { label: "Remove", accessibleLabel: `Remove ${file.name}`, }, }, }))} onRemove={(id) => { field.handleChange( selected.filter((_, index) => String(index) !== id), ); }} onCancel={() => undefined} onRetry={() => undefined} />
); } /** One control, wired by the Field around it (Field clones these props in). */ function SpecControl({ spec, field, locked, ...control }: ControlState & ControlProps): ReactNode { const setText = (value: string) => { field.handleChange(value); }; const shared = { ...control, name: spec.name, disabled: locked, onBlur: field.handleBlur, }; switch (spec.kind) { case "text": case "email": case "url": case "tel": return ( { setText(event.currentTarget.value); }} /> ); case "textarea": return (