Skip to content
Nacre
Color palette
中文

Field

stableFieldOpen in playground
Field
Runs the real component in your browser.

One container for the label, required asterisk, description and error text; every form in the app is laid out with it. Vertical in dialogs and the right panel, horizontal on settings pages.

Status: stable · Components: Field

import { Field } from 'nacre'
import { signal } from 'nsh'
import { Field, TextField } from 'nacre'
export function FieldUsage() {
const name = signal('')
return (
<Field class="w-[260px] max-w-full" label="Customer name" required>
<TextField value={name()} onChange={(value) => name.set(value)} accessibilityLabel="Customer name" />
</Field>
)
}

The label sits on top; description goes below the control and is replaced by a warning icon and red text when there is an error. required adds a red asterisk before the label.

import { View, signal } from 'nsh'
import { Field, SelectTrigger, TextField } from 'nacre'
export function FieldVertical() {
const reason = signal('')
return (
<View class="w-[260px] max-w-full gap-4">
<Field label="Category" required description="Choose the most specific level">
<SelectTrigger block placeholder="Choose…" accessibilityLabel="Category" />
</Field>
<Field label="Reason" required error={reason() ? null : 'Enter a reason for reassigning'}>
<TextField
multiline
rows={2}
maxLength={200}
counter
value={reason()}
onChange={(value) => reason.set(value)}
invalid={!reason()}
accessibilityLabel="Reason"
/>
</Field>
</View>
)
}

action holds a field-level action (“Fill with AI”) and only shows in the vertical layout; size=“sm” is for the right panel and popovers (11pt label).

import { Button, Field, SelectTrigger } from 'nacre'
export function FieldAction() {
return (
<Field
class="w-[260px] max-w-full"
label="Status"
size="sm"
action={
<Button variant="secondary" size="xs" icon="sparkles">
Fill with AI
</Button>
}
>
<SelectTrigger block value="Resolved" accessibilityLabel="Status" />
</Field>
)
}

Set labelWidth=“flex” for a wrapping label and a trailing switch. Fixed-width labels default to 96pt and align help with the control.

import { View, signal } from 'nsh'
import { Field, Switch, TextField } from 'nacre'
export function FieldHorizontal() {
const nickname = signal('Jiayi')
const quiet = signal(true)
return (
<View testID="example-horizontal-fields" class="w-[420px] max-w-full gap-3">
<Field orientation="horizontal" label="Nickname">
<TextField size="md" value={nickname()} onChange={(value) => nickname.set(value)} accessibilityLabel="Nickname" />
</Field>
<Field orientation="horizontal" label="Employee ID" error="This employee ID is already taken">
<TextField size="md" value="A1024" invalid accessibilityLabel="Employee ID" />
</Field>
<Field orientation="horizontal" labelWidth="flex" label="Do not disturb">
<Switch testID="example-field-trailing-switch" checked={quiet()} onChange={(checked) => quiet.set(checked)} accessibilityLabel="Do not disturb" />
</Field>
</View>
)
}
  • NSH has no label-for: give the control an accessibilityLabel that matches the label.
  • Error text carries a warning icon, so errors aren’t shown by the control’s red border alone; also pass invalid to the control.

Do

  • Say how to fix it in the error (“Enter a reason for reassigning”)
  • Mark required fields with an asterisk, not “(required)”
  • Match the control’s accessibilityLabel to the label

Don’t

  • Put the description in the placeholder
  • Mix vertical and horizontal layouts in one form
  • Hand-write labels and error text outside Field

Property descriptions follow the Chinese source comments.

Prop Type Default Description
label string — 可见标签;TextField 自动继承,其余控件的 accessibilityLabel 用同一句。
required boolean — 必填:标签前一个红星(与网页版一致)
description string — 标签下的说明;有错误时被错误替换
error string | null — 错误文字:警示图标 + 红字,不只靠控件的红边(HIG:不只靠颜色);空串 / undefined 不显示
labelAccessory JSX.Element — 紧跟标签文字之后的小标记(「AI 已填」这类,不是操作)
action JSX.Element — 标签行右侧的字段级操作(「AI 填写」、计数);只在竖排时显示
orientation 'vertical' | 'horizontal' — vertical(默认):标签在上,表单与右栏 horizontal:标签在左定宽(默认 96)。手机放不下 160pt 控件时标签移到上方
labelWidth number | 'flex' — horizontal 时标签列宽;flex 让标签换行占用剩余宽度,控件贴右。
alignTop boolean — horizontal 时标签与控件首行对齐(多行控件),默认整行垂直居中
size 'sm' | 'md' — sm:右栏、浮层里(标签 11pt);md(默认):对话框、设置页(标签 12pt)
errorTestID string — 错误文字的 testID(容器不挂 testID:和里面控件的 testID 撞名)
class string —
children JSX.Element —

* required