Select Field
Dropdown select component integrated with TanStack Form. Options are defined as Record<string, string> where keys are labels.
npx shadcn@latest add https://shuip.plvo.dev/r/tsf-select-field.json
Preview
Loading...
Dropdowns present a list of options in a compact UI. SelectField
uses Radix UI's Select primitive wrapped with TanStack Form state management. The options
prop accepts a Record<string, string>
where keys are the display labels and values are what gets stored in the form state.
This format makes it simple to define options inline or map from API responses. For dependent selects (e.g., country → states), use useStore
to watch one field and update another field's options dynamically with setFieldValue
.
Built-in features
- Record-based options for clean label/value mapping
- Dynamic options via state or API data
- Dependent selects with
form.useStore
andsetFieldValue
- Empty state placeholder via
placeholder
prop
Options format
const options = {'Display Label 1': 'value1','Display Label 2': 'value2',}<SelectFieldform={form}name='choice'options={options}label='Select an option'placeholder='Choose...'/>
Dependent selects
const [states, setStates] = React.useState<Record<string, string>>({})// Watch country changesform.useStore((state) => state.values.country, {onChange: (country) => {setStates(country === 'us' ? { 'California': 'ca', 'Texas': 'tx' } : {})form.setFieldValue('state', '') // Reset state field}})<SelectField form={form} name='country' options={{ 'United States': 'us' }} label='Country' /><SelectField form={form} name='state' options={states} label='State' placeholder='Select country first' />
Examples
Conditional
Loading...
Dependent
Loading...
Dynamic
Loading categories...
Default
Loading...
Props
Name | Type | Description |
---|---|---|
form | ReactFormApi<TFormData> | Form instance from useForm hook |
name | DeepKeys<TFormData> | Type-safe field name (supports nested paths like "user.country") |
options | Record<string, string> | Options as key-value pairs where key is the label and value is the field value |
label | string? | Field label text |
placeholder | string? | Placeholder text shown when no value is selected |
description | string? | Helper text displayed below the select |
formProps | Partial<FieldOptions>? | TanStack Form field options (validators, etc.) |
fieldProps | React.ComponentProps<typeof Field>? | Props passed to the Field wrapper component |
props | React.ComponentProps<typeof Select>? | Props passed to the Select component |