CheckBox
示例
基本使用
<template>
<div>
<SuperCheckbox v-model="value" :options="options"></SuperCheckbox>
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
const value = ref([])
const options = [
{ label: '选项1', value: '1' },
{ label: '选项2', value: '2' },
{ label: '选项3', value: '3' },
]
</script>
不同类型展示状态
<template>
<div>
<div>默认样式: type="checkbox"</div>
<SuperCheckbox v-model="value" :options="options" type="checkbox"></SuperCheckbox>
<div>按钮样式: type="button"</div>
<SuperCheckbox v-model="value" :options="options" type="button"></SuperCheckbox>
<div>边框样式: border</div>
<SuperCheckbox v-model="value" :options="options" border></SuperCheckbox>
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
const value = ref([])
const options = [
{ label: '选项1', value: '1' },
{ label: '选项2', value: '2' },
{ label: '选项3', value: '3' },
]
</script>
label-添加前缀
<template>
<div>
<div>默认前缀: prefix</div>
<SuperCheckbox v-model="value" :options="options" prefix></SuperCheckbox>
<div>自定义前缀: prefix="测试前缀-"</div>
<SuperCheckbox v-model="value" :options="options" prefix="测试前缀-"></SuperCheckbox>
<div>函数前缀: :prefix="(item: { label: string; value: string }) => `函数前缀-${item.value}-`"</div>
<SuperCheckbox v-model="value" :options="options" :prefix="(item: { label: string; value: string }) => `函数前缀-${item.value}-`"
></SuperCheckbox>
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
const value = ref([])
const options = [
{ label: '选项1', value: '1' },
{ label: '选项2', value: '2' },
{ label: '选项3', value: '3' },
]
</script>
禁用状态
<template>
<div>
<div>全部禁用</div>
<SuperCheckbox v-model="value" :options="options" disabled></SuperCheckbox>
<div>选项禁用</div>
<SuperCheckbox v-model="value" :options="options" :disabled="isDisabled"></SuperCheckbox>
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
const value = ref([])
const options = [
{ label: '选项1', value: '1' },
{ label: '选项2', value: '2' },
{ label: '选项3', value: '3' },
]
const isDisabled = (item: { value: string }) => item.value === '2'
</script>
包含/排除选项
<template>
<div>
<div>包含选项: include="['1', '3']"</div>
<SuperCheckbox v-model="value" :options="options" :include="['1', '3']"></SuperCheckbox>
<div>排除选项: exclude="['1', '3']"</div>
<SuperCheckbox v-model="value" :options="options" :exclude="['1', '3']"></SuperCheckbox>
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
const value = ref([])
const options = [
{ label: '选项1', value: '1' },
{ label: '选项2', value: '2' },
{ label: '选项3', value: '3' },
]
</script>
显示全选按钮
<template>
<div>
<SuperCheckbox v-model="value" :options="options" hasCheckAll></SuperCheckbox>
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
const value = ref([])
const options = [
{ label: '选项1', value: '1' },
{ label: '选项2', value: '2' },
{ label: '选项3', value: '3' },
]
</script>
选项互斥
<template>
<div>
<div>互斥选项: :exclusion="[{ value: '2', exclusion: '3' },{ value: '4', exclusion: ['3', '5'] },]"</div>
<SuperCheckbox
v-model="value"
:options="options"
hasCheckAll
:exclusion="[
{ value: '2', exclusion: '3' },
{ value: '4', exclusion: ['3', '5'] },
]"
></SuperCheckbox>
<div>互斥选项: 2</div>
<SuperCheckbox
v-model="value"
:options="options"
hasCheckAll
:exclusion="'2'"
></SuperCheckbox>
<div>所选中的值:{{ value }}</div>
</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const value = ref([])
const options = [
{ value: '1', label: '电视机' },
{ value: '2', label: '冰箱' },
{ value: '3', label: '洗衣机' },
{ value: '4', label: '空调' },
{ value: '5', label: '微波炉' },
{ value: '6', label: '电饭煲' },
{ value: '99', label: '其他' },
]
</script>
API
Props
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
modelValue | 绑定值 | Array<any> | - | - |
type | 按钮样式 | string | 'checkbox' | 'button' | checkbox |
size | 尺寸 | string | small | default | large | default |
border | 是否显示边框 | boolean | true|false | false |
options | 选项数据源 | Array<any> | - | - |
labelKey | 自定义label字段属性名 | string | - | label |
valueKey | 自定义value字段属性名 | string | - | value |
prefix | 是否使用前缀 | boolean | ((item: any) => boolean) | - | - |
disabled | 是否禁用 | boolean | ((item: any) => boolean) | - | - |
include | 包含的选项 | Array<string | number> | - | - |
exclude | 排除的选项 | Array<string | number> | - | - |
hasCheckAll | 是否显示全选 | boolean | true/false | false |
valueKeys | 选中项的值类型配置 | { valueKey: string; labelKey: string } | - | - |
exclusion | 选项互斥 | number | string | { value: string; exclusion: string} | Array<{ value: string; exclusion: string}>[] | - | - |
exclusion 属性用法说明
exclusion 四种格式
- number/string 当为 number 或者 字符串时,exclusion 的值与其他项互斥
- map 当为 map 对象时,格式为{ value: "2", exclusion: "3" }选中 2 则 3 取消选中,选中 3 则 2 取消选中 或者 { value: "2", exclusion: ["3", "4"] } ;选中 2 则 3、4 取消选中,选中 3 或者 4 则 2 取消选中
- array 当为 array 时,格式为[{ value: "2", exclusion: "3" }, { value: "4", exclusion: ["3", "5"] }] 选中 2 时,3 取消选中,选择 4 时,3 和 5 取消选中
Events
事件名 | 说明 | 回调参数 |
---|---|---|
change | 当绑定值变化时触发的事件 | Function <t-tip content='(value:[string/number], result: values[]) => void |
'/> |
Slots
插槽名 | 说明 |
---|---|
default | 自定义默认内容 |