跳转到内容
Nacre
组件配色
EN

勾选 Checkbox

stableCheckBox · CheckRow · CheckMark在画廊运行
CheckBox
在浏览器里运行真实组件。

多选与开关式确认,支持半选。框用 CheckBox,带说明文字、整行可点的用 CheckRow;回调统一 onChange(checked)。

状态:stable · 组件:CheckBox、CheckRow、CheckMark

import { CheckBox, CheckRow, CheckMark } from 'nacre'
import { signal } from 'nsh'
import { CheckRow } from 'nacre'
export function CheckboxUsage() {
const notify = signal(true)
return <CheckRow label="同时通知协作人" checked={notify()} onChange={(checked) => notify.set(checked)} />
}

选中、未选、半选(mixed)、禁用。CheckBox 只有框,label 只给读屏。

import { View, signal } from 'nsh'
import { CheckBox } from 'nacre'
export function CheckboxStates() {
const on = signal(true)
const off = signal(false)
return (
<View class="flex-row items-center gap-4">
<CheckBox label="选中" checked={on()} onChange={(v) => on.set(v)} />
<CheckBox label="未选" checked={off()} onChange={(v) => off.set(v)} />
<CheckBox label="半选" checked={false} mixed onChange={() => {}} />
<CheckBox label="禁用" checked disabled onChange={() => {}} />
</View>
)
}

父项按子项算:全选、全不选、部分选(mixed);点父项全选或全清。

import { View, signal } from 'nsh'
import { CheckRow } from 'nacre'
const teams = () => ['香港团队', '新加坡团队', '高净值专线']
export function CheckboxIndeterminate() {
const TEAMS = teams()
const picked = signal<string[]>([TEAMS[0]])
const all = () => picked().length === TEAMS.length
const some = () => picked().length > 0 && !all()
const toggle = (team: string, on: boolean) => picked.update((list) => (on ? [...list, team] : list.filter((t) => t !== team)))
return (
<View class="w-[240px] max-w-full gap-1">
<CheckRow label="全部团队" checked={all()} mixed={some()} onChange={(on) => picked.set(on ? [...TEAMS] : [])} />
<View class="pl-6 gap-1">
{TEAMS.map((team) => (
<CheckRow label={team} checked={picked().includes(team)} onChange={(on) => toggle(team, on)} />
))}
</View>
</View>
)
}

CheckRow 整行可点,文字就是读屏名;适合设置项与筛选列表。

import { View, signal } from 'nsh'
import { CheckRow } from 'nacre'
export function CheckboxRows() {
const sound = signal(true)
const badge = signal(false)
return (
<View class="w-[260px] max-w-full gap-1">
<CheckRow label="新消息播放提示音" checked={sound()} onChange={(v) => sound.set(v)} />
<CheckRow label="Dock 显示未读数" checked={badge()} onChange={(v) => badge.set(v)} />
<CheckRow label="由管理员统一设置" checked={false} disabled onChange={() => {}} />
</View>
)
}
键 行为
Tab 聚焦
Space 切换勾选
  • role 是 checkbox,勾选态读作 checked / unchecked / mixed。
  • CheckBox 的 label 必填(读屏名);CheckRow 的文字即读屏名。
  • 禁用项不进 Tab 序列。

该用

  • 多选、或「确认后才生效」的设置用勾选
  • 一组相关项给全选父项(半选)

不该用

  • 立即生效的开关用勾选(用 Switch)
  • 互斥选项用勾选(用 Radio)
属性 类型 默认 说明
checked * boolean —
mixed boolean — 半选(全选列表头、树的父节点):画横杠,读屏报 mixed;点一下的去向由 onChange 决定
disabled boolean —
label * string — 读屏名(框旁没有可见文字,必须给)
onChange * (checked: boolean) => void — 回调与勾选家族一致:拿到点击后应有的新值(!checked;半选时为 true)
testID string —

* 必填

属性 类型 默认 说明
checked * boolean —
mixed boolean — 半选(父项部分子项勾选):画横杠,读屏报 mixed
label * string —
onChange * (checked: boolean) => void —
disabled boolean —
testID string —
class string —
children JSX.Element — 文字之外的内容(说明、标签);不给就只有 label

* 必填

属性 类型 默认 说明
checked * boolean —
mixed boolean —

* 必填