# t-checkbox
Checkbox 多选框
t-checkbox 组件介绍
t-checkbox 是一个多选复选框组件,支持自定义样式、状态和事件自定义图标,适用于各种表单场景。组件提供了多种模式(复选框、按钮、单元格),支持主题类型、尺寸、禁用状态等配置,可与 t-checkbox-group 组件配合使用,实现多选功能。
t-checkbox 平台兼容
| Harmony | H5 | Android | iOS | 小程序 | UTS |
|---|---|---|---|---|---|
| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
t-checkbox 基础用法
基础用法参考 demo/checkbox/checkbox.uvue,通过 checked 属性设置选中状态,使用 label 属性设置标签文本。
vue
<template>
<view class="p-15">
<t-section title="主题-type-开启动画"
class="mb-15"></t-section>
<view :class="[`fl mb-15 fww tvg r-10`]">
<t-checkbox :transition="true"
@change="onChange">
<template #label>
<t-text-light class="ml-8"
:patternsValue="['用户隐私协议']"
patternsColor="#2979ff">登录、注册代表您同意【用户隐私协议】</t-text-light>
</template>
</t-checkbox>
</view>
<t-section title="size-large"
class="mb-15 fww"></t-section>
<view :class="[`fl mb-15 fww tvg r-10`]">
<t-checkbox :checked="true"
type="primary"
size="large">石榴</t-checkbox>
<t-checkbox type="primary"
size="large">橙子</t-checkbox>
<t-checkbox type="primary"
size="large">香蕉</t-checkbox>
<t-checkbox type="primary"
size="large">橙子</t-checkbox>
</view>
<t-section title="size-medium"
class="mb-15 fww"></t-section>
<view :class="[`fl mb-15 fww tvg r-10`]">
<t-checkbox type="primary"
size="medium">石榴</t-checkbox>
<t-checkbox type="primary"
size="medium">橙子</t-checkbox>
<t-checkbox type="primary"
size="medium">香蕉</t-checkbox>
<t-checkbox type="primary"
size="medium"
:disabled="true">橙子</t-checkbox>
</view>
<t-section title="size-small"
class="mb-15 fww"></t-section>
<view :class="[`fl mb-15 fww tvg r-10`]">
<t-checkbox type="primary"
size="small">石榴</t-checkbox>
<t-checkbox type="primary"
size="small">橙子</t-checkbox>
<t-checkbox type="primary"
size="small">香蕉</t-checkbox>
<t-checkbox type="primary"
size="small">橙子</t-checkbox>
</view>
<t-section title="size-mini"
class="mb-15 fww"></t-section>
<view :class="[`fl mb-15 fww tvg r-10`]">
<t-checkbox type="primary"
size="mini">石榴</t-checkbox>
<t-checkbox type="primary"
size="mini">橙子</t-checkbox>
<t-checkbox type="primary"
size="mini">香蕉</t-checkbox>
<t-checkbox type="primary"
size="mini">橙子</t-checkbox>
</view>
<view :class="[`fl mb-15 fww tvg r-10`]">
<t-section title="自定义图标 自定义样式 "
class="mb-15"></t-section>
<t-checkbox type="primary"
:transition="true"
label-class="c-#666">梨子</t-checkbox>
<t-checkbox type="warning"
:transition="true"
active-class="bg-blue bw-0!"
active-label-class="c-red s-10"
inactive-label-class="c-blue s-15">梨子</t-checkbox>
</view>
<t-section title="动态增删"
class="mb-15"></t-section>
<view :class="[`fl mb-15 fww tvg r-10`]">
<t-checkbox v-for="(item,index) in list"
:key="index"
type="primary"
size="large"
:transition="true">{{item.label}}</t-checkbox>
</view>
<view class="fl">
<t-button class="mr-10"
type="primary"
@click="add">增加</t-button>
<t-button type="error"
@click="del">删除</t-button>
</view>
<t-section title="按钮模式"
class="mtb-15"></t-section>
<view :class="[`fl mb-15 fww tvg r-10 p-10`]">
<t-checkbox class="mrb-10"
size="large"
active-class="bg-#fff"
inactive-class="bg-#000"
active-label-class="c-#000"
inactive-label-class="c-#fff"
type="primary"
mode="button">石榴</t-checkbox>
<t-checkbox class="mrb-10"
size="small"
type="success"
mode="button"
effect="dark">香蕉</t-checkbox>
<t-checkbox class="mrb-10"
size="mini"
type="error"
mode="button">橙子</t-checkbox>
<t-checkbox class="mrb-10"
size="mini"
type="warning"
mode="button">梨子</t-checkbox>
</view>
<view :class="[`mb-15 fww tvg r-10`]">
<t-checkbox class="mb-5"
type="primary"
mode="button"
size="large">石榴</t-checkbox>
<t-checkbox class="mb-5"
type="warning"
mode="button"
size="medium">橙子</t-checkbox>
<t-checkbox class="mb-5"
type="success"
mode="button"
size="small">香蕉</t-checkbox>
<t-checkbox class="mb-5"
type="error"
mode="button"
size="mini">橙子</t-checkbox>
<t-checkbox class="mb-5"
type="info"
mode="button"
size="mini">梨子</t-checkbox>
</view>
<t-section title="列表模式"
class="mtb-15"></t-section>
<view :class="[`tvg r-10`]">
<t-checkbox size="large"
type="primary"
mode="cell"
:transition="true">石榴</t-checkbox>
<t-checkbox size="large"
type="warning"
icon="thumb-up"
mode="cell"
:transition="true">橙子</t-checkbox>
<t-checkbox size="large"
type="success"
mode="cell">香蕉</t-checkbox>
<t-checkbox size="large"
type="error"
mode="cell"
:transition="true">橙子</t-checkbox>
<t-checkbox size="large"
type="warning"
mode="cell"
:transition="true">梨子</t-checkbox>
</view>
<tui-theme-switch></tui-theme-switch>
</view>
</template>
<script setup>
import { TuiGuid } from '@/uni_modules/tui-common'
type radiolisttype = {
value : string
label : string
}
const checked = ref<boolean>(false)
const list = ref<radiolisttype[]>([
{ value: '1', label: '石榴' },
{ value: '2', label: '橙子' },
{ value: '3', label: '香蕉' },
{ value: '4', label: '橙子' },
{ value: '5', label: '梨子' }
] as radiolisttype[])
function add() {
const a : radiolisttype = {
value: TuiGuid(10),
label: TuiGuid(10)
}
list.value.push(a)
}
function del() {
list.value.splice(1, 1)
}
function onChange(e : boolean) {
console.log(e)
}
</script>t-checkbox Props
| 属性名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
path | 点击组件后跳转的页面路径;为空时仅响应事件 | string | '' |
hover | 是否启用点击效果 | boolean | false |
type | 组件主题类型,可选 info、primary、error、warning、success | string | '' |
disabled | 是否禁用 | boolean | false |
effect | 组件显示主题,可选 normal、dark、light、plain | string | '' |
size | 组件尺寸,可选 large、medium、small、mini | string | '' |
transition | 是否开启图标动画 | boolean | false |
duration | 图标动画时间 | number | 200 |
border | 是否显示边框 | boolean | true |
checked | 是否被选中 | boolean | false |
value | 当前选中的值 | string | '' |
label | 标签 | string | '' |
labelClass | 标签的样式 | any | '' |
icon | 图标 | string | 'check-fill' |
iconBoxClass | 图标盒子的样式 | any | '' |
activeClass | 被选中时的样式 | any | '' |
inactiveClass | 未选中时的样式 | any | '' |
iconClass | 图标样式 | any | '' |
activeIconClass | 被选中时图标的样式 | any | '' |
inactiveIconClass | 未被选中时图标的样式 | any | '' |
activeLabelClass | 被选中时标签的样式 | any | '' |
inactiveLabelClass | 未被选中时标签的样式 | any | '' |
mode | 筛选框的样式,可选 checkbox、cell、button | string | 'checkbox' |
t-checkbox Events
| 事件名 | 说明 | 回调参数 |
|---|---|---|
change | 复选框状态变化时触发 | boolean |
select | 选择时触发 | boolean |
click | 点击时触发 | UniPointerEvent |
initFinished | 组件初始化完成时触发,返回组件节点信息 | NodeInfo |
touchstart | 触摸开始时触发 | - |
touchend | 触摸结束时触发 | - |
touchmove | 触摸移动时触发 | - |
touchcancel | 触摸取消时触发 | - |
transitionend | 动画结束时触发 | - |
t-checkbox Slots
| 插槽名 | 说明 |
|---|---|
default | 默认插槽,用于自定义标签内容 |
label | 标签插槽,用于自定义标签的显示 |