Skip to content

Segmented

A segmented control for displaying multiple options and allowing users to select a single option.

When to Use

  • When you need to display multiple options and allow users to select a single option;
  • When switching between selected options changes the content of the associated area.

Component Types

Basic Usage

Set the options list through options and bind the currently selected item through v-model:value.

vue
<wd-segmented :options="list" v-model:value="current"></wd-segmented>
ts
const list = ref<string[]>(['Comments', 'Likes', 'Contributions', 'Tips'])

const current = ref('Likes')

Component States

Disabled

Set the disabled property to disable the entire segmented control.

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

Component Variants

Outline Theme

Switch to outline style through theme="outline".

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

Component Styles

Custom Render Segmented Labels

Use the label slot to customize the rendering of segmented control label content.

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: 'Li Lei',
    disabled: false,
    payload: {
      avatar: 'https://wot-ui.cn/assets/redpanda.jpg'
    }
  },
  {
    value: 'Han Meimei',
    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;
  }
}

Special Styles

Segmented with Vibration Effect

After setting vibrate-short, a short vibration feedback will be triggered when switching options.

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

Using in Popup

In WeChat Mini Program, when using this component in a popup, you need to call the updateActiveStyle method to update the segmented control style. See Common Problems.

html
<wd-button @click="handleClick">Open Popup</wd-button>
<wd-popup v-model="showPopup" position="bottom" @after-enter="handlePopupShow" closable custom-style="height: 200px;padding: 0 24rpx;">
  <view class="title">Using in Popup</view>
  <wd-segmented :options="list" v-model:value="current" ref="segmentedRef"></wd-segmented>
</wd-popup>
ts
const list = ref<string[]>(['Comments', 'Likes', 'Contributions', 'Tips'])
const current = ref('Likes')

const segmentedRef = ref<SegmentedInstance>() // Get segmented control instance
const showPopup = ref(false) // Control popup display
/**
 * Click button to open popup
 */
function handleClick() {
  showPopup.value = true
}
/**
 * Update segmented control style after popup opens
 */
function handlePopupShow() {
  segmentedRef.value?.updateActiveStyle()
}
css
.title {
  display: flex;
  font-size: 32rpx;
  align-items: center;
  justify-content: center;
  padding: 24rpx 0;
}

Attributes

ParameterDescriptionTypeDefault Value
value / v-model:valueCurrently selected valuestring | number-
disabledWhether to disable the segmented controlbooleanfalse
optionsData collectionstring[] | number[] | SegmentedOption[][]
themeSegmented control theme, optional values are card, outlinestring'card'
vibrate-shortWhether to trigger short vibration when switching optionsbooleanfalse
custom-classRoot node custom class namestring''
custom-styleRoot node custom stylestring''

SegmentedOption

ParameterDescriptionTypeDefault Value
valueOption valuestring | number-
disabledWhether to disable this optionbooleanfalse
payloadAdditional data, can be used for slot renderingany-

Events

Event NameDescriptionParameters
changeTriggered when option switchesSegmentedOption
click v1.2.20Triggered when option is clickedSegmentedOption

Methods

Method NameDescriptionType
updateActiveStyleUpdate slider offset, parameter animation is used to control whether to enable animation, enabled by default(animation?: boolean) => void

Slots

NameDescriptionParameters
labelOption label content{ 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轮廓模式选项激活禁用背景色

Source Code

Documentation
Component

Released under the MIT License.

Released under the MIT License.