Skip to content

Switch

Rules

Basic Example

js
const rule = {
    type:"switch",
    title:"On Sale",
    field:"is_show",
    value:"1",
    props: {
        round: false,
    },
}

Props Configuration Examples

Custom Values

js
const rule = {
    type:"switch",
    title:"Product Status",
    field:"status",
    value:true,
    props: {
        checkedValue: true,
        uncheckedValue: false,
    }
}

Square Switch

js
const rule = {
    type:"switch",
    title:"Notification Switch",
    field:"notification",
    value:true,
    props: {
        round: false,
    }
}

Loading State

js
const rule = {
    type:"switch",
    title:"Auto Save",
    field:"autoSave",
    value:false,
    props: {
        loading: false,
    }
}

Different Sizes

js
const rule = {
    type:"switch",
    title:"Switch",
    field:"switch",
    value:true,
    props: {
        size: "large",
    }
}

Events Examples

Handle Switch Changes

js
const rule = {
    type:"switch",
    title:"On Sale",
    field:"is_show",
    value:"1",
    props: {
        checkedValue: "1",
        uncheckedValue: "0",
    },
    on: {
        'update:value': (value) => {
            console.log('Switch state changed:', value);
        },
    },
}

Linkage Update Other Fields

js
const rule = [
    {
        type:"switch",
        title:"Enable Discount",
        field:"enableDiscount",
        value:false,
        props: {
            checkedValue: true,
            uncheckedValue: false,
        },
        inject: true,
        on: {
            'update:value': ($inject, value) => {
                // Set default discount rate
                if (value) {
                    $inject.api.setValue('discount', 0.9);
                } else {
                    $inject.api.setValue('discount', 1);
                }
            },
        },
    },
    {
        type:"input-number",
        title:"Discount Rate",
        field:"discount",
        props: {
            min: 0,
            max: 1,
            step: 0.1,
            precision: 2,
            disabled: false,
        },
    },
]

Complete configuration items: naive-ui_Switch

value :Number | String

Props

NameTypeDefault ValueDescription
checked-valuestring | boolean | numbertrueValue when checked
disabledbooleanfalseWhether disabled
loadingbooleanfalseWhether loading
rail-style(info: { focused: boolean, checked: boolean }) => (CSSProperties | string)undefinedFunction to create rail style
roundbooleantrueWhether it is a round button
size'small' | 'medium' | 'large''medium'Switch size
unchecked-valuestring | boolean | numberfalseValue when unchecked
on-update:value(value: boolean) => voidundefinedCallback when component value changes

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