跳转到内容
Nacre
组件配色
EN

分段 Segmented

stableSegmented在画廊运行
Segmented
在浏览器里运行真实组件。

在 2–5 个互斥值里选一个,全部选项一眼可见;受控 value + onChange,选中块弹簧滑过去。

状态:stable · 组件:Segmented

import { Segmented } from 'nacre'
import { signal } from 'nsh'
import { Segmented } from 'nacre'
export function SegmentedUsage() {
const range = signal<'today' | 'week' | 'month'>('today')
return (
<Segmented
accessibilityLabel="时间段"
value={range()}
onChange={(value) => range.set(value)}
options={[
{ value: 'today', label: '今日' },
{ value: 'week', label: '本周' },
{ value: 'month', label: '本月' },
]}
/>
)
}

count 跟在名称后,等宽数字;读屏名合成「待处理 3」。

import { signal } from 'nsh'
import { Segmented } from 'nacre'
const sessionTabs = () => [
{ value: 'current' as const, label: '当前会话' },
{ value: 'pending' as const, label: '待处理', count: '3' },
{ value: 'closed' as const, label: '已关闭' },
]
export function SegmentedCounts() {
const tab = signal<'current' | 'pending' | 'closed'>('current')
return <Segmented accessibilityLabel="会话" value={tab()} onChange={(value) => tab.set(value)} options={sessionTabs()} />
}

xs 24(区块标题行里)/ sm 28(默认)/ md 32;没有 lg。同一行的控件用同一档。

手机:手机档 xs 28 / sm 32(默认,iOS 分段)/ md 40,铺满整行时各项等宽;每个选项的点击区上下补到 44。

import { View, signal } from 'nsh'
import { Segmented } from 'nacre'
const sessionTabs = () => [
{ value: 'current' as const, label: '当前会话' },
{ value: 'pending' as const, label: '待处理', count: '3' },
{ value: 'closed' as const, label: '已关闭' },
]
export function SegmentedSizes() {
const tab = signal<'current' | 'pending' | 'closed'>('pending')
return (
<View class="gap-2">
{(['xs', 'sm', 'md'] as const).map((size) => (
<Segmented size={size} accessibilityLabel={`$会话 ${size}`} value={tab()} onChange={(value) => tab.set(value)} options={sessionTabs()} />
))}
</View>
)
}

整组不可点、降到 40% 不透明度。

import { Segmented } from 'nacre'
export function SegmentedDisabled() {
return (
<Segmented
disabled
accessibilityLabel="时间段(无权限)"
value="today"
onChange={() => {}}
options={[
{ value: 'today', label: '今日' },
{ value: 'week', label: '本周' },
]}
/>
)
}
键 行为
Tab 整组只停一次:进组落在选中的那一段,再按离开这一组
← → 左右键直接选中相邻段,到头回绕;页签模式另有 Home / End 到首尾。横排页签把上下键留给页面
Space / Return 选中聚焦的段
  • 默认是单选组(radio group,nsh/base RadioGroup):选中态读作 checked;切内容面板(预览 / 代码、页面分区)时给 tabs,是一组页签(nsh/base Tabs),选中态读作 selected。accessibilityLabel 写这组选的是什么(「时间段」)。
  • 选中段用比轨道亮的底和一层近影。键盘导航另加 1pt 内环,两态字重与宽度相同。

该用

  • 项数少且都要一眼看到
  • 可带计数(「待处理 3」)
  • 选项文字短(2–4 个字)

不该用

  • 超过 5 项(用选择框)
  • 当导航用
  • 用字重表示选中
属性 类型 默认 说明
options * readonly SegmentedOption<T>[] — 按显示顺序排列的分段,每项含值、标签和可选计数。
value * T — 当前值(受控):照组件库统一契约 value + onChange,不收 Signal(漏传回调会静默失效)
onChange * (value: T) => void — 选中另一项时调用;选项的 testID 是 ${testID}-${value}
size 'xs' | 'sm' | 'md' 'sm' xs 24(区块标题行里)/ sm 28(默认)/ md 32
accessibilityLabel string — 整组的读屏名(「时间段」)
testID string — 分段组的测试标识前缀。
disabled boolean — 禁用整组分段。
tabs boolean — 切内容面板(预览 / 代码、页面内分区)时给 true:用页签语义(nsh/base Tabs),读屏读成一组页签,方向键切到哪个就显示哪个; 默认是选一个值(单选组语义)。外观相同,创建时读一次
focusRef (focus: () => void) => void — 拿到一个「把焦点交给选中的那一段」的函数(焦点区域的入口、F6 落点)

* 必填