Skip to content

Checkbox 复选框

复选框用于在一组选项中进行多项选择,支持基础选中、禁用、半选等状态。

基础用法

最简单的复选框形式,通过 v-model 进行双向绑定。

vue
<template>
  <u-checkbox v-model="checked1">基础复选框</u-checkbox>
  <u-checkbox v-model="checked2">已选中</u-checkbox>
</template>

<script setup>
  import { ref } from 'vue';
  const checked1 = ref(false);
  const checked2 = ref(true);
</script>

不同颜色

复选框支持多种主题颜色:primary、success、warning、error、info。

vue
<template>
  <u-checkbox v-model="checked1" color="primary">Primary</u-checkbox>
  <u-checkbox v-model="checked2" color="success">Success</u-checkbox>
  <u-checkbox v-model="checked3" color="warning">Warning</u-checkbox>
  <u-checkbox v-model="checked4" color="error">Error</u-checkbox>
  <u-checkbox v-model="checked5" color="info">Info</u-checkbox>
</template>

<script setup>
  import { ref } from 'vue';
  const checked1 = ref(true);
  const checked2 = ref(true);
  const checked3 = ref(true);
  const checked4 = ref(true);
  const checked5 = ref(true);
</script>

禁用状态

通过 [disabled](file:///d:\github\ume-ui\packages\ume-ui\checkbox\src\types.ts#L3-L3) 属性禁用复选框。

vue
<template>
  <u-checkbox disabled>未选中禁用</u-checkbox>
  <u-checkbox v-model="checked" disabled>已选中禁用</u-checkbox>
</template>

<script setup>
  import { ref } from 'vue';
  const checked = ref(true);
</script>

半选状态

通过 [indeterminate](file:///d:\github\ume-ui\packages\ume-ui\checkbox\src\types.ts#L4-L4) 属性设置半选状态,用于实现全选/反选功能。

vue
<template>
  <u-checkbox v-model="checkAll" :indeterminate="isIndeterminate" @change="handleCheckAll">全选</u-checkbox>
  <u-checkbox v-model="checkedList[0]">选项 1</u-checkbox>
  <u-checkbox v-model="checkedList[1]">选项 2</u-checkbox>
  <u-checkbox v-model="checkedList[2]">选项 3</u-checkbox>
</template>

<script setup>
  import { ref, computed } from 'vue';
  
  const checkedList = ref([true, false, false]);
  
  const checkAll = ref(false);
  const isIndeterminate = computed(() => {
    const checkedCount = checkedList.value.filter(Boolean).length;
    return checkedCount > 0 && checkedCount < checkedList.value.length;
  });
  
  const handleCheckAll = (val) => {
    checkedList.value = [val, val, val];
  };
</script>

API

属性说明类型默认值
modelValue绑定值booleanfalse
color主题颜色primary | success | warning | error | infoprimary
disabled是否禁用booleanfalse
checked是否选中booleanfalse
indeterminate是否半选booleanfalse
label标签文本string-
namename 属性string-