Skip to content

Checkbox

Checkbox component encapsulation, works better with z-form component.

Basic Usage

Pass in options to automatically generate options

<!-- eslint-disable no-console -->
<script lang="ts" setup>
import { ref } from 'vue'

const options = ref([
  { label: 'Option 1', value: 1 },
  { label: 'Option 2', value: 2 },
  { label: 'Option 3', value: 3 },
])

const checkboxVal = ref([1])

function click() {
  options.value.push({ label: '选项四', value: 4 })
}

function handleCheckboxChange(val: number[]) {
  console.log(val)
}
</script>

<template>
  <el-button @click="click">
    Click
  </el-button>
  <z-checkbox v-model="checkboxVal" :options="options" @change="handleCheckboxChange" />
</template>

Disabled

Set disabled to true for a certain item in option

<script lang="ts" setup>
import { ref } from 'vue'

const options = ref([
  { label: 'Option 1', value: 1 },
  { label: 'Option 2', value: 2, disabled: true },
  { label: 'Option 3', value: 3 },
])

const checkboxVal = ref([1])
</script>

<template>
  <z-checkbox v-model="checkboxVal" :options="options" />
</template>

Disable all, pass disabled as true to the component

<script lang="ts" setup>
import { ref } from 'vue'

const options = ref([
  { label: 'Option 1', value: 1 },
  { label: 'Option 2', value: 2 },
  { label: 'Option 3', value: 3 },
])

const checkboxVal = ref([1])
</script>

<template>
  <z-checkbox v-model="checkboxVal" :disabled="true" :options="options" />
</template>

Key-Value Configuration

<script lang="ts" setup>
import { ref } from 'vue'

const options = ref([
  { data: { title: 'Option 1' }, key: 1 },
  { data: { title: 'Option 2' }, key: 2 },
  { data: { title: 'Option 3' }, key: 3 },
])

const alias = {
  label: 'data.title',
  value: 'key',
}

const checkboxVal = ref([1])
</script>

<template>
  <z-checkbox v-model="checkboxVal" :alias="alias" :options="options" />
</template>

Button Style

Set type for a certain item in option or pass type directly to the component

<script lang="ts" setup>
import { ref } from 'vue'

const options = ref([
  { label: 'Option 1', value: 1 },
  { label: 'Option 2', value: 2 },
  { label: 'Option 3', value: 3, type: 'checkbox-button' },
])

const checkboxVal = ref([1])
</script>

<template>
  <z-checkbox v-model="checkboxVal" type="checkbox-button" :options="options" />
  <z-checkbox v-model="checkboxVal" :options="options" class="mt-4" />
</template>

With Border

The border attribute or field can be rendered as a checkbox with border.

<script lang="ts" setup>
import { ref } from 'vue'

const options = ref([
  { label: 'Option 1', value: 1, border: true },
  { label: 'Option 2', value: 2 },
  { label: 'Option 3', value: 3 },
])

const checkboxVal = ref([1])
</script>

<template>
  <z-checkbox v-model="checkboxVal" :options="options" />
  <z-checkbox v-model="checkboxVal" :border="true" :options="options" class="mt-4" />
</template>

z-checkbox Attributes

AttributeDescriptionTypeDefault
model-value / v-modelBinding valuearray
optionsConfigurable optionsarray
typeCheckbox formstringcheckbox
aliasKey-value pair configurationobject{ label: 'label', value: 'value', disabled: 'disabled' }
borderWhether to show borderbooleanfalse
sizeSize of checkbox button or border buttonstringdefault
disabledWhether to disablebooleanfalse
text-colorText color when checkbox button is activestring#ffffff
fillFill color and border color when checkbox button is activestring#409EFF
validate-eventWhether to trigger form validation when inputbooleantrue
nameNative name attributestring
idNative id attributestring

z-checkbox Events

Event NameDescriptionType
changeEvent triggered when binding value changesFunction

Options Item Configurable Attributes

AttributeDescriptionTypeDefault
valueBinding value of checkboxstring / number / boolean
labelText of checkboxstring
disabledWhether to disable checkboxbooleanfalse
borderWhether to show borderbooleanfalse
sizeSize of checkboxenum
nameNative name attributestring
typeCheckbox formstringel-checkbox

Released under the MIT License.