agentleFS
heryTzheryTz/skills/skills/engineering/tanstack-form/SKILL.md

tanstack-form

Skill1 starsChanged 6 months agoMITView on GitHub

Create or modify forms using TanStack Form's composition API. Uses formOptions() as the form shape primitive, withForm HOC for decomposing large forms into sections, withFieldGroup for reusable field groups across forms, Zod for validation, and i18n-prefixed error messages. Use when creating or modifying forms in the web app.

4 other projects use a version of this.

Adopted by

Most recently taken up by

  • Other versions4 projects use a version of this.

    Word for word · 0

    None found.

    Edited · 0

    None found.

    Wrote their own version · 4

    1. Kiranism/next-shadcn-dashboard-starter★ 7.2ksame name
    2. dvaJi/argos★ 3same name
    3. prdlk/skills★ 1same name
    4. LaoitdevOpen/laoitdev-skills★ 0same name

    How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.

  • In this repository4 other files it gives its agents.

The file

Files
SKILL.md

TanStack Form v2

For initial setup see setup.md. For withForm, withFieldGroup, and extendForm see composition.md. For array fields see arrays.md.

Rules

  1. Define form shape with formOptions() — share it into useAppForm and any withForm sub-components via spread
  2. Schemas live in the form file (or a sibling schema.ts), never inside field components
  3. Field components are dumb UI wrappers — they own no schema knowledge and no defaults
  4. onSubmit only calls mutation.mutate — side effects go in useMutation callbacks
  5. Wrap form.SubmitButton in form.AppForm
  6. Zod error messages: prefix with "i18n:" if project uses i18n (check package.json for react-i18next)
  7. Forms with 3+ distinct sections → split with withForm HOC
  8. Field groups reused across multiple forms → withFieldGroup HOC
  9. render in withForm/withFieldGroup must be a named function, not an arrow function (ESLint hooks rule)

Form Shape

formOptions() is the canonical primitive. Define it once; spread into useAppForm (and into withForm sub-components — see composition.md).

import { formOptions } from "@tanstack/react-form";
import { z } from "zod";

const schema = z.object({
  name: z.string().min(1, "i18n:validation.required"),
  email: z.string().email("i18n:validation.email"),
});

const opts = formOptions({
  defaultValues: { name: "", email: "" },
});

// ---

const mutation = useMutation({
  mutationFn: myServerFn,
  onSuccess: () => { /* redirect, invalidate */ },
});

const form = useAppForm({
  ...opts,
  validators: { onSubmit: schema },
  onSubmit: ({ value }) => mutation.mutate({ data: value }),
});
<form onSubmit={(e) => { e.preventDefault(); e.stopPropagation(); form.handleSubmit(); }}>
  <form.AppField name="name">{(field) => <field.InputField label="Name" />}</form.AppField>
  <form.AppField name="email">{(field) => <field.InputField label="Email" type="email" />}</form.AppField>
  {mutation.isError && <p className="text-sm text-destructive">Something went wrong.</p>}
  <form.AppForm>
    <form.SubmitButton isLoading={mutation.isPending}>Submit</form.SubmitButton>
  </form.AppForm>
</form>

Adding a Field

Field components are pure UI. They read from context, render the input, show errors. They know nothing about schemas or defaults.

1. Create <name>-field.tsx — import useFieldContext from form-context, not form-setup

import { useFieldContext } from "../form-context";
import { useFormatError } from "src/lib/use-format-error";

export function MyField({ label, ...props }: { label: string } & Omit<ComponentProps<typeof UiComp>, "value" | "onChange" | "onBlur">) {
  const field = useFieldContext<string>();
  const formatError = useFormatError();
  const error = field.state.meta.isValid ? undefined : formatError(field.state.meta.errors);
  return (
    <FormField label={label} error={error}>
      <UiComp {...props} id={field.name} value={field.state.value}
        onChange={(e) => field.handleChange(e.target.value)} onBlur={field.handleBlur} aria-invalid={!!error || undefined} />
    </FormField>
  );
}

2. Register in form-setup.ts fieldComponents

3. Use at call site — all UI props go here, never inside the field component

<form.AppField name="x">{(field) => <field.MyField label="X" placeholder="…" />}</form.AppField>

Common Mistakes

MistakeFix
Schema or defaults inside a field componentMove to the form file
const defaultValues = {} outside useAppFormUse formOptions()
Duplicating defaultValues in parent and withFormSpread opts in both
Server call in onSubmituseMutation, call mutation.mutate in onSubmit
SubmitButton unwrappedWrap in form.AppForm
Field imports useFieldContext from form-setupImport from form-context (avoids circular dep)
Arrow fn in withForm/withFieldGroup renderNamed fn: render: function Render({…}) {}
Missing stopPropagationAdd both preventDefault + stopPropagation

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.