Segmented
选择一个选项来切换时间段或视图。
何时使用
选择值
options 属性接受具有单独 value 和 label 的字符串、数字或对象。该模型保留 value 类型:数字 0 与字符串 '0' 不同。标签和图标不会更改所选值的类型。
| 问题 | 设置 | 示例 |
|---|---|---|
| 链接本地选择 | bind:value、options |
基本选择 |
| 外部管理选择 | value、onChange |
引导选择 |
| 将编号保存到模型 | value |
组名称和数值 |
| 更新选项集 | 新 options 和有效 value |
动态选项 |
defaultValue属性指定初始本地值;如果没有它,则使用第一个 options。对 defaultValue 的后续更改不会重新初始化该控件。对于更改和重置,请更新 value 或通过 bind:value 关联的状态。
onChange 处理程序根据用户选择报告新值。再次选择当前选项不会更改它,并且对 value 的外部分配不被视为用户事件。 指导示例 单独统计选择并返回外部按钮的状态。
更改 options 不会自动选择替换项。当删除当前选项时,应用程序必须同时分配一个有效 value。禁用会 disabled 整个组或单个变体,而不会将其转换为不同的模型值。
签名和位置
label 可以是字符串、数字或 Svelte 片段;icon 接受片段。 你的签名 结合了头像和名称,而 带文字的图标 则将常规标题保留在 SVG 旁边。
对于只有图标的选项,设置 aria-label;工具提示或 title 提供附加说明。不要将绘图视为唯一可用的名称。 图标示例 显示标题和单独的 tooltip。
block 属性将组拉伸到可用宽度。orientation 指定水平或垂直方向,并优先于 vertical 方向。 shape=round 改变形状; size 选择 small/medium/large,没有局部值则组件的整体大小有效。 尺寸示例 和 全宽 分别显示这些设置。
root、item、label 和 icon 的 classNames 和 CSS styles 字符串可用于装饰 - 来自 props 的对象或函数。 Semantic slots 显示当前设置。 theme.components.Segmented 组件设计令牌(design tokens)应用于皮肤; 令牌部分 描述了可用选项。
非空本地 prefixCls 指定根、变体和指示符的完整 CSS 前缀。空字符串和 undefined 继承ConfigProvider.prefixCls,带有 -segmented 后缀,默认为 ant-segmented。 CSS 变量和通用标记保留名称。 示例 ConfigProvider 切换全局和本地 prefix,同时保持无线电 DOM、选择和焦点。更改类本身不会触发 onChange。
键盘和表格
选项使用具有通用名称的本机无线电。如果没有明确的 name,Segmented 将创建自己的名称。 Tab 将焦点移入或移出组;箭头更改选择,跳过不可用的选项并移动到列表末尾。为该组指定一个易于访问的名称,如基本示例所示。
指标移动不会创建单独的选择事件;使用偏好 prefers-reduced-motion 它被禁用。该应用程序使用 value 和 onChange,而不是 CSS 动画的结束。挂载后访问root,提供了 bind:nativeElement;没有单独的 API 实例。
在当前包中,本机 input 不接收 value 变体,因此 FormData 返回字符串 on 而不是选定的字符串或数字。name 连接无线电,但不提供所需的序列化。 优先级示例 从 Svelte 状态发送一个数字,保留其原始类型。
示例
基本选择
options 和 bind:value 中的字符串。它旁边的值反映了选定的时间段。
<script lang="ts">
import { Segmented } from '@woodger/antd';
let value = $state("周");
</script>
<Segmented aria-label={("基期")} options={([
"天",
"周",
"月"
])} bind:value />
<p><output aria-label={("所选期间")}>{(value)}</output></p>
垂直方向
orientation="vertical" 从上到下排列选项。还支持兼容的 vertical。
<script lang="ts">
import { Segmented } from '@woodger/antd';
</script>
<Segmented aria-label={("垂直视图")} orientation="vertical" options={(["地图", "路线", "卫星"])} />
全宽
block 拉伸组并在选项之间分配可用宽度。
<script lang="ts">
import { Segmented } from '@woodger/antd';
</script>
<Segmented aria-label={("全宽视图")} block options={([
"列表",
"木板",
"日历"
])} />
圆形
shape="round" 更改组和所选选项的形状。
<script lang="ts">
import { Segmented } from '@woodger/antd';
</script>
<Segmented aria-label={("舍入周期")} shape="round" options={([
"天",
"周",
"月"
])} />
不可用的选项
屏蔽整个群组和特定月份。键盘会跳过不可用的选项。
<script lang="ts">
import { Segmented } from '@woodger/antd';
</script>
<style>
.examples {
display: grid;
justify-items: start;
gap: 16px;
}
</style>
<div class="examples">
<Segmented aria-label={("不可用组")} disabled options={([
"天",
"周",
"月"
])} />
<Segmented aria-label={("不可使用的月份")} options={([
"天",
"周",
{ value: "月", label: "月", disabled: true }
])} />
</div>
引导选择
value/onChange 和外部操作。仅当用户选择新值时计数器才会更改。
<script lang="ts">
import { Button, Segmented, type SegmentedOption } from '@woodger/antd';
type View = 'list' | 'board';
const options: SegmentedOption<View>[] = [
{ value: 'list', label: "列表" },
{ value: 'board', label: "木板" }
];
let value = $state<View>('list');
let changes = $state(0);
function handleChange(next: View) {
value = next;
changes += 1;
}
</script>
<style>
.controls {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
}
</style>
<div class="controls">
<Segmented aria-label={("托管视图")} options={(options)}
{value} onChange={handleChange} />
<Button onclick={() => { value = 'list'; }}>{("返回列表")}</Button>
</div>
<p>
<output aria-label={("托管视图状态")}>{value}{(";变化: ")}{changes}</output>
</p>
<script lang="ts">
import { Avatar, Segmented } from '@woodger/antd';
</script>
<style>.person {
display: grid;
justify-items: center;
gap: 6px;
padding: 8px 12px;
line-height: 1.5;
}</style>
{#snippet anna()}<div
class="person"
><Avatar
style="background:#722ed1"
>{("一个")}</Avatar><span>{("安娜")}</span></div>{/snippet}
{#snippet boris()}<div
class="person"
><Avatar
style="background:#d46b08"
>{("乙")}</Avatar><span>{("鲍里斯")}</span></div>{/snippet}
<Segmented aria-label={("负责任的")} options={([
{ value: 'anna', label: anna, 'aria-label': "安娜" },
{ value: 'boris', label: boris, 'aria-label': "鲍里斯" }
])} />
动态选项
添加季度和年份将维持当前选择。
<script lang="ts">
import { Button, Segmented } from '@woodger/antd';
let extended = $state(false);
let value = $state("天");
const initial = ["天", "周", "月"];
</script>
<Segmented aria-label={("动态周期")} options={(extended ? [
...initial,
"季度",
"年"
] : initial)} bind:value />
<p>
<Button disabled={extended} onclick={() => { extended = true; }}>
{("添加句点")}
</Button>
</p>
<p><output aria-label={("动态周期")}>{(value)}</output></p>
large
medium
small
三种尺寸
large、medium、small。如果没有 size,则使用常规设置中的尺寸。
<script lang="ts">
import { Segmented } from '@woodger/antd';
const sizes = ['large', 'medium', 'small'] as const;
</script>
<style>
.examples {
display: grid;
gap: 16px;
}
p {
margin: 0 0 8px;
}
</style>
<div class="examples">
{#each sizes as size (size)}
<div><p>{(size)}</p><Segmented aria-label={("尺寸分段 " + String((size)) + "")} {size} options={(["天", "周", "月"])} /></div>
{/each}
</div>
图标和签名
icon 接受带有 SVG 的 Svelte 片段;label 仍然是常规字符串。
<script lang="ts">
import { Segmented } from '@woodger/antd';
</script>
{#snippet list()}<svg
width="14"
height="14"
viewBox="0 0 16 16"
fill="none"
><path
d="M5 4h9M5 8h9M5 12h9M2 4h.01M2 8h.01M2 12h.01"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
/></svg>{/snippet}
{#snippet board()}<svg
width="14"
height="14"
viewBox="0 0 16 16"
fill="none"
><path
d="M2 2h5v12H2zM9 2h5v8H9z"
stroke="currentColor"
stroke-width="1.5"
/></svg>{/snippet}
<Segmented aria-label={("用图标查看")} options={([{
value: 'list',
label: "列表",
icon: list
}, { value: 'board', label: "木板", icon: board }])} />
<script lang="ts">
import { Segmented } from '@woodger/antd';
</script>
{#snippet list()}<svg
width="16"
height="16"
viewBox="0 0 16 16"
fill="none"
><path
d="M2 4h12M2 8h12M2 12h12"
stroke="currentColor"
stroke-width="1.5"
/></svg>{/snippet}
{#snippet grid()}<svg
width="16"
height="16"
viewBox="0 0 16 16"
fill="none"
><path
d="M2 2h4v4H2zM10 2h4v4h-4zM2 10h4v4H2zM10 10h4v4h-4z"
stroke="currentColor"
stroke-width="1.5"
/></svg>{/snippet}
<Segmented aria-label={("仅图标")} options={([
{ value: 'list', icon: list, 'aria-label': "列表", tooltip: "显示为列表" },
{ value: 'grid', icon: grid, 'aria-label': "瓦", tooltip: "显示平铺" }
])} />
组名称和数值
name 结合了本地广播。该表格提交了来自 Svelte 州的号码;不使用自动无线电序列化。
<script lang="ts">
import { Button, Segmented } from '@woodger/antd';
let value = $state<number>(1);
let submitted = $state<number | null>(null);
function handleSubmit(event: SubmitEvent) {
event.preventDefault();
submitted = value;
}
</script>
<form onsubmit={handleSubmit}>
<Segmented aria-label={("任务优先级")} name="priority" options={([
{ value: 1, label: "普通的" }, { value: 2, label: "高" }
])} bind:value />
<p><Button htmlType="submit">{("申请优先权")}</Button></p>
</form>
<p><output aria-label={("发送优先级")}>{(submitted === null ? "未发送" : `${submitted} (${typeof submitted})`)}</output></p>
Semantic slots
classNames 对象和 styles 函数设置实际 root、item、label 和 icon 的样式。styles 值是 CSS 字符串。
<script lang="ts">
import { Segmented } from '@woodger/antd';
</script>
{#snippet list()}<span>≡</span>{/snippet}
{#snippet board()}<span>▤</span>{/snippet}
<Segmented aria-label={("语义槽设计")} options={([
{ value: 'list', label: "列表", icon: list }, { value: 'board', label: "木板", icon: board }
])} classNames={{ label: 'documented-segment-label' }} styles={({ props }) => ({
root: `
border: 1px dashed var(--ant-color-primary);
padding: 6px;
`,
item: props.disabled ? 'opacity:0.5' : '',
label: 'font-weight:600',
icon: 'color:var(--ant-color-primary)'
})} />
已完成的任务: 58
<script lang="ts">
import { Segmented, type SegmentedValue } from '@woodger/antd';
let period = $state<SegmentedValue>('week');
const totals: Record<string, number> = { day: 12, week: 58, month: 211 };
</script>
<Segmented
aria-label={("报告期")}
options={([
{ value: 'day', label: "天" },
{ value: 'week', label: "周", tooltip: "近7天指标" },
{ value: 'month', label: "月" }
])}
bind:value={period}
/>
<p>{("已完成的任务: ")}<strong>{(totals[String(period)])}</strong></p>
API
ant.design 上的所有 13 个分段实时页面示例主题都映射到上面的卡片。附加卡可保存审阅报告。该表描述了已安装的 SegmentedProps;完整类型属于包。
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
prefixCls | 根、变体和指示符的完整本地 CSS 前缀。空字符串继承公共前缀;无需配置,使用 ant-segmented。更改类别会保存无线电和所选值。 | string | ConfigProvider + -segmented |
options | 具有 value、label、icon、disabled、tooltip、class、title 和 aria-label 的字符串、数字或对象。 | readonly (Value | SegmentedOption<Value>)[] | 必需的 |
value | 当前值。支持 bind:value;数字类型被保留。 | Value extends string | number | undefined |
defaultValue | 初始本地值,读取一次。稍后更改 defaultValue 不会重置选择。 | Value | 第一个选项 |
onChange | 当选择另一个可用选项时调用。对值的外部赋 value 不会触发回调。 | (value: Value) => void | — |
disabled | 阻止整个组。通过 options[].disabled 禁用单独的选项。 | boolean | false |
block | 将组和选项拉伸到父级的宽度。 | boolean | false |
size | 标准密度下 large - 40 像素,medium - 32 像素,small - 24 像素。 | large | medium | small | 来自配置提供者 |
orientation | 布局方向。优先于兼容 vertical。 | horizontal | vertical | horizontal |
vertical | 垂直模式的兼容名称。 | boolean | false |
shape | 形状规则或圆形。 | default | round | default |
name | 本地广播的通用名称。不替换可用的组名称。 | string | 生成的名称 |
classNames | root、item、label 和 icon 类。单个选项的类是通过 option.class 指定的,而不是 className。 | SegmentedSemanticSlots | (info) => SegmentedSemanticSlots | — |
styles | 相同插槽的 CSS 行。该函数接收 info.props。 | SegmentedSemanticSlots | (info) => SegmentedSemanticSlots | — |
nativeElement | bind:nativeElement 挂载后返回根 div;该组件不导出单独的 focus()/blur()。 | HTMLDivElement | — |
与 React 不同,label 和 icon 接受 Svelte 片段,styles 接受 CSS 字符串,变体类称为 class。当前包中没有单独的ConfigProvider.segmented。适用一般 componentSize 和 direction。
Semantic DOM
该组是一个具有 radiogroup 角色、aria-orientation 和可用名称的 div。每个选项都是一个input type="radio" 的 label。对于组,使用 aria-label 或 aria-labelledby;对于没有文本的图标,设置 options[].aria-label。
| Slot | 元素 |
|---|---|
root | 组的根 div。 |
item | 每个选项的 label。 |
label | 标签内有 label 容器。 |
icon | 带有 aria-hidden 的 span 图标。 |
单选按钮上的箭头循环显示可用选项,并跳转到列表末尾。本机 input 获得焦点。对于编程焦点 bind:nativeElement;没有单独的 API 实例。通过偏好 prefers-reduced-motion 禁用指示器移动。
注册令牌
根据 ant.design 和 ComponentTokenMap['Segmented'] 检查八个组件标记。 theme.components.Segmented 将组件标记应用于样式。通过 styles 指定本地设计,如 语义槽示例.
| 代币 | 目的 |
|---|---|
itemActiveBg | 单击选项的背景。 |
itemColor | 选项文本颜色。 |
itemHoverBg | 悬停时的背景。 |
itemHoverColor | 悬停文本颜色。 |
itemSelectedBg | 所选选项的背景。 |
itemSelectedColor | 所选文本的颜色。 |
trackBg | 集团背景。 |
trackPadding | 组缩进。 |
当前主题的常用标记
该列表根据 ant.design 上的分段表进行检查。这些值是通过 theme.useToken() 反应式读取的。
| 代币 | 当前值 |
|---|---|
colorPrimaryBorder | #adc6ff |
colorText | rgba(0,0,0,0.88) |
colorTextDisabled | rgba(0,0,0,0.25) |
borderRadius | 6 |
borderRadiusLG | 8 |
borderRadiusSM | 4 |
borderRadiusXS | 2 |
boxShadowTertiary |
0 1px 2px 0 rgba(0,0,0,0.05),
0 1px 6px -1px rgba(0,0,0,0.03),
0 2px 4px 0 rgba(0,0,0,0.03)
|
controlHeight | 32 |
controlHeightLG | 40 |
controlHeightSM | 24 |
controlPaddingHorizontal | 12 |
controlPaddingHorizontalSM | 8 |
fontFamily | -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial,
'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol',
'Noto Color Emoji' |
fontSize | 14 |
fontSizeLG | 16 |
lineHeight | 1.5714285714285714 |
lineWidth | 1 |
lineWidthFocus | 3 |
marginSM | 12 |
motionDurationMid | 0.2s |
motionDurationSlow | 0.3s |
motionEaseInOut | cubic-bezier(0.645, 0.045, 0.355, 1) |
paddingXXS | 4 |
FAQ
分段与选项卡有何不同?
分段为现有区域选择一个值:周期、排序或视图。选项卡组织各个内容面板及其导航。
如何获取数字而不是字符串?
传递 value 和键入的状态,如 优先级示例 中所示。label 不定义值的类型。
name 足以提交 HTML 表单吗?
不,在当前包中,本机 input 不接收 value 选项:FormData 将返回标准 on。从 bind:value 或 onChange 中读取选择,如示例表单中所示。无线电分组需要 name。
当我删除选定的选项时会发生什么?
动态 options 不会自动选择替换项。应用程序在删除当前变体时必须分配一个有效 value。 动态选项示例 显示添加而不重置选择。