Segmented 分段控制器
把几个互斥的选项排成一行,选中项下面有一块会滑动的底色。适合选项少(一般 2 到 5 个)、需要频繁切换的场景,例如切换视图、切换时间范围。
引入
import { HuiSegmented } from '@nscode/handy-ui'
自持状态与受控回填
内部尚无选择时保留首项显示回退,首次点击首项会提交一次;保存选择后,同值操作不重复发change。显式undefined仍受控并保留首项显示回退。 未传属性或仅监听更新时组件自行保存,父无关重绘不重置;传入属性(含undefined)即受控,父不接受更新时显示保持父值。切换模式会输出开发诊断,不新增defaultValue等属性。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSegmented, HuiButton, HuiSpace } from '@nscode/handy-ui'
const q1Redraw = ref(0)
const q1Last = ref('尚无请求')
const q1Model = ref('a')
</script>
<template>
<div class="q1-state-demo segmented-q1-demo">
<HuiButton class="q1-redraw" @click="q1Redraw++">父重绘 {{ q1Redraw }}</HuiButton>
<HuiSpace direction="vertical" fill>
<div>
<p>未传属性,自行保存</p>
<div class="q1-local"><HuiSegmented :options="['a', 'b', 'c']" /></div>
</div>
<div>
<p>仅监听更新,仍自行保存</p>
<div class="q1-listener">
<HuiSegmented :options="['a', 'b', 'c']" @update:model-value="q1Last = String($event)" />
</div>
</div>
<div>
<p>显式undefined,父值优先</p>
<div class="q1-empty">
<HuiSegmented :options="['a', 'b', 'c']" :model-value="undefined" />
</div>
</div>
<div>
<p>固定受控,父拒绝更新</p>
<div class="q1-fixed">
<HuiSegmented
:options="['a', 'b', 'c']"
:model-value="'b'"
@update:model-value="q1Last = String($event)"
/>
</div>
</div>
<div>
<p>双向绑定</p>
<div class="q1-bound"><HuiSegmented :options="['a', 'b', 'c']" v-model="q1Model" /></div>
</div>
<p class="q1-notification">绑定值:{{ q1Model }};最近请求:{{ q1Last }}</p>
</HuiSpace>
</div>
</template>
基础用法
options 传字符串数组,v-model 绑定当前选中的那一项。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSegmented } from '@nscode/handy-ui'
const value = ref('每日')
const options = ['每日', '每周', '每月', '每年']
</script>
<template>
<HuiSegmented v-model="value" :options="options" />
<p style="margin-top: 12px">绑定值:'{{ value }}'</p>
</template>
对象选项
显示的文字和绑定的值不一样时,options 传对象数组:value 是值,label 是文字(不写时显示 value),title 是鼠标停在选项上时浏览器显示的提示。payload 可以放自己的附加数据,组件不读取。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSegmented } from '@nscode/handy-ui'
import type { SegmentedOption } from '@nscode/handy-ui'
const value = ref('list')
const options: SegmentedOption[] = [
{ value: 'list', label: '列表视图', title: '按列表显示' },
{ value: 'grid', label: '网格视图', title: '按网格显示' },
{ value: 'table', label: '表格视图', title: '按表格显示' }
]
</script>
<template>
<HuiSegmented v-model="value" :options="options" />
<p style="margin-top: 12px">绑定值:'{{ value }}'</p>
</template>
数字选项
options 也可以是数字数组,这时绑定值是数字。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSegmented } from '@nscode/handy-ui'
const pageSize = ref(20)
const options = [10, 20, 50, 100]
</script>
<template>
<HuiSegmented v-model="pageSize" :options="options" />
<p style="margin-top: 12px">绑定值:{{ pageSize }}({{ typeof pageSize }})</p>
</template>
带图标
选项的 icon 写图标名(和 HuiIcon 的 name 一样),图标显示在文字前面。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSegmented } from '@nscode/handy-ui'
import type { SegmentedOption } from '@nscode/handy-ui'
const value = ref('list')
const options: SegmentedOption[] = [
{ value: 'list', label: '列表', icon: 'menu' },
{ value: 'grid', label: '网格', icon: 'apps' },
{ value: 'table', label: '表格', icon: 'table' }
]
</script>
<template>
<HuiSegmented v-model="value" :options="options" />
</template>
尺寸
size 有 small、default、large 三种,高度分别是 24、32、40px,和同尺寸的按钮、输入框一样高;文字是 12、14、16px,圆角跟着尺寸变(外框 4、6、8px)。
不写 size 时依次跟随所在的 HuiFormItem、HuiForm、HuiConfigProvider 的 size,都没有时是 default。下面每一行最后放一个同尺寸的按钮对比高度:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSegmented, HuiSpace } from '@nscode/handy-ui'
import type { SegmentedOption } from '@nscode/handy-ui'
const value = ref('day')
const options: SegmentedOption[] = [
{ value: 'day', label: '每日', icon: 'calendar' },
{ value: 'week', label: '每周' },
{ value: 'month', label: '每月' }
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">large</span>
<HuiSegmented v-model="value" :options="options" size="large" />
<HuiButton size="large">按钮</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">default</span>
<HuiSegmented v-model="value" :options="options" />
<HuiButton>按钮</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">small</span>
<HuiSegmented v-model="value" :options="options" size="small" />
<HuiButton size="small">按钮</HuiButton>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 56px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
撑满宽度
加上 block 以后组件占满父容器的宽度,各个选项平分宽度。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSegmented } from '@nscode/handy-ui'
const value = ref('每日')
const options = ['每日', '每周', '每月']
</script>
<template>
<HuiSegmented v-model="value" :options="options" block />
</template>
禁用
disabled 禁用整个组件。只禁用某一项时,在这一项上写 disabled: true,方向键也会跳过它。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSegmented, HuiSpace } from '@nscode/handy-ui'
import type { SegmentedOption } from '@nscode/handy-ui'
const a = ref('每日')
const b = ref('day')
const options = ['每日', '每周', '每月']
const partlyDisabled: SegmentedOption[] = [
{ value: 'day', label: '每日' },
{ value: 'week', label: '每周', disabled: true },
{ value: 'month', label: '每月' }
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">整个禁用</span>
<HuiSegmented v-model="a" :options="options" disabled />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">禁用一项</span>
<HuiSegmented v-model="b" :options="partlyDisabled" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 72px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
事件
用户换了选中项时触发 change(和 update:modelValue 同时),参数是新选中项的 value;点已经选中的项不触发。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMessage, HuiSegmented } from '@nscode/handy-ui'
const value = ref('chart')
const options = [
{ value: 'chart', label: '图表' },
{ value: 'table', label: '表格' }
]
const onChange = (next: string | number) => {
HuiMessage.info(`切换到:${next}`)
}
</script>
<template>
<HuiSegmented v-model="value" :options="options" @change="onChange" />
</template>
选项动态变化
options 可以随时换。选项增减、文字变长、容器宽度变化以后,底色块会跟着选中项重新定位。绑定值在新选项里找不到时,没有选中项,底色块不显示。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSegmented, HuiSpace } from '@nscode/handy-ui'
import type { SegmentedOption } from '@nscode/handy-ui'
const value = ref('b')
const options = ref<SegmentedOption[]>([
{ value: 'a', label: '选项A' },
{ value: 'b', label: '选项B' },
{ value: 'c', label: '选项C' }
])
let seed = 0
const prepend = () => {
seed += 1
options.value = [{ value: `new-${seed}`, label: `新选项${seed}` }, ...options.value]
}
const removeSelected = () => {
options.value = options.value.filter((opt) => opt.value !== value.value)
}
</script>
<template>
<HuiSpace wrap>
<HuiSegmented v-model="value" :options="options" />
<HuiButton size="small" @click="prepend">在前面加一项</HuiButton>
<HuiButton size="small" @click="removeSelected">删掉选中项</HuiButton>
</HuiSpace>
</template>
在表单里使用
放在 HuiFormItem 里时,值变化触发 change 规则,组件通过 aria-labelledby 关联表单项的标签,尺寸跟随表单项和表单。不需要校验时设 :validate-event="false"。
<script setup lang="ts">
import { reactive } from 'vue'
import { HuiForm, HuiFormItem, HuiSegmented } from '@nscode/handy-ui'
const model = reactive({ period: '按周', view: '列表' })
</script>
<template>
<HuiForm :model="model" size="small" @submit.prevent>
<HuiFormItem label="统计周期">
<HuiSegmented v-model="model.period" :options="['按日', '按周', '按月']" />
</HuiFormItem>
<HuiFormItem label="显示方式">
<HuiSegmented v-model="model.view" :options="['列表', '网格']" />
</HuiFormItem>
</HuiForm>
</template>
键盘操作
用 Tab 键进入组件时焦点落在实际选中项上,整个组件只占一个 Tab 停靠点。组内停靠点跟着焦点;受控父组件拒绝或规范化选择时,焦点可以与选择不同,离开组后入口回到实际选中项。
| 按键 | 作用 |
|---|---|
→ ↓ | 选中下一项,到最后一项后回到第一项 |
← ↑ | 选中上一项,到第一项后回到最后一项 |
Home / End | 聚焦并选择首个 / 末个可用项 |
Enter 空格 | 选中当前聚焦的项 |
方向键和 Home/End 都跳过禁用项。输入法组合中(含 229 兼容码)、Alt/Ctrl/Meta 组合键、子控件已消费的按键不处理。PageUp/PageDown 和首字母不作为导航键。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSegmented, HuiSpace } from '@nscode/handy-ui'
const value = ref<string | number>('a')
const event = ref('等待操作')
const options = [
{ value: 'a', label: '选项A' },
{ value: 'b', label: '选项B(禁用)', disabled: true },
{ value: 'c', label: '选项C' },
{ value: 'd', label: '选项D' }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSegmented v-model="value" :options="options" @change="event = 'change:' + $event" />
<span>当前值:{{ value }};{{ event }}</span>
</HuiSpace>
</template>
选项使用稳定且唯一的 value。重排按 key 保留焦点;聚焦项被过滤、删除或禁用后就近恢复到可用项,不自动选择或改值;全禁用和空列表没有 Tab 入口。程序调用 focus() 也不触发选择,已移到外部的焦点不会被数据更新抢回。
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| modelValue | string | number | undefined | 选中项的值,支持 v-model。未传时自行保存,初值 undefined;传入属性即受控。尚无值时首项仅作显示回退。 |
| options | SegmentedOption[] | string[] | number[] | [] | 选项。字符串、数字数组里每一项既是值也是文字 |
| size | 'small' | 'default' | 'large' | - | 尺寸,高度 24 / 32 / 40px。不写时跟随 FormItem、Form、ConfigProvider,都没有时是 'default' |
| disabled | boolean | false | 是否禁用整个组件 |
| block | boolean | false | 是否占满父容器宽度 |
| validateEvent | boolean | true | 值变化时是否触发所在表单项的校验 |
SegmentedOption
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| value | string | number | - | 选项的值,必填,同一组里不能重复 |
| label | string | - | 显示的文字。不写时显示 value |
| disabled | boolean | false | 是否禁用这一项 |
| icon | string | - | 图标名,显示在文字前面 |
| title | string | - | 原生 title 属性,鼠标停留时由浏览器显示 |
| payload | any | - | 使用方自己带的附加数据,组件不读取 |
Events
| 事件 | 回调参数 | 说明 |
|---|---|---|
| update:modelValue | (value: string | number) | 用户换了选中项时触发,v-model 用的就是它 |
| change | (value: string | number) | 用户换了选中项时触发,参数是新选中项的 value。点已经选中的项不触发 |
类型
import type { SegmentedOption, SegmentedProps } from '@nscode/handy-ui'
interface SegmentedOption {
value: string | number
label?: string
disabled?: boolean
icon?: string
title?: string
payload?: any
}
interface SegmentedProps {
modelValue?: string | number
options?: SegmentedOption[] | string[] | number[]
size?: 'small' | 'default' | 'large'
disabled?: boolean
block?: boolean
/** 值变化时是否触发所在表单项的校验,默认 true */
validateEvent?: boolean
}
焦点方法与公开类型
组件ref使用SegmentedExpose。focus()聚焦当前、已选或首个可用控件,不改值、不打开文件选择框;禁用或卸载后不聚焦,只读按控件原合同保留可聚焦性。blur()只让本组件所属文档内的活动元素失焦,不影响外部焦点。组控件的focus/blur只在整组进出时各触发一次,内部移动不重复;既有表单校验行为保持。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSegmented, HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { SegmentedExpose } from '@nscode/handy-ui'
const control = ref<SegmentedExpose>()
const value = ref('B')
const event = ref('等待操作')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton @mousedown.prevent @click="control?.focus()">focus()</HuiButton>
<HuiButton @mousedown.prevent @click="control?.blur()">blur()</HuiButton>
</HuiSpace>
<HuiSegmented
ref="control"
v-model="value"
:options="['A', 'B']"
@focus="event = 'focus'"
@blur="event = 'blur'"
/>
<span>{{ event }}</span>
</HuiSpace>
</template>
| 方法 | 签名 |
|---|---|
| focus | () => void |
| blur | () => void |
整组件焦点事件
| 事件 | 参数 | 行为 |
|---|---|---|
| focus | event: FocusEvent | 焦点从组外进入时一次 |
| blur | event: FocusEvent | 焦点离开整组时一次,内部移动不重复 |