Skip to content

Segmented 分段器

分段器用于展示多个选项并允许用户选择其中单个选项。

何时使用

  • 用于展示多个选项并允许用户选择其中单个选项;
  • 当切换选中选项时,关联区域的内容会发生变化。

组件类型

基本用法

通过 options 设置选项列表,通过 v-model:value 绑定当前选中项。

vue
<wd-segmented :options="list" v-model:value="current"></wd-segmented>
ts
const list = ref<string[]>(['评论', '点赞', '贡献', '打赏'])

const current = ref('点赞')

组件状态

禁用

设置 disabled 属性禁用整个分段器。

html
<wd-segmented :options="list" v-model:value="current" disabled></wd-segmented>

组件变体

轮廓主题

通过 theme="outline" 切换为轮廓样式。

html
<wd-segmented :options="list" v-model:value="current" theme="outline"></wd-segmented>

组件样式

自定义渲染分段器标签

使用插槽 label 可以自定义渲染分段器的标签内容。

html
<wd-segmented :options="list" v-model:value="current" :vibrate-short="true">
  <template #label="{ option }">
    <view class="section-slot">
      <image style="border-radius: 50%; width: 32px; height: 32px" :src="option.payload.avatar" />
      <view class="name">{{ option.value }}</view>
    </view>
  </template>
</wd-segmented>
ts
const list = ref([
  {
    value: '李雷',
    disabled: false,
    payload: {
      avatar: 'https://wot-ui.cn/assets/redpanda.jpg'
    }
  },
  {
    value: '韩梅梅',
    disabled: false,
    payload: {
      avatar: 'https://wot-ui.cn/assets/capybara.jpg'
    }
  }
])
scss
.section {
  width: 100%;
  padding: 0 24rpx;
  box-sizing: border-box;
  &-slot {
    padding: 4px;
  }
}

特殊样式

带振动效果的分段器

设置 vibrate-short 后,切换选项时会触发短振动反馈。

html
<wd-segmented :options="list" v-model:value="current" :vibrate-short="true"></wd-segmented>

在弹出框中使用

微信小程序端,在弹出框中使用本组件时,需要调用 updateActiveStyle 方法更新分段器样式,参见常见问题

html
<wd-button @click="handleClick">打开弹窗</wd-button>
<wd-popup v-model="showPopup" position="bottom" @after-enter="handlePopupShow" closable custom-style="height: 200px;padding: 0 24rpx;">
  <view class="title">在弹出框中使用</view>
  <wd-segmented :options="list" v-model:value="current" ref="segmentedRef"></wd-segmented>
</wd-popup>
ts
const list = ref<string[]>(['评论', '点赞', '贡献', '打赏'])
const current = ref('点赞')

const segmentedRef = ref<SegmentedInstance>() // 获取分段器实例
const showPopup = ref(false) // 控制popup显示
/**
 * 点击按钮打开popup
 */
function handleClick() {
  showPopup.value = true
}
/**
 * popup打开后更新分段器样式
 */
function handlePopupShow() {
  segmentedRef.value?.updateActiveStyle()
}
css
.title {
  display: flex;
  font-size: 32rpx;
  align-items: center;
  justify-content: center;
  padding: 24rpx 0;
}

Attributes

参数说明类型默认值
value / v-model:value当前选中的值string | number-
disabled是否禁用分段器booleanfalse
options数据集合string[] | number[] | SegmentedOption[][]
theme分段器主题,可选值为 cardoutlinestring'card'
vibrate-short切换选项时是否触发短振动booleanfalse
custom-class根节点自定义类名string''
custom-style根节点自定义样式string''

SegmentedOption

参数说明类型默认值
value选项值string | number-
disabled是否禁用该选项booleanfalse
payload附加数据,可用于插槽渲染any-

Events

事件名称说明参数
change选项切换时触发SegmentedOption
click v1.2.20选项点击时触发SegmentedOption

Methods

方法名说明类型
updateActiveStyle更新滑块偏移量,参数 animation 用于控制是否启用动画,默认开启(animation?: boolean) => void

Slots

name说明参数
label选项标签内容{ option: SegmentedOption }

主题定制

CSS 变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 全局配置

名称默认值描述
--wot-segmented-bg$filled-content组件背景色
--wot-segmented-border-width$stroke-main组件边框宽度
--wot-segmented-padding$padding-super-tight组件内边距
--wot-segmented-radius$radius-main组件圆角
--wot-segmented-item-color$text-secondary选项文字颜色
--wot-segmented-item-font-size$typography-body-size-extra-large选项文字字号
--wot-segmented-item-line-height$typography-body-line--height-size-extra-large选项文字行高
--wot-segmented-item-padding$padding-ultra-tight $padding-loose选项内边距
--wot-segmented-item-font-weight$font-weight-regular选项文字字重
--wot-segmented-item-font-weight-active$font-weight-medium选项激活字重
--wot-segmented-item-bg-active$filled-oppo选项激活背景色
--wot-segmented-item-color-active$primary-6选项激活文字颜色
--wot-segmented-item-disabled-opacity$opacity-disabled选项禁用透明度
--wot-segmented-outline-border-color$border-main轮廓模式边框颜色
--wot-segmented-outline-radius$radius-zero轮廓模式圆角
--wot-segmented-outline-bg$filled-zero轮廓模式背景色
--wot-segmented-outline-padding$padding-zero轮廓模式内边距
--wot-segmented-outline-item-padding$padding-extra-tight $padding-extra-loose轮廓模式选项内边距
--wot-segmented-outline-item-color$text-secondary轮廓模式选项文字颜色
--wot-segmented-outline-item-bg-active$primary-6轮廓模式选项激活背景色
--wot-segmented-outline-item-color-active$text-white轮廓模式选项激活文字颜色
--wot-segmented-outline-item-disabled-color$text-disabled轮廓模式选项禁用文字颜色
--wot-segmented-outline-item-bg-active-disabled$filled-content轮廓模式选项激活禁用背景色

源代码

文档
组件

Released under the MIT License.

Released under the MIT License.