Skip to content

Checkbox 复选框

介绍

在一组选项中进行多选,通常与 TnCheckboxGroup 配合使用。支持自定义颜色、形状、数量限制、半选状态及边框样式。

引入

typescript
import { TnCheckbox, TnCheckboxGroup } from "@tuniao/tn-ui";

代码演示

基础用法

通过 TnCheckboxGroup 包裹 TnCheckbox,使用 modelValue 绑定当前选中值数组。每个 TnCheckbox 通过 label 属性指定对应值。

基础用法

点我查看代码
typescript
@Local checkboxValue: Array<string> = ["a"];

TnCheckboxGroup({
  group: "fruit",
  modelValue: this.checkboxValue,
  onChange: (value: Array<string | number>) => {
    this.checkboxValue = value as Array<string>;
  },
}) {
  TnCheckbox({ group: "fruit", label: "a" }) {
    Text("选项A")
  }
  TnCheckbox({ group: "fruit", label: "b" }) {
    Text("选项B")
  }
  TnCheckbox({ group: "fruit", label: "c" }) {
    Text("选项C")
  }
  TnCheckbox({ group: "fruit", label: "d" }) {
    Text("选项D")
  }
}

自定义颜色

通过 activeColor 属性自定义选中时的颜色,可在 TnCheckboxGroup 上统一设置,也可在单个 TnCheckbox 上单独设置。

自定义颜色

点我查看代码
typescript
@Local checkboxValue: Array<string> = ["a"];

TnCheckboxGroup({
  group: "customColor",
  modelValue: this.checkboxValue,
  activeColor: "danger",
  onChange: (value: Array<string | number>) => {
    this.checkboxValue = value as Array<string>;
  },
}) {
  TnCheckbox({ group: "customColor", label: "a" }) {
    Text("选项A");
  };
  TnCheckbox({ group: "customColor", label: "b" }) {
    Text("选项B");
  };
  TnCheckbox({ group: "customColor", label: "c" }) {
    Text("选项C");
  };
  TnCheckbox({ group: "customColor", label: "d" }) {
    Text("选项D");
  };
}

形状

通过 shape 属性设置复选框形状,可选值为 square(方形,默认)和 circle(圆形)。

形状

点我查看代码
typescript
@Local checkboxValue: Array<string> = ["a","c"];

TnCheckboxGroup({
  group: "shape",
  modelValue: this.checkboxValue,
  wrap: true,
  onChange: (value: Array<string | number>) => {
    this.checkboxValue = value as Array<string>;
  },
}) {
  TnCheckbox({ group: "shape", label: "a", shape: TnCheckboxShape.SQUARE }) {
    Text("方形打勾");
  };
  TnCheckbox({ group: "shape", label: "b", shape: TnCheckboxShape.SQUARE }) {
    Text("方形打勾");
  };
  TnCheckbox({ group: "shape", label: "c", shape: TnCheckboxShape.CIRCLE }) {
    Text("圆形圆点");
  };
  TnCheckbox({ group: "shape", label: "d", shape: TnCheckboxShape.CIRCLE }) {
    Text("圆形圆点");
  };
}

限制数量

通过 minmax 属性限制可选数量。

限制数量

点我查看代码
typescript
@Local checkboxValue: Array<string> = ["volleyball"];

TnCheckboxGroup({
  group: "maxLimit",
  modelValue: this.checkboxValue,
  min: 1,
  max: 2,
  onChange: (value: Array<string | number>) => {
    this.checkboxValue = value as Array<string>;
  },
}) {
  TnCheckbox({ group: "maxLimit", label: "football" }) {
    Text("足球");
  };
  TnCheckbox({ group: "maxLimit", label: "basketball" }) {
    Text("篮球");
  };
  TnCheckbox({ group: "maxLimit", label: "volleyball" }) {
    Text("排球");
  };
  TnCheckbox({ group: "maxLimit", label: "pingpong" }) {
    Text("乒乓球");
  };
}

半选状态

通过 indeterminate 属性设置复选框为半选状态,常用于实现全选效果。

点我查看代码
typescript
@Local checkAll: boolean = false;
@Local isIndeterminate: boolean = true;
@Local checkboxValue: Array<string> = ["apple"];
private allOptions: Array<string> = ["apple", "banana", "orange"];

