import{o as a,a as n,y as t}from"./vue-libs.b44bc779.js";const l={class:"van-doc-markdown-body"},e=t(`

Checkbox

Intro

A group of options for multiple choices.

Install

Register component globally via app.use, refer to Component Registration for more registration ways.

import { createApp } from 'vue';
import { Checkbox, CheckboxGroup } from 'vant';

const app = createApp();
app.use(Checkbox);
app.use(CheckboxGroup);

Usage

Basic Usage

<van-checkbox v-model="checked">Checkbox</van-checkbox>
import { ref } from 'vue';

export default {
  setup() {
    const checked = ref(true);
    return {
      checked,
    };
  },
};

Disabled

<van-checkbox v-model="checked" disabled>Checkbox</van-checkbox>

Custom Shape

<van-checkbox v-model="checked" shape="square">Checkbox</van-checkbox>

Custom Color

<van-checkbox v-model="checked" checked-color="#ee0a24">Checkbox</van-checkbox>

Custom Icon Size

<van-checkbox v-model="checked" icon-size="24px">Checkbox</van-checkbox>

Custom Icon

Use icon slot to custom icon.

<van-checkbox v-model="checked">
  customize icon
  <template #icon="props">
    <img class="img-icon" :src="props.checked ? activeIcon : inactiveIcon" />
  </template>
</van-checkbox>

<style>
  .img-icon {
    height: 20px;
  }
</style>
import { ref } from 'vue';

export default {
  setup() {
    const checked = ref(true);
    return {
      checked,
      activeIcon:
        'https://fastly.jsdelivr.net/npm/@vant/assets/user-active.png',
      inactiveIcon:
        'https://fastly.jsdelivr.net/npm/@vant/assets/user-inactive.png',
    };
  },
};

Disable Label Click

<van-checkbox v-model="checked" label-disabled>Checkbox</van-checkbox>

Checkbox Group

When Checkboxes are inside a CheckboxGroup, the checked checkboxes's name is an array and bound with CheckboxGroup by v-model.

<van-checkbox-group v-model="checked">
  <van-checkbox name="a">Checkbox a</van-checkbox>
  <van-checkbox name="b">Checkbox b</van-checkbox>
</van-checkbox-group>
import { ref } from 'vue';

export default {
  setup() {
    const checked = ref(['a', 'b']);
    return { checked };
  },
};

Horizontal

<van-checkbox-group v-model="checked" direction="horizontal">
  <van-checkbox name="a">Checkbox a</van-checkbox>
  <van-checkbox name="b">Checkbox b</van-checkbox>
</van-checkbox-group>
import { ref } from 'vue';

export default {
  setup() {
    const checked = ref([]);
    return { checked };
  },
};

Maximum amount of checked options

<van-checkbox-group v-model="checked" :max="2">
  <van-checkbox name="a">Checkbox a</van-checkbox>
  <van-checkbox name="b">Checkbox b</van-checkbox>
  <van-checkbox name="c">Checkbox c</van-checkbox>
</van-checkbox-group>

Toggle All

<van-checkbox-group v-model="checked" ref="checkboxGroup">
  <van-checkbox name="a">Checkbox a</van-checkbox>
  <van-checkbox name="b">Checkbox b</van-checkbox>
  <van-checkbox name="c">Checkbox c</van-checkbox>
</van-checkbox-group>

<van-button type="primary" @click="checkAll">Check All</van-button>
<van-button type="primary" @click="toggleAll">Toggle All</van-button>
import { ref } from 'vue';

export default {
  setup() {
    const checked = ref([]);
    const checkboxGroup = ref(null);

    const checkAll = () => {
      checkboxGroup.value.toggleAll(true);
    }
    const toggleAll = () => {
      checkboxGroup.value.toggleAll();
    },

    return {
      checked,
      checkAll,
      toggleAll,
      checkboxGroup,
    };
  },
};

Inside a Cell

<van-checkbox-group v-model="checked">
  <van-cell-group inset>
    <van-cell
      v-for="(item, index) in list"
      clickable
      :key="item"
      :title="\`Checkbox \${item}\`"
      @click="toggle(index)"
    >
      <template #right-icon>
        <van-checkbox
          :name="item"
          :ref="el => checkboxRefs[index] = el"
          @click.stop
        />
      </template>
    </van-cell>
  </van-cell-group>
</van-checkbox-group>
import { ref, onBeforeUpdate } from 'vue';

export default {
  setup() {
    const checked = ref([]);
    const checkboxRefs = ref([]);
    const toggle = (index) => {
      checkboxRefs.value[index].toggle();
    };

    onBeforeUpdate(() => {
      checkboxRefs.value = [];
    });

    return {
      list: ['a', 'b'],
      toggle,
      checked,
      checkboxRefs,
    };
  },
};

API

Checkbox Props

AttributeDescriptionTypeDefault
v-modelCheck statusbooleanfalse
nameCheckbox name, usually a unique string or numberany-
shapeCan be set to squarestringround
disabledDisable checkboxbooleanfalse
label-disabledWhether to disable label clickbooleanfalse
label-positionCan be set to leftstringright
icon-sizeIcon sizenumber | string20px
checked-colorChecked colorstring#1989fa
bind-groupWhether to bind with CheckboxGroupbooleantrue

CheckboxGroup Props

AttributeDescriptionTypeDefault
v-modelNames of all checked checkboxesany[]-
disabledWhether to disable all checkboxesbooleanfalse
maxMaximum amount of checked optionsnumber | string0(Unlimited)
directionDirection, can be set to horizontalstringvertical
icon-sizeIcon size of all checkboxesnumber | string20px
checked-colorChecked color of all checkboxesstring#1989fa

Checkbox Events

EventDescriptionParameters
changeEmitted when value changedchecked: boolean
clickEmitted when the checkbox is clickedevent: MouseEvent

CheckboxGroup Events

EventDescriptionParameters
changeEmitted when value changednames: any[]

Checkbox Slots

NameDescriptionSlotProps
defaultCustom label-
iconCustom icon{ checked: boolean, disabled: boolean }

CheckboxGroup Methods

Use ref to get CheckboxGroup instance and call instance methods.

NameDescriptionAttributeReturn value
toggleAllToggle check status of all checkboxesoptions?: boolean | object-

toggleAll Usage

import { ref } from 'vue';
import type { CheckboxGroupInstance } from 'vant';

const checkboxGroupRef = ref<CheckboxGroupInstance>();

// Toggle all
checkboxGroup.value?.toggleAll();
// Select all
checkboxGroup.value?.toggleAll(true);
// Unselect all
checkboxGroup.value?.toggleAll(false);

// Toggle all, skip disabled
checkboxGroup.value?.toggleAll({
  skipDisabled: true,
});
// Select all, skip disabled
checkboxGroup.value?.toggleAll({
  checked: true,
  skipDisabled: true,
});

Checkbox Methods

Use ref to get Checkbox instance and call instance methods.

NameDescriptionAttributeReturn value
toggleToggle check statuschecked?: boolean-

Types

The component exports the following type definitions:

import type {
  CheckboxProps,
  CheckboxShape,
  CheckboxInstance,
  CheckboxLabelPosition,
  CheckboxGroupProps,
  CheckboxGroupInstance,
  CheckboxGroupDirection,
  CheckboxGroupToggleAllOptions,
} from 'vant';

CheckboxInstance and CheckboxGroupInstance is the type of component instance:

import { ref } from 'vue';
import type { CheckboxInstance, CheckboxGroupInstance } from 'vant';

const checkboxRef = ref<CheckboxInstance>();
const checkboxGroupRef = ref<CheckboxGroupInstance>();

checkboxRef.value?.toggle();
checkboxGroupRef.value?.toggleAll();

Theming

CSS Variables

The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.

NameDefault ValueDescription
--van-checkbox-size20px-
--van-checkbox-border-colorvar(--van-gray-5)-
--van-checkbox-transition-durationvar(--van-animation-duration-fast)-
--van-checkbox-label-marginvar(--van-padding-xs)-
--van-checkbox-label-colorvar(--van-text-color)-
--van-checkbox-checked-icon-colorvar(--van-primary-color)-
--van-checkbox-disabled-icon-colorvar(--van-gray-5)-
--van-checkbox-disabled-label-colorvar(--van-text-color-3)-
--van-checkbox-disabled-background-colorvar(--van-border-color)-
`,28),c=[e],i={__name:"README",setup(p,{expose:s}){return s({frontmatter:{}}),(h,d)=>(a(),n("div",l,c))}};export{i as default};