Skip to content

CheckButton 复选框按钮组件

该组件内部实现以tag二次封装按钮复选框和单选框

温馨提示

本项目参考了 uView-Plus 开源项目的组件开发方式,基于 Vue 3 和 TypeScript 实现了自定义组件。目前该组件仍处于测试阶段。
感谢 uView-Plus 开源项目及其团队成员的贡献,他们的组件开发思路为本项目提供了宝贵地参考。如果需要了解更多组件开发细节,可以参考uView-Plus的 tag组件 的代码实现。

平台差异说明

APP(vue)H5微信小程序

基本使用示例

html
<!-- 全局使用 -->
<hy-check-button v-model="value" :columns="columns"></hy-check-button>
<!-- 单个组件引入 -->
<HyCheckButton v-model="value" :columns="columns"></HyCheckButton>
ts
import { reactive, ref } from "vue";
import { HyCheckButton } from "hy-app";

const value = ref("");
const columns = reactive([
    { label: "老师", value: 0 },
    { label: "护士", value: 1 },
    { label: "空姐", value: 2 },
    { label: "作家", value: 3 },
    { label: "网红", value: 4 },
    { label: "科学家", value: 5 },
]);

主题色

  • 通过type设置配置主题色
    • primary:信息按钮(默认)
    • success:主要按钮
    • info:默认按钮
    • warning:警告按钮
    • error:危险按钮
html
<hy-check-button
    v-model="value"
    :columns="columns"
    :type="type"
></hy-check-button>
ts
import { reactive, ref } from "vue";

const value = ref("");
const type = ref<HyApp.ThemeType>("primary");

const columns = reactive([
    { label: "老师", value: 0 },
    { label: "护士", value: 1 },
    { label: "空姐", value: 2 },
    { label: "作家", value: 3 },
    { label: "网红", value: 4 },
    { label: "科学家", value: 5 },
]);

配置按钮大小

  • 通过size设置配置按钮大小
    • large 大的
    • medium 中等的
    • small 小的
html
<hy-check-button
    v-model="value"
    :columns="columns"
    :type="type"
></hy-check-button>
ts
import { reactive, ref } from "vue";

const value = ref("");
const type = ref<HyApp.SizeType>("medium");

const columns = reactive([
    { label: "老师", value: 0 },
    { label: "护士", value: 1 },
    { label: "空姐", value: 2 },
    { label: "作家", value: 3 },
    { label: "网红", value: 4 },
    { label: "科学家", value: 5 },
]);

配置形状

  • 通过shape值设置按钮形状;
    • square为方形(默认)
    • circle为圆角
html
<hy-check-button
    v-model="value"
    :columns="columns"
    :shape="shape"
></hy-check-button>
ts
import { reactive, ref } from "vue";

const value = ref("");
const type = ref<HyApp.ShapeType>("square");

const columns = reactive([
    { label: "老师", value: 0 },
    { label: "护士", value: 1 },
    { label: "空姐", value: 2 },
    { label: "作家", value: 3 },
    { label: "网红", value: 4 },
    { label: "科学家", value: 5 },
]);

单选按钮

  • 通过selectType设置为radio
html
<hy-check-button
    v-model="value"
    :columns="columns"
    selectType="radio"
></hy-check-button>
ts
import { reactive, ref } from "vue";

const value = ref("");

const columns = reactive([
    { label: "老师", value: 0 },
    { label: "护士", value: 1 },
    { label: "空姐", value: 2 },
    { label: "作家", value: 3 },
    { label: "网红", value: 4 },
    { label: "科学家", value: 5 },
]);

多选按钮

  • 通过selectType设置为checkbox
html
<hy-check-button
    v-model="value"
    :columns="columns"
    selectType="checkbox"
></hy-check-button>
ts
import { reactive, ref } from "vue";

const value = ref([0]);

const columns = reactive([
    { label: "老师", value: 0 },
    { label: "护士", value: 1 },
    { label: "空姐", value: 2 },
    { label: "作家", value: 3 },
    { label: "网红", value: 4 },
    { label: "科学家", value: 5 },
]);

API

参数说明类型默认值
v-model选中得值[1]string|number| (string|number)[]-
columns列表数据array-
fieldNames自定义columns对应得键object{label: "label",value: "value",checked: "checked"}
selectType单选还是多选[2]checkbox|radiocheckbox
disabled禁用booleanfalse
col每行几列,每列等宽stringrepeat(3, 1fr)
gap设置每行间距,需要加单位string| number10px
type标签类型[3]error|warning|success |primary|infoprimary
size标签的大小[4]small|medium|largemedium
shapetag的形状[5]circle|squaresquare

columns

参数说明类型默认值
label显示文本内容string-
valuestring-
checked是否选中boolean-
disabled是否禁用boolean-

API

参数说明类型默认值
label自定义columns的文本键stringlabel
value自定义columns的值键stringvalue
checked自定义columns的选中键stringchecked

Events

事件名说明回调参数
click--

Slots

插槽名说明接收值
name--
00:46

  1. selectType设置为radio时候v-model必须传是字符串/数字,设置为checkbox时候v-model必须传数组 ↩︎

  2. checkbox:多选;radio:单选 ↩︎

  3. error:#fa3534;warning:#ff9900;success:#19be6b;primary:#2979ff; info:#909399; ↩︎

  4. normal:默认尺寸;large:大尺寸; small:小尺寸; ↩︎

  5. circle:两边半圆形; square:方形,带圆角 ↩︎