TnCheckbox({
  group: "all",
  label: "all",
  indeterminate: this.isIndeterminate,
  onChange: () => {
    if (this.checkAll) {
      this.checkboxValue = [];
      this.checkAll = false;
    } else {
      this.checkboxValue = [...this.allOptions];
      this.checkAll = true;
    }
    this.isIndeterminate = false;
  },
}) {
  Text("全选")
}

TnCheckboxGroup({
  group: "fruit",
  modelValue: this.checkboxValue,
  onChange: (value: Array<string | number>) => {
    this.checkboxValue = value as Array<string>;
    this.checkAll = value.length === this.allOptions.length;
    this.isIndeterminate = value.length > 0 && value.length < this.allOptions.length;
  },
}) {
  TnCheckbox({ group: "fruit", label: "apple" }) {
    Text("苹果")
  }
  TnCheckbox({ group: "fruit", label: "banana" }) {
    Text("香蕉")
  }
  TnCheckbox({ group: "fruit", label: "orange" }) {
    Text("橘子")
  }
}

边框样式

通过 showBorder 属性开启边框样式,选中时边框高亮显示。

边框样式

点我查看代码
typescript
@Local checkboxValue: Array<string> = ["a"];

TnCheckboxGroup({
  group: "border",
  modelValue: this.checkboxValue,
  wrap: true,
  onChange: (value: Array<string | number>) => {
    this.checkboxValue = value as Array<string>;
  },
}) {
  TnCheckbox({ group: "border", label: "a", showBorder: true }) {
    Text("选项A");
  };
  TnCheckbox({ group: "border", label: "b", showBorder: true }) {
    Text("选项B");
  };
  TnCheckbox({ group: "border", label: "c", showBorder: true }) {
    Text("选项C");
  };
  TnCheckbox({ group: "border", label: "d", showBorder: true }) {
    Text("选项D");
  };
}

禁用状态

通过 disabled 属性禁用复选框。可在 TnCheckboxGroup 上统一设置,也可在单个 TnCheckbox 上单独设置。

禁用状态

点我查看代码
typescript
@Local checkboxValue: Array<string> = ["a", "c"];

// 全部禁用
TnCheckboxGroup({
  group: "disabled",
  modelValue: this.checkboxValue,
  disabled: true,
}) {
  TnCheckbox({ group: "disabled", label: "a" }) {
    Text("选项A(已选)");
  };
  TnCheckbox({ group: "disabled", label: "b" }) {
    Text("选项B(未选)");
  };
  TnCheckbox({ group: "disabled", label: "c" }) {
    Text("选项C(已选)");
  };
  TnCheckbox({ group: "disabled", label: "d" }) {
    Text("选项D(未选)");
  };
}

// 单个禁用
TnCheckboxGroup({
  group: "fruit2",
  modelValue: this.checkboxValue,
  onChange: (value: Array<string | number>) => {
    this.checkboxValue = value as Array<string>;
  },
}) {
  TnCheckbox({ group: "fruit2", label: "apple" }) {
    Text("苹果")
  }
  TnCheckbox({ group: "fruit2", label: "banana", disabled: true }) {
    Text("香蕉(禁用)")
  }
}

API

TnCheckboxGroup Props

参数说明类型默认值
group复选框组标识string-
modelValue当前选中值数组Array<string | number>-
disabled是否禁用整组booleanfalse
wrap是否水平排列并换行booleanfalse
activeColor选中时的颜色string-
groupSize复选框组统一大小number-
min最少可选数量number0
max最多可选数量numberInfinity

TnCheckbox Props

参数说明类型默认值
group所属复选框组标识string-
label复选框对应的值TnCheckboxValueType-
disabled是否禁用booleanfalse
labelDisabled是否禁用文字点击切换booleanfalse
activeColor选中时的颜色string-
checkboxSize复选框大小number-
showBorder是否显示边框booleanfalse
indeterminate是否为半选状态booleanfalse
shape复选框形状TnCheckboxShape"square"

TnCheckboxShape

typescript
type TnCheckboxShape = "square" | "circle";

TnCheckboxGroup Events

事件名说明回调参数
onChange选中值变化时触发(value: Array<string | number>) => void

TnCheckbox Slots

名称说明
defaultBuilder自定义复选框标签内容,通过尾随闭包传入

TnCheckbox Events

事件名说明回调参数
onChange选中状态变化时触发(value: TnCheckboxValueType) => void

类型定义

typescript
type TnCheckboxValueType = string | number;