Skip to content

Checkbox 多选器

基本使用

html
<be-checkbox v-model="model" :options="options">
  <template #default="{ options, isChecked, selectItem }">
    <view class="checkbox1">
      <view
        class="item"
        :class="{ checked: isChecked(item) }"
        v-for="item in options"
        :key="item.value"
        @click="selectItem(item)"
      >
        {{ item.title }}
      </view>
    </view>
  </template>
</be-checkbox>
ts
import { ref } from 'vue';

const model = ref<number[]>([]);
const options = ref([
  { title: '选项1', value: 1 },
  { title: '选项2', value: 2 },
  { title: '选项3', value: 3 }
]);
scss
.checkbox1 {
  width: 100%;
  display: flex;
  align-items: center;

  .item {
    width: 50px;
    height: 50px;
    border-radius: 10rpx;
    background-color: #dddddd;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 12px;

    & + .item {
      margin-left: 10px;
    }

    &.checked {
      color: #4a68cc;
      border: 2px solid #4a68cc;
    }
  }
}

API

Props

属性名说明类型默认值
modelValue(v-model)当前选中项的 value 数组Array<string | number>[]
options选项数据Array<CheckboxOption>[]

Slots

插槽名说明slot props默认值
default视图{ options: Array<CheckboxOption>, isChecked: (item: CheckboxOption) => boolean, selectItem: (item: CheckboxOption) => void }

Types

类型名说明
CheckboxOption{ title: string, value: string | number, disabled?: boolean, desc?: object }-