Segmented 分段控制器
用于展示多个选项并允许用户选择其中单个选项。
基础用法
export function SegmentedBasic() { const [value, setValue] = useState<string>("Mon"); return ( <div style={stack}> <ElSegmented value={value} onChange={(v) => setValue(v as string)} options={DAY_OPTIONS} size="large" /> <ElSegmented value={value} onChange={(v) => setValue(v as string)} options={DAY_OPTIONS} /> <ElSegmented value={value} onChange={(v) => setValue(v as string)} options={DAY_OPTIONS} size="small" /> </div> ); } 配置方向2.8.7
export function SegmentedCustomDirection() { const [value, setValue] = useState<string>("Apple"); const [direction, setDirection] = useState<"horizontal" | "vertical">("horizontal"); const [size, setSize] = useState<ComponentSize>("default"); const options = FRUIT_OPTIONS.map((item) => item.value === "Watermelon" ? { ...item, disabled: true } : item, ); return ( <div> <ElSegmented value={size} onChange={(v) => setSize(v as ComponentSize)} options={["large", "default", "small"]} style={{ marginBottom: 16 }} /> <br /> <ElSegmented value={direction} onChange={(v) => setDirection(v as "horizontal" | "vertical")} options={[ { label: "Horizontal", value: "horizontal" }, { label: "Vertical", value: "vertical" }, ]} style={{ marginBottom: 16 }} /> <br /> <ElSegmented value={value} onChange={(v) => setValue(v as string)} options={options} direction={direction} size={size} > {(item) => { const option = item as { label: string; icon: string }; return ( <div style={{ display: "flex", flexDirection: "column", alignItems: "center", gap: "0.5rem", padding: direction === "horizontal" ? "0.5rem" : undefined, }} > <ElIcon size={20}> <FruitIcon name={option.icon} /> </ElIcon> <div>{option.label}</div> </div> ); }} </ElSegmented> </div> ); } 禁用状态
export function SegmentedDisabled() { const [value, setValue] = useState<string>("Mon"); const options = [ { label: "Mon", value: "Mon", disabled: true }, { label: "Tue", value: "Tue" }, { label: "Wed", value: "Wed", disabled: true }, { label: "Thu", value: "Thu" }, { label: "Fri", value: "Fri", disabled: true }, { label: "Sat", value: "Sat" }, { label: "Sun", value: "Sun" }, ]; return ( <div style={stack}> <ElSegmented value={value} onChange={(v) => setValue(v as string)} options={options} disabled /> <ElSegmented value={value} onChange={(v) => setValue(v as string)} options={options} /> </div> ); } 自定义选项 2.9.8 当您的 options 格式不同于默认格式时,可通过 props 属性自定义 options
export function SegmentedAliases() { const [value, setValue] = useState<string>("Mon"); const optionProps = { label: "myLabel", value: "myValue", disabled: "myDisabled" }; const options = [ { myLabel: "Mon", myValue: "Mon", myDisabled: true }, { myLabel: "Tue", myValue: "Tue" }, { myLabel: "Wed", myValue: "Wed", myDisabled: true }, { myLabel: "Thu", myValue: "Thu" }, { myLabel: "Fri", myValue: "Fri", myDisabled: true }, { myLabel: "Sat", myValue: "Sat" }, { myLabel: "Sun", myValue: "Sun" }, ]; return ( <ElSegmented value={value} onChange={(v) => setValue(v as string)} options={options} optionProps={optionProps} /> ); } Block 分段选择器
export function SegmentedBlock() { const [value, setValue] = useState<string>("Mon"); const options = [ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sunday long long long long long long long", ]; return <ElSegmented value={value} onChange={(v) => setValue(v as string)} options={options} block />; } 自定义内容 设置 default slot 位来渲染自定义内容。
export function SegmentedCustomContent() { const [value, setValue] = useState<string>("Apple"); return ( <ElSegmented value={value} onChange={(v) => setValue(v as string)} options={FRUIT_OPTIONS}> {(item) => { const option = item as { label: string; icon: string }; return ( <div style={{ display: "flex", flexDirection: "column", alignItems: "center", gap: "0.5rem", padding: "0.5rem", }} > <ElIcon size={20}> <FruitIcon name={option.icon} /> </ElIcon> <div>{option.label}</div> </div> ); }} </ElSegmented> ); } 自定义样式
export function SegmentedCustomStyle() { const [value, setValue] = useState<string>("Delicacy"); const options = ["Delicacy", "Desserts&Drinks", "Fresh foods", "Supermarket"]; const customVars = { "--el-segmented-item-selected-color": "var(--el-text-color-primary)", "--el-segmented-item-selected-bg-color": "#ffd100", "--el-border-radius-base": "16px", } as CSSProperties; return ( <ElSegmented value={value} onChange={(v) => setValue(v as string)} options={options} style={customVars} /> ); } Segmented API
Segmented Attributes
属性名 说明 类型 默认值 value 绑定值,对应官方 v-model string / number / boolean— options 选项数据 array[] option-props 选项字段别名,对应官方 props object— size 尺寸 enum'default' block 是否撑满父容器宽度 booleanfalse disabled 是否禁用整个组件 booleanfalse validate-event 是否触发表单校验 booleantrue name 原生 name 属性 string— id 原生 id 属性 string— aria-label 原生 aria-label 属性 string— direction 排列方向 enum'horizontal' class-name / style 根元素类名与样式 string / object—
optionProps
字段 说明 类型 默认值 value 选项对象里作为值的键名 stringvalue label 选项对象里作为标签的键名 stringlabel disabled 选项对象里作为禁用标记的键名 stringdisabled
Segmented Events
事件名 说明 参数 onChange 选中值变化时触发,参数为当前选中值 Function
Segmented Slots
名称 说明 参数 children 自定义选项内容,传入函数即生效 Function