Segmented#
数据录入
@rockshin/tao-ui从紧凑选项中选择单个视图或模式。
组件采用 tao-ui 原生实现,公共 API 对齐 Ant Design 6.7.0。外观、主题 token 与 Semantic DOM 定制遵循 tao-ui,主题通过 TaoProvider 配置。
6.7.0 value/options API 使用原生单选控件与 tao-ui 动效实现;尺寸继承 TaoProvider,减弱动态效果时关闭移动高亮。
#何时使用
适合少量紧密相关的选项,原生单选控件支持表单提交与键盘选择。
#代码演示
#基础选择
TSX
import { Segmented } from "@rockshin/tao-ui";
export default function Example() {
return <Segmented options={["Day", "Week", "Month"]} defaultValue="Week" />;
}#受控选择
Current view: List
TSX
import { useState } from "react";
import { Segmented } from "@rockshin/tao-ui";
export default function Example() {
const [view, setView] = useState("List");
return (
<div>
<Segmented<string>
options={["List", "Board", "Calendar"]}
value={view}
onChange={setView}
/>
<p>Current view: {view}</p>
</div>
);
}#尺寸与圆形风格
TSX
import { Segmented } from "@rockshin/tao-ui";
export default function Example() {
return (
<div style={{ display: "flex", gap: 16, flexDirection: "column" }}>
<Segmented size="small" options={["A", "B", "C"]} />
<Segmented size="medium" shape="round" options={["A", "B", "C"]} />
<Segmented
size="large"
block
options={["Overview", "Activity", "Settings"]}
/>
</div>
);
}#禁用选项与自定义标签
TSX
import { Segmented } from "@rockshin/tao-ui";
export default function Example() {
return (
<div style={{ display: "flex", gap: 16, flexDirection: "column" }}>
<Segmented
options={[
{
value: "open",
label: "Open",
icon: <span aria-hidden="true">○</span>,
},
{ value: "closed", label: "Closed", disabled: true },
{
value: "all",
label: "All records",
tooltip: "Includes archived records",
},
]}
/>
<Segmented disabled options={["Day", "Week"]} />
</div>
);
}#垂直排列与 RTL
TSX
import { Segmented } from "@rockshin/tao-ui";
export default function Example() {
return (
<div
style={{
display: "flex",
gap: 16,
alignItems: "center",
flexWrap: "wrap",
}}
>
<Segmented
orientation="vertical"
options={["Overview", "Activity", "Settings"]}
/>
<Segmented direction="rtl" options={["One", "Two", "Three"]} />
</div>
);
}#提交原生表单
TSX
import { useState } from "react";
import { Button, Segmented } from "@rockshin/tao-ui";
export default function Example() {
const [submitted, setSubmitted] = useState("");
return (
<form
onSubmit={(event) => {
event.preventDefault();
setSubmitted(String(new FormData(event.currentTarget).get("period")));
}}
>
<Segmented
name="period"
options={["Day", "Week", "Month"]}
defaultValue="Week"
/>
<Button type="submit" style={{ marginLeft: 12 }}>
Apply
</Button>
<p>Submitted: {submitted || "—"}</p>
</form>
);
}#API
#Segmented
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| disabled | 是否禁用 | boolean | undefined | - |