agentleFS
LaoitdevOpenLaoitdevOpen/laoitdev-skills/skills/frontend/tanstack-form/SKILL.md

tanstack-form

Skill0 starsChanged 4 months agoMITView on GitHub

How to build forms using TanStack Form v1 with the shared useAppForm hook and App* field components. Use this whenever creating or modifying any form — create forms, update/edit forms, filter forms, or modal forms. Covers form setup, field components, Zod schema validation, form sections pattern, submit handling with confirmation dialogs, and filter forms that sync with URL.

4 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

5 rules. 0 are common, 1 is distinctive, and 4 are very specific to this project.

  • 0

    Common

    Most of the 3 edited versions we read keep these.

  • 1

    Distinctive

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Always use useAppForm, don't use useApp.

    line 12
  2. Very specific:

    Every form's DOM scope is <form.AppForm> wrapping a <form> element with

    line 13
  3. Very specific:

    Always render the submit action via <form.SubmitButton> — never a raw MUI <Button type="submit">.

    line 14
  4. Distinctive:

    Read the relevant file when you need it:

    line 18
  5. Very specific:

    All accessed via field.ComponentName inside form.AppField:

    line 86

“Common” is measured: kept by at least 6 in 10 of the 3 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Forms with TanStack Form

The project uses TanStack Form v1 with a shared useAppForm hook that pre-binds all form components. Always use this hook rather than raw useForm.

Core Invariants (always enforced — never violate)

  • Always use useAppForm, don't use useApp.
  • Every form's DOM scope is <form.AppForm> wrapping a <form> element with onSubmit={(e) => { e.preventDefault(); form.handleSubmit() }} (the existing <Box component="form"> examples already satisfy this — no code changes needed there, this just makes the structural rule explicit).
  • Always render the submit action via <form.SubmitButton> — never a raw MUI <Button type="submit">.

References

Read the relevant file when you need it:

  • references/form-composition.md — withForm, withFieldGroup, useTypedAppFormContext for multi-section or nested-object forms
  • references/focus-management.md — createOnSubmitInvalidFocusHandler, focusFirstInvalidField, how it works
  • references/filter-forms.md — URL-synced filter forms (Accordion + search params) + reactive/cascading fields

Imports

import { useAppForm, withForm, withFieldGroup, useTypedAppFormContext } from '@/shared/components/form'
import { createOnSubmitInvalidFocusHandler } from '@/shared/components/form'

Basic Form Setup

export function FeatureForm() {
  const { t } = useTranslation()
  const formRef = useRef<HTMLFormElement>(null)

  const form = useAppForm({
    defaultValues: {
      name: '' as string,
      count: 0,
      category_id: null as number | null,
    },
    validators: { onChange: yourZodSchema },
    onSubmitInvalid: createOnSubmitInvalidFocusHandler(() => formRef.current),
    onSubmit: async ({ value }) => {
      await mutation.mutateAsync(value)
    },
  })

  return (
    <form.AppForm>
      <Box
        component="form"
        ref={formRef}
        onSubmit={(e) => { e.preventDefault(); form.handleSubmit() }}
        noValidate
      >
        <form.AppField name="name">
          {(field) => <field.TextField label={t('feature.name')} required />}
        </form.AppField>

        <form.AppField name="count">
          {(field) => <field.TextFieldNumber label={t('feature.count')} />}
        </form.AppField>

        <form.SubmitButton
          label={t('common.save')}
          loading={mutation.isPending}
        />
      </Box>
    </form.AppForm>
  )
}

form.SubmitButton renders MUI's native loading prop under the hood — this shows a CircularProgress spinner and auto-disables the button; no separate loadingLabel text-swap is needed.


Available Field Components

All accessed via field.ComponentName inside form.AppField:

ComponentUse for
field.TextFieldText input
field.TextFieldNumberNumeric input
field.SelectDropdown with options: { value, label }[]
field.AutocompleteSearchable dropdown
field.AutocompleteInfinitePaginated searchable dropdown
field.DatePickerDate picker (Day.js)
field.DateTimePickerDate + time picker
field.CheckboxBoolean checkbox
field.SwitchToggle switch
field.ImageUploadFieldFile upload

TextField

<form.AppField name="description">
  {(field) => (
    <field.TextField label="Description" required multiline rows={4} placeholder="Enter..." />
  )}
</form.AppField>

Select

<form.AppField name="status">
  {(field) => (
    <field.Select
      label={t('feature.status')}
      options={STATUS_OPTIONS.map(opt => ({ value: opt.value, label: t(opt.label) }))}
      clearable
    />
  )}
</form.AppField>

Autocomplete

<form.AppField name="category_id">
  {(field) => (
    <field.Autocomplete
      label={t('feature.category')}
      options={categories}
      getOptionLabel={(opt) => opt.name}
      getOptionValue={(opt) => opt.id}
      multiple={false}
    />
  )}
</form.AppField>

Zod Schema with Translation

Schemas needing translated error messages are factory functions accepting t: TFunction:

import { z } from 'zod'
import type { TFunction } from 'i18next'
import { formOptions } from '@tanstack/react-form'

export const getFeatureCreateSchema = (t: TFunction) => z.object({
  name: z.string().min(1, t('feature.validation.nameRequired')).max(255),
  count: z.number().min(0),
  category_id: z.number().min(1, t('feature.validation.categoryRequired')),
  notes: z.string().nullable(),
})

export type FeatureCreateFormData = z.infer<ReturnType<typeof getFeatureCreateSchema>>

export const featureCreateFormOption = formOptions({
  defaultValues: {
    name: '',
    count: 0,
    category_id: null as number | null,
    notes: null as string | null,
  },
})

Usage:

const schema = getFeatureCreateSchema(t)
const form = useAppForm({
  defaultValues: featureCreateFormOption.defaultValues,
  validators: { onChange: schema },
})

Submit with Confirmation Dialog

const { confirm } = useConfirmDialog()

const form = useAppForm({
  onSubmit: async () => {
    const confirmed = await confirm({
      title: t('feature.confirmUpdate'),
      message: t('feature.confirmUpdateMessage'),
      confirmText: t('common.save'),
      cancelText: t('common.cancel'),
      confirmColor: 'primary',
      maxWidth: 'xs',
    })
    if (!confirmed) return

    mutation.mutate(form.state.values, {
      onSuccess: () => {
        showSuccess(t('feature.updateSuccess'))
        navigate({ to: '/feature/$featureId', params: { featureId: String(id) } })
      },
      onError: (error) => showError(translateError(error)),
    })
  },
})

Sticky Footer Submit Bar

<Box
  sx={{
    position: 'sticky', bottom: 0,
    bgcolor: 'background.paper',
    borderTop: '1px solid', borderColor: 'divider',
    p: 2, mt: 3, mx: -2,
  }}
>
  <Stack direction={{ xs: 'column', sm: 'row' }} spacing={2} justifyContent="flex-end">
    <Button variant="outlined" onClick={() => navigate({ to: '..' })}>
      {t('common.cancel')}
    </Button>
    <form.SubmitButton
      label={t('common.save')}
      loading={mutation.isPending}
    />
  </Stack>
</Box>

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.