Skip to content

Checkbox 复选框 ​

uvx-checkbox 复选框,配合 uvx-checkbox-group 管理多选值,支持圆、方、卡片三种形状、禁用、自定义颜色、图标位置与插槽自定义。

平台兼容性 ​

AndroidiOS鸿蒙H5微信
3.9+4.11+4.61+4.0+4.41+

小程序端仅支持微信。

平台能力 ​

能力AndroidiOS鸿蒙H5微信
选择、禁用、形状(circle、square、card)√√√√√
v-model 双向绑定与 value 受控√√√√√
自定义颜色、图标位置、排列方式与下边框√√√√√
默认插槽与图标插槽自定义√√√√√

使用须知

  1. uvx-checkbox 必须在 uvx-checkbox-group 内使用,单独使用会抛出运行时错误。
  2. 选中值通过组的 v-model 绑定数组;v-model 与 value 同时传递时,非空的 v-model 优先。
  3. 单项的 disabled、label-disabled 只有传布尔值才生效,传空字符串等非布尔值时会回退组配置;组上的 label-disabled、border-bottom 支持布尔简写。
  4. checked 为 true 的复选框始终显示选中:组同步选中状态时会与 checked 取“或”,点击无法取消其选中显示。
  5. 点击图标或标签切换选中;shape="card" 时点击整块卡片均可切换;禁用项的所有点击无效。
  6. name 是写入组选中值的唯一标识,支持字符串、数字与布尔类型。

基础用法 ​

uvx-checkbox-group 通过 v-model 绑定选中值数组,placement="column" 设置纵向排列。

vue
<template>
   <uvx-checkbox-group v-model="checkboxValue" placement="column" @change="handleChange">
      <uvx-checkbox
         v-for="(item, index) in checkboxList"
         :key="index"
         :label="item.name"
         :name="item.name"
      ></uvx-checkbox>
   </uvx-checkbox-group>
</template>

<script lang="uts" setup>
type CheckboxValue = string | number | boolean;

type CheckboxItem = {
   name: string;
};

const checkboxValue = ref<CheckboxValue[]>(["苹果", "橙子"]);
const checkboxList: CheckboxItem[] = [
   { name: "苹果" },
   { name: "香蕉" },
   { name: "橙子" },
];

/**
 * 输出当前组的选中值
 * @param values 选中值
 * @returns null
 */
const handleChange = (values: CheckboxValue[]): void => {
   console.log("复选框选中值:", values);
};
</script>

自定义形状 ​

shape 支持 square(默认)、circle、card,设置在组上对全部复选框生效。

vue
<template>
   <uvx-checkbox-group v-model="checkboxValue" placement="column" shape="circle">
      <uvx-checkbox
         v-for="(item, index) in checkboxList"
         :key="index"
         :label="item.name"
         :name="item.name"
      ></uvx-checkbox>
   </uvx-checkbox-group>
</template>

禁用状态 ​

组的 disabled 禁用全部复选框,单项设置 disabled 只禁用当前项。

vue
<template>
   <uvx-checkbox-group v-model="checkboxValue" placement="column">
      <uvx-checkbox
         v-for="(item, index) in checkboxList"
         :key="index"
         :label="item.name"
         :name="item.name"
         :disabled="index == 0"
      ></uvx-checkbox>
   </uvx-checkbox-group>
</template>

禁止点击标签 ​

组的 label-disabled 为 true 时,点击标签文字不再切换状态,只能点击图标切换。

vue
<template>
   <uvx-checkbox-group v-model="checkboxValue" placement="column" label-disabled>
      <uvx-checkbox
         v-for="(item, index) in checkboxList"
         :key="index"
         :label="item.name"
         :name="item.name"
      ></uvx-checkbox>
   </uvx-checkbox-group>
</template>

卡片与自定义颜色 ​

shape="card" 时选中项以卡片形式高亮,单项通过 active-color 覆盖选中边框与勾选图标底色。

