Skip to content
Nacre
Color palette
中文

TextField

stableTextField · TouchTextAreaOpen in playground
TextField
Runs the real component in your browser.

Single-line or multiline input. Counts sit at the end of a single-line field or at the bottom right of a multiline field. Set counterWarnAt for a near-limit warning. Validation uses an error border and message. Use SearchField for search.

Status: stable · Components: TextField, TouchTextArea

import { TextField, TouchTextArea } from 'nacre'
import { signal } from 'nsh'
import { TextField, TOUCH } from 'nacre'
export function TextFieldUsage() {
const name = signal('')
return <TextField class={TOUCH ? 'w-full' : 'w-[240px] max-w-full'} value={name()} onChange={(value) => name.set(value)} placeholder="Customer name" accessibilityLabel="Customer name" />
}

Keep the field label and character count visible. maxLength limits how many characters can be entered.

import { signal } from 'nsh'
import { TouchGroup, TouchTextArea } from 'nacre'
export function TextFieldTouchNote() {
const note = signal('')
return <TouchGroup>
<TouchTextArea testID="demo-touch-note" label="Note" maxLength={120} value={note()} onChange={(v) => note.set(v)} />
</TouchGroup>
}

Requests NSH’s native phone-pad keyboard. The dial variant uses a 40pt field and large tabular digits. Formatting, validation and dialing remain with the caller.

Phone: On phones the dial field is 44 tall like other inputs, with 17 pt tabular digits.

import { signal } from 'nsh'
import { TextField } from 'nacre'
export function TextFieldDial() {
const number = signal('+852')
return <TextField variant="dial" keyboardType="phone-pad" autoCorrect={false} value={number()} onChange={(value) => number.set(value)} accessibilityLabel="Phone number" />
}

Editable, error (invalid) and disabled. Disabled: 40% opacity, not editable, and not in the Tab order; pair an error with a Field that shows the error text.

import { View, signal } from 'nsh'
import { TextField } from 'nacre'
export function TextFieldStates() {
const name = signal('')
return (
<View class="flex-row flex-wrap items-center gap-2">
<TextField class="w-[200px] max-w-full" size="md" value={name()} onChange={(value) => name.set(value)} placeholder="Click to see focus" accessibilityLabel="Example" />
<TextField class="w-[200px] max-w-full" size="md" value="138 0000" invalid accessibilityLabel="Error" />
<TextField class="w-[200px] max-w-full" size="md" value="Disabled" disabled accessibilityLabel="Disabled" />
</View>
)
}

The counter and native maxLength both count Unicode code points, so 😀 counts as one. counter with maxLength shows the count at the end of a single line or below multiline input. counterWarnAt is an absolute count: this field changes color at 180/200 without rejecting input.

import { signal } from 'nsh'
import { TextField } from 'nacre'
export function TextFieldMultiline() {
const note = signal('Identity verified')
return (
<TextField
testID="example-notes-field"
class="w-[320px] max-w-full"
multiline
rows={3}
maxLength={200}
counter
counterWarnAt={180}
value={note()}
onChange={(value) => note.set(value)}
placeholder="Write a chat note"
accessibilityLabel="Chat notes"
/>
)
}

xs 24 / sm 28 (default) / md 32 / lg 36; dialog forms use md, and controls in one row share one size.

Phone: On phones: xs 32 / sm 44 (default) / md 48 / lg 52 with 16 pt text, a light gray fill with no idle border, switching to a white fill and accent outline on focus.

import { View } from 'nsh'
import { TextField } from 'nacre'
export function TextFieldSizes() {
return (
<View class="gap-2">
{(['xs', 'sm', 'md', 'lg'] as const).map((size) => (
<TextField testID={`demo-textfield-${size}`} class="w-[200px] max-w-full" size={size} placeholder="Enter text" accessibilityLabel={`$Enter text ${size}`} />
))}
</View>
)
}

leading / trailing hold an icon, a unit or a small button; the focus ring wraps the whole field.

import { Text, View, signal } from 'nsh'
import { Icon, TextField, TEXT_SIZE } from 'nacre'
export function TextFieldAffixes() {
const amount = signal('5000')
const email = signal('')
return (
<View class="flex-row flex-wrap items-center gap-2">
<TextField testID="example-counted-name" class="w-[200px]" defaultValue="Sam" maxLength={12} counter
accessibilityLabel="Display name" />
<TextField
class="w-[200px] max-w-full"
value={amount()}
onChange={(value) => amount.set(value)}
accessibilityLabel="Deposit amount"
trailing={<Text class={`${TEXT_SIZE.small} text-muted-foreground`}>HKD</Text>}
/>
<TextField
class="w-[220px] max-w-full"
value={email()}
onChange={(value) => email.set(value)}
placeholder="Customer email"
accessibilityLabel="Customer email"
leading={<Icon name="mail" size={14} color="muted-foreground" />}
/>
</View>
)
}
Key Action
Tab Focus the field (disabled fields are skipped)
Return Single-line: submit (onSubmit); multiline: new line
  • accessibilityLabel is required when there is no visible label; inside a Field, make it match the label (NSH has no label-for).
  • Don’t rely on a red border alone for errors: use Field’s error for a warning icon plus text.
  • Disabled: 40% opacity and not editable; announced as disabled and not in the Tab order.

Do

  • Pair it with Field for a label and error text
  • Give accessibilityLabel when there is no visible label
  • Turn on the count for multiline input with a length limit

Don’t

  • Use a placeholder instead of a label
  • Show errors with color alone
  • Use it for search (use SearchField)

Property descriptions follow the Chinese source comments.

Prop Type Default Description
value string —
defaultValue string —
onChange (value: string) => void —
onSubmit (value: string) => void —
submitBehavior 'submit' | 'blurAndSubmit' | 'newline' — 回车的行为:多行默认换行,单行默认提交
placeholder string —
multiline boolean —
height number — 多行的固定高(pt);不给时按 rows 行算
rows number — 多行的行数(默认 3),每行 20
maxLength number —
counter boolean — 显示「已输入 / 上限」(配 maxLength):单行在行尾,多行在右下角
counterWarnAt number — 计数达到此字数时换警示色,不影响输入与校验
disabled boolean —
invalid boolean —
autoFocus boolean —
autoCorrect boolean —
keyboardType KeyboardType — 原生软键盘布局;phone-pad 用于拨号,不负责号码格式化与校验
variant 'default' | 'dial' — 单行大号等宽数字,适合键盘输入与屏上拨号键共用的号码框
size 'xs' | 'sm' | 'md' | 'lg' 'sm' xs 24 / sm 28(默认)12pt;md 32 / lg 36 13pt(对话框表单用 md)
class string — 框的宽度、外边距等
leading JSX.Element — 单行的前缀或多行输入上方的内容(如引用)
trailing JSX.Element — 行尾操作,多行时贴齐输入区域底部
footer JSX.Element — 多行框底部一行(计数之外的东西:附件、提示)
testID string —
accessibilityLabel string —
inputRef (node: InputRef) => void —
onFocus () => void —
onBlur () => void —
onContentSizeChange (event: { size: { width: number; height: number } }) => void —
scrollbar boolean —

* required

Prop Type Default Description
testID string —
label * string —
value * string —
onChange * (value: string) => void —
placeholder string —
maxLength number —
minHeight number — 输入区最小高,默认 120
onFocus () => void —

* required