Skip to content

Checkbox

checkbox

Rule

Basic Example

js
const rule = {
    type: 'checkbox',
    title: 'Checkbox',
    field: 'checkbox',
    value: ['1'],
    props: {
        options: [
            {
                text: 'Checkbox 1',
                value: '1',
            },
            {
                text: 'Checkbox 2',
                value: '2',
            },
        ]
    }
}

Props Configuration Examples

Limit Selection Count

js
const rule = {
    type: 'checkbox',
    title: 'Product Tags',
    field: 'tags',
    value: ['1'],
    props: {
        options: [
            { text: 'Hot Sale', value: '1' },
            { text: 'New Product', value: '2' },
            { text: 'Recommended', value: '3' },
        ],
        max: 3,
    }
}

Horizontal Layout

js
const rule = {
    type: 'checkbox',
    title: 'Product Tags',
    field: 'tags',
    value: [],
    props: {
        options: [
            { text: 'Hot Sale', value: '1' },
            { text: 'New Product', value: '2' },
            { text: 'Recommended', value: '3' },
        ],
        direction: 'horizontal',
    }
}

Custom Style

js
const rule = {
    type: 'checkbox',
    title: 'Product Tags',
    field: 'tags',
    value: [],
    props: {
        options: [
            { text: 'Hot Sale', value: '1' },
            { text: 'New Product', value: '2' },
        ],
        shape: 'square',
        checkedColor: '#ee0a24',
        iconSize: '24px',
    }
}

Events Examples

Listen to Selection Changes

js
const rule = {
    type: 'checkbox',
    title: 'Product Tags',
    field: 'tags',
    value: [],
    props: {
        options: [
            { text: 'Hot Sale', value: '1' },
            { text: 'New Product', value: '2' },
            { text: 'Recommended', value: '3' },
        ],
    },
    on: {
        change: (names) => {
            console.log('Selection changed:', names);
        },
    },
}

Linkage Update After Selection

js
const rule = [
    {
        type: 'checkbox',
        title: 'Product Tags',
        field: 'tags',
        value: [],
        props: {
            options: [
                { text: 'Hot Sale', value: '1' },
                { text: 'New Product', value: '2' },
                { text: 'Recommended', value: '3' },
            ],
        },
        inject: true,
        on: {
            change: ($inject, names) => {
                // Auto-set product status based on number of selected tags
                if (names.length >= 2) {
                    $inject.api.setValue('status', 'active');
                } else {
                    $inject.api.setValue('status', 'inactive');
                }
            },
        },
    },
    {
        type: 'input',
        title: 'Product Status',
        field: 'status',
        props: {
            disabled: true,
        },
    },
]

Complete configuration items:Vant_Checkbox

value :Array

Options

Field NameDescriptionField TypeRequiredDefault Value
valueParameter valueString,Numbertrue-
textField aliasStringtrue-
disabledSet to disabled stateBooleanfalsefalse

Props

ParameterDescriptionTypeDefault Value
disabledWhether to disable all checkboxesbooleanfalse
maxMaximum selectable count, 0 means unlimitednumber | string0
directionLayout direction, optional value is horizontalstringvertical
icon-sizeIcon size for all checkboxes, default unit is pxnumber | string20px
checked-colorSelected state color for all checkboxesstring#1989fa
shapeShape, optional value is squarestringround

Events

Event NameDescriptionCallback Parameters
changeEvent triggered when bound value changesnames: any[]

FormCreate is an open-source project released under the MIT License. Free for personal and commercial use.