Dependent Reset

When a parent field changes, clears only the dependent values it made invalid and names each one with Undo, so fields don't vanish silently.

StopsDependent fields cleared without notice

Installation

pnpm dlx shadcn@latest add https://bollard-ui.vercel.app/r/dependent-reset.json

The files land in your project. They're yours to edit.

Usage

dependent-reset-usage.tsx
import { DependentResetNotice, clearedFieldClass, useDependentReset, type DependentRules, } from "@/components/ui/dependent-reset"

<DependentResetNotice notice={form.notice} onUndo={form.undo} onDismiss={form.dismiss} />

What it catches

  • Dependent values that became invalid after a parent change
  • Partial loss (some options pruned) versus a field cleared entirely
  • Silent data loss, by naming each value and offering Undo

Props

DependentResetNotice props
PropTypeDefault
notice* (required)

The notice from `useDependentReset`.

ResetNotice<T> | null—
onUndo* (required)

Restore the change and everything it cleared.

() => void—
onDismiss* (required)

Close the notice and keep the new values.

() => void—
message

`{count}` ("2 fields") and `{cause}` ("Country") are filled in.

string"{count} cleared because you changed {cause}."
fieldWord

Word for one field and for several.

[singular: string, plural: string]["field", "fields"]
removedSuffix

Shown after the items a list lost.

string"removed"
clearedSuffix

Read to screen readers after a fully cleared value.

string"cleared"
undoLabel

Button labels.

string"Undo"
dismissLabel

Accessible label for the dismiss button.

string"Dismiss"

* required. Also accepts the native props of <div> (className, aria-*, data-*…), passed through to it.

Accessibility

  • The notice is role="status" with aria-live="polite"
  • Each lost field is described in text (e.g. "cleared" or "removed")
  • Dismiss has an aria-label

Only what the source does. Test with your own screen reader and content before shipping.

Limitations

  • You define which values are valid for each parent value
  • Undo restores the previous values in the form; it doesn't undo anything already saved

What it stops

Dependent fields cleared without notice

Also known as

dependent fields · cascading select · country state city · conditional fields · add-ons · plan picker · react hook form

Field type

Dependencies

Pairs well with