vue
<template>
   <uvx-checkbox-group v-model="checkboxValue" placement="column" shape="card">
      <uvx-checkbox
         v-for="(item, index) in checkboxList"
         :key="index"
         :active-color="item.color"
         :label="item.name"
         :name="item.name"
         :disabled="item.disabled"
      ></uvx-checkbox>
   </uvx-checkbox-group>
</template>

横向排列 ​

placement 默认为 row,复选框横向换行排列,适用于选项较多的场景。

vue
<template>
   <uvx-checkbox-group v-model="checkboxValue">
      <uvx-checkbox
         v-for="(item, index) in checkboxList"
         :key="index"
         :label="item.name"
         :name="item.name"
      ></uvx-checkbox>
   </uvx-checkbox-group>
</template>

图标右置与下边框 ​

icon-placement="right" 将勾选图标放到标签右侧并与标签两端对齐,配合 placement="column" 和 border-bottom 显示下边框。

vue
<template>
   <uvx-checkbox-group
      v-model="checkboxValue"
      placement="column"
      icon-placement="right"
      border-bottom
   >
      <uvx-checkbox
         v-for="(item, index) in checkboxList"
         :key="index"
         :label="item.name"
         :name="item.name"
      ></uvx-checkbox>
   </uvx-checkbox-group>
</template>

API ​

Props ​

属性说明类型默认值平台
name复选框标识符,选中后写入组选中值string | number | boolean""全部
shape形状,空值继承组配置circle | square | card""全部
size复选框尺寸,空值继承组配置string | number""全部
checked是否默认选中;为 true 时保持选中显示booleanfalse全部
disabled是否禁用,仅布尔值生效,非布尔值继承组配置string | boolean""全部
active-color选中颜色,空值继承组配置string""全部
inactive-color未选中颜色,空值继承组配置string""全部
icon-size勾选图标尺寸,空值继承组配置string | number""全部
icon-color勾选图标颜色,空值继承组配置string""全部
label标签文字string | number | boolean""全部
label-size标签字号,空值继承组配置string | number""全部
label-color标签颜色,禁用时使用未选中色,空值继承组配置string""全部
label-disabled是否禁止点击标签切换状态,仅布尔值生效,非布尔值继承组配置string | boolean""全部
u-style自定义样式,CSS 字符串string""全部
u-class自定义类名string""全部

Events ​

事件说明参数平台
change复选框状态变化时触发checked: boolean(切换后的选中状态)全部

Slots ​

名称说明平台
default自定义标签内容,使用后替换 label全部
icon自定义勾选图标全部

CheckboxGroup Props ​

属性说明类型默认值平台
value选中值数组,受控模式(string | number | boolean)[][]全部
model-valuev-model 选中值数组,非空时优先于 value(string | number | boolean)[][]全部
name表单标识符(当前版本未生效)string""全部
shape组内复选框形状circle | square | cardsquare全部
disabled是否禁用全部复选框booleanfalse全部
active-color选中颜色,空值跟随主题主色string""全部
inactive-color未选中颜色,空值跟随主题占位色string""全部
size复选框尺寸string | number18全部
placement排列方式row | columnrow全部
label-size标签字号string | number14全部
label-color标签颜色,空值跟随主题string""全部
label-disabled是否禁止点击标签切换状态booleanfalse全部
icon-color勾选图标颜色string#ffffff全部
icon-size勾选图标尺寸string | number12全部
icon-placement勾选图标位置left | rightleft全部
border-bottom纵向排列时是否显示下边框booleanfalse全部
u-style自定义样式,CSS 字符串string""全部
u-class自定义类名string""全部

CheckboxGroup Events ​

事件说明参数平台
update:modelValue更新双向绑定值values: (string | number | boolean)[](选中值数组)全部
input选中值变化时触发values: (string | number | boolean)[](选中值数组)全部
change选中值变化时触发values: (string | number | boolean)[](选中值数组)全部

参考 ​

uvx-ui · uni-app x 组件库