Skip to content

Checkbox - Vant 4

☑️ Checkbox - Interactive Selection Made Simple

🎯 Introduction

Create intuitive multiple-choice experiences with our flexible Checkbox component! Perfect for forms, settings panels, and any scenario where users need to select multiple options from a list.

📦 Installation

Get started quickly by registering the component globally via app.use. For more registration options, check out our Component Registration guide.

js
import { createApp } from'vue'; import { Checkbox, CheckboxGroup } from'vant'; const app = createApp(); app.use(Checkbox); app.use(CheckboxGroup);

🎨 Usage Examples

🌟 Basic Usage

Start with the simplest checkbox implementation:

html
js
import { ref } from'vue'; exportdefault { setup() { const checked = ref(true); return { checked, }; }, };

🚫 Disabled State

Prevent user interaction when needed:

html

🔷 Custom Shape

Switch between round and square styles to match your design:

html
js
import { ref } from'vue'; exportdefault { setup() { const checked = ref(['a', 'b']); return { checked }; }, };

🎨 Custom Color

Make checkboxes match your brand colors:

html

📏 Custom Icon Size

Adjust the checkbox size to fit your layout perfectly:

html

🖼️ Custom Icon

Create unique checkbox experiences with custom icons:

html
js
import { ref } from'vue'; exportdefault { 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', }; }, };

⬅️ Left Label

Position labels on the left side for better layout flexibility:

html

🔒 Disable Label Click

Prevent accidental selections by disabling label clicks:

html

📋 Checkbox Group

Manage multiple checkboxes effortlessly with group functionality:

html
js
import { ref } from'vue'; exportdefault { setup() { const checked = ref(['a', 'b']); return { checked }; }, };

↔️ Horizontal Layout

Display checkboxes in a horizontal row for compact layouts:

html
js
import { ref } from'vue'; exportdefault { setup() { const checked = ref([]); return { checked }; }, };

🔢 Maximum Selection Limit

Control how many options users can select:

html

🔄 Toggle All Functionality

Provide convenient select-all and deselect-all controls:

html
js
import { ref } from'vue'; exportdefault { setup() { const checked = ref([]); const checkboxGroup = ref(null); constcheckAll = () => { checkboxGroup.value.toggleAll(true); } consttoggleAll = () => { checkboxGroup.value.toggleAll(); }, return { checked, checkAll, toggleAll, checkboxGroup, }; }, };

📱 Inside a Cell

Integrate checkboxes seamlessly within cell components:

html
js
import { ref, onBeforeUpdate } from'vue'; exportdefault { setup() { const checked = ref([]); const checkboxRefs = ref([]); consttoggle = (index) => { checkboxRefs.value[index].toggle(); }; onBeforeUpdate(() => { checkboxRefs.value = []; }); return { list: ['a', 'b'], toggle, checked, checkboxRefs, }; }, };

⚡ Indeterminate State

Show partial selection states for better user feedback:

html
js
import { ref } from'vue'; exportdefault { setup() { const list = ['a', 'b', 'c', 'd'] const isCheckAll = ref(false); const checkedResult = ref(['a', 'b', 'd']); const isIndeterminate = ref(true); constcheckAllChange = (val: boolean) => { checkedResult.value = val ? list : [] isIndeterminate.value = false } constcheckedResultChange = (value: string[]) => { const checkedCount = value.length isCheckAll.value = checkedCount === list.length isIndeterminate.value = checkedCount > 0 && checkedCount < list.length } return { list, isCheckAll, checkedResult, checkAllChange, isIndeterminate, checkedResultChange }; }, };

🔧 API Reference

🎛️ Checkbox Props

Configure your checkbox behavior with these powerful options:

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 size*numberstring*
checked-colorChecked colorstring#1989fa

| bind-group | Whether to bind with CheckboxGroup | boolean | true | | indeterminate | Whether indeterminate status | boolean | false |

📋 CheckboxGroup Props

Manage multiple checkboxes with these group-level configurations:

AttributeDescriptionTypeDefault
v-modelNames of all checked checkboxesany[]-
disabledWhether to disable all checkboxesbooleanfalse
maxMaximum amount of checked options*numberstring*
directionDirection, can be set to horizontalstringvertical
icon-sizeIcon size of all checkboxes*numberstring*
checked-colorChecked color of all checkboxesstring#1989fa

| shape v4.6.3 | Can be set to square | string | round |

📡 Checkbox Events

Listen to user interactions and state changes:

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

📡 CheckboxGroup Events

Track group-level changes:

EventDescriptionParameters
changeEmitted when value changednames: any[]

🎰 Checkbox Slots

Customize checkbox appearance with flexible slots:

NameDescriptionSlotProps
defaultCustom label{ checked: boolean, disabled: boolean }
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 checkboxes*options?: booleanobject*

toggleAll Usage

js
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

Control individual checkboxes programmatically:

NameDescriptionAttributeReturn value
toggleToggle check statuschecked?: boolean-

📝 Types

Access comprehensive TypeScript definitions for better development experience:

ts
importtype { CheckboxProps, CheckboxShape, CheckboxInstance, CheckboxLabelPosition, CheckboxGroupProps, CheckboxGroupInstance, CheckboxGroupDirection, CheckboxGroupToggleAllOptions, } from'vant';

CheckboxInstance and CheckboxGroupInstance is the type of component instance:

ts
import { ref } from'vue'; importtype { CheckboxInstance, CheckboxGroupInstance } from'vant'; const checkboxRef = ref<CheckboxInstance>(); const checkboxGroupRef = ref<CheckboxGroupInstance>(); checkboxRef.value?.toggle(); checkboxGroupRef.value?.toggleAll();

🎨 Theming

🎯 CSS Variables

Customize the checkbox appearance to match your design system perfectly:

NameDefault ValueDescription
--van-checkbox-size20px-
--van-checkbox-border-colorvar(--van-gray-5)-
--van-checkbox-durationvar(--van-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-backgroundvar(--van-border-color)-

Explore more components and resources to enhance your development experience:

Form Components

  • Radio - Single selection from multiple options
  • Switch - Toggle between two states
  • Field - Input field with validation
  • Form - Complete form solution with validation

Layout & Display

  • Cell - List item container
  • List - Scrollable list component
  • Grid - Flexible grid layout system

Development Resources

Enterprise-level mobile solution based on Vant