Skip to content

xtf-checkbox-group

组件说明

xtf-checkbox-group 是复选框组容器组件,用于管理多个 xtf-checkbox 的选中值。支持 options 快速生成选项、方向布局、最大/最小选中数限制、变更拦截和全选/反选方法,是表单多选场景的核心组件。


基础用法

1. 最简示例

通过 v-model 绑定选中值数组,包裹 xtf-checkbox 并用 name 标识:

vue
<template>
  <xtf-checkbox-group v-model="selected" direction="column">
    <xtf-checkbox name="apple" label="苹果" />
    <xtf-checkbox name="banana" label="香蕉" />
    <xtf-checkbox name="orange" label="橙子" />
  </xtf-checkbox-group>
</template>

<script>
export default {
  data() {
    return {
      selected: ['apple']
    }
  }
}
</script>

2. Options 快速生成

通过 options 属性直接传入选项数组,无需手动写 xtf-checkbox

vue
<template>
  <xtf-checkbox-group v-model="selected" :options="fruitOptions" direction="row" :gap="24" />
</template>

<script>
export default {
  data() {
    return {
      selected: ['apple'],
      fruitOptions: [
        { label: '苹果', value: 'apple' },
        { label: '香蕉', value: 'banana' },
        { label: '橙子', value: 'orange' },
        { label: '葡萄', value: 'grape', disabled: true }
      ]
    }
  }
}
</script>

3. 方向与间距

通过 direction 切换排列方向,通过 gap 调整间距:

vue
<template>
  <view style="display: flex; flex-direction: column; gap: 24rpx">
    <xtf-text level="caption" color="secondary">水平排列</xtf-text>
    <xtf-checkbox-group v-model="h1" direction="row" :gap="32">
      <xtf-checkbox name="a" label="选项A" />
      <xtf-checkbox name="b" label="选项B" />
      <xtf-checkbox name="c" label="选项C" />
    </xtf-checkbox-group>

    <xtf-text level="caption" color="secondary">垂直排列</xtf-text>
    <xtf-checkbox-group v-model="h2" direction="column" :gap="16">
      <xtf-checkbox name="a" label="选项A" />
      <xtf-checkbox name="b" label="选项B" />
      <xtf-checkbox name="c" label="选项C" />
    </xtf-checkbox-group>
  </view>
</template>

<script>
export default {
  data() {
    return {
      h1: ['a'],
      h2: ['a']
    }
  }
}
</script>

4. 选中数限制与全选

通过 max / min 限制选中数量,通过 checkAll 方法实现全选/反选:

vue
<template>
  <view>
    <xtf-button-group :gap="12" style="margin-bottom: 16rpx">
      <xtf-button label="全选" size="sm" type="light" @click="selectAll" />
      <xtf-button label="反选" size="sm" type="light" @click="deselectAll" />
    </xtf-button-group>
    <xtf-checkbox-group ref="checkGroup" v-model="selected" :max="3" :min="1" direction="column">
      <xtf-checkbox name="reading" label="阅读" />
      <xtf-checkbox name="music" label="音乐" />
      <xtf-checkbox name="sports" label="运动" />
      <xtf-checkbox name="travel" label="旅行" />
      <xtf-checkbox name="cooking" label="烹饪" />
    </xtf-checkbox-group>
    <xtf-text level="caption" color="secondary">最多选3项,最少选1项</xtf-text>
  </view>
</template>

<script>
export default {
  data() {
    return {
      selected: ['reading']
    }
  },
  methods: {
    selectAll() {
      this.$refs.checkGroup.checkAll(true)
    },
    deselectAll() {
      this.$refs.checkGroup.checkAll(false)
    }
  }
}
</script>

全部属性

属性类型默认值作用描述适用范围
modelValueArrayundefined绑定值(Vue 3)双向绑定
valueArray[]绑定值(Vue 2)双向绑定
nameString''表单字段名表单提交
optionsArray[]选项数组,支持字符串或 { label, value, disabled } 对象快速生成
optionLabelKeyString'label'选项文字字段名自定义字段映射
optionValueKeyString'value'选项值字段名自定义字段映射
optionDisabledKeyString'disabled'选项禁用字段名自定义字段映射
disabledBooleanfalse是否全局禁用禁用交互
themeString'primary'主题色切换主题色
colorString''自定义颜色自定义色
activeColorString''选中时颜色自定义选中色
variantString'default'子复选框变体统一视觉风格
checkAnimationString'scale'子复选框勾选动画统一动画
iconSizeString | Number''子复选框图标大小统一图标大小
checkedIconString''子复选框选中图标统一选中图标
uncheckedIconString''子复选框未选图标统一未选图标
indeterminateIconString''子复选框半选图标统一半选图标
sizeString'md'子复选框尺寸:'sm' / 'md' / 'lg'统一大小
shapeString''子复选框形状统一形状
directionString'column'排列方向:'column' / 'row'控制方向
gapString | Number20子项间距(rpx)调整间距
maxString | Number0最大选中数,0 不限制限制选中数
minString | Number0最小选中数,0 不限制限制选中数
beforeChangeFunctionnull变更拦截函数变更拦截
customClassString''自定义类名样式覆盖
customStyleString | Object''自定义样式动态样式覆盖

事件

事件名称触发时机回调参数参数说明
change选中值变更时触发(value: Array)新的选中值数组
inputVue 2 双向绑定更新(value: Array)新的选中值数组
update:modelValueVue 3 双向绑定更新(value: Array)新的选中值数组

事件使用示例

vue
<template>
  <xtf-checkbox-group v-model="selected" :options="options" @input="onInput" @change="onChange" />
</template>

<script>
export default {
  data() {
    return { selected: [], options: [{ label: '阅读', value: 'reading' }] }
  },
  methods: {
    onInput(value) {
      console.log('绑定值', value)
    },
    onChange(value) {
      console.log('已选择', value)
    }
  }
}
</script>

插槽

插槽名说明
默认插槽未提供 options 时,放置 xtf-checkbox 子组件。

方法

方法名参数返回值说明
checkAll(checked: Boolean, values?: Array)Promise<Boolean>全选或反选,可指定特定值列表
updateValue(optionValue, checked: Boolean)Promise<Boolean>手动更新某个选项的选中状态

主题说明

  • 子项间距使用 --xtf-checkbox-group-gap
  • 方向布局通过 flex-direction 控制

如需全局调整主题,请在 xtf-config-provider 或主题配置中覆盖上述 CSS 变量。

MIT Licensed