Skip to content

Switch ​

Rules ​

Basic Example ​

js
const rule = {
    type:"switch",
    title:"On Sale",
    field:"is_show",
    value:"1",
    props: {
        activeValue:"1",
        inactiveValue:"0",
    },
}

Props Configuration Examples ​

With Text Description ​

js
const rule = {
    type:"switch",
    title:"Product Status",
    field:"status",
    value:true,
    props: {
        activeText: "On Sale",
        inactiveText: "Off Sale",
        activeValue: true,
        inactiveValue: false,
    },
}

With Icon ​

js
const rule = {
    type:"switch",
    title:"Enabled",
    field:"enabled",
    value:true,
    props: {
        activeIcon: "Check",
        inactiveIcon: "Close",
    },
}

Loading State ​

js
const rule = {
    type:"switch",
    title:"Auto Save",
    field:"autoSave",
    value:false,
    props: {
        loading: false,
        activeText: "On",
        inactiveText: "Off",
    },
    on: {
        change: (value) => {
            // Show loading state when toggling
            // Need to control loading state through other means
        },
    },
}

Different Sizes ​

js
const rule = {
    type:"switch",
    title:"Notification Switch",
    field:"notification",
    value:true,
    props: {
        size: "large",
        activeText: "On",
        inactiveText: "Off",
    },
}

Custom Width ​

js
const rule = {
    type:"switch",
    title:"Auto Renew",
    field:"autoRenew",
    value:false,
    props: {
        width: 60,
        activeText: "Yes",
        inactiveText: "No",
    },
}

Events Examples ​

Listen to Switch Changes ​

js
const rule = {
    type:"switch",
    title:"On Sale",
    field:"is_show",
    value:"1",
    props: {
        activeValue:"1",
        inactiveValue:"0",
        activeText: "On Sale",
        inactiveText: "Off Sale",
    },
    on: {
        change: (value) => {
            console.log('Switch state changed:', value);
        },
    },
}

Confirm Before Toggle ​

js
const rule = {
    type:"switch",
    title:"Delete Product",
    field:"allowDelete",
    value:false,
    props: {
        activeText: "Allow",
        inactiveText: "Forbid",
        beforeChange: () => {
            // Return Promise, can be used for async confirmation
            return new Promise((resolve) => {
                if (confirm('Are you sure you want to modify delete permission?')) {
                    resolve(true);
                } else {
                    resolve(false);
                }
            });
        },
    },
}

Linkage Update Other Fields ​

js
const rule = [
    {
        type:"switch",
        title:"Enable Discount",
        field:"enableDiscount",
        value:false,
        props: {
            activeText: "Enable",
            inactiveText: "Disable",
        },
        inject: true,
        on: {
            change: ($inject, value) => {
                // Auto-set default discount rate when enabled
                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: Element_Switch

value :Number | String

Props ​

Property NameDescriptionTypeDefault
disabledWhether disabledbooleanfalse
loadingWhether to show loadingbooleanfalse
sizeSwitch sizeenum''
widthSwitch widthnumber / string''
inlinePromptWhether icon or text is displayed inside the dot, only the first character of text will be renderedbooleanfalse
activeIconIcon displayed when switch state is on, setting this will ignore active-textstring / Component—
inactiveIconIcon displayed when switch state is off, setting this will ignore inactive-textstring / Component—
activeActionIconIcon component displayed in on statestring / Component—
inactiveActionIconIcon component displayed in off statestring / Component—
activeTextText description when switch is onstring''
inactiveTextText description when switch state is offstring''
activeValueValue when switch state is onboolean / string / numbertrue
inactiveValueValue when switch state is offboolean / string / numberfalse
nameCorresponding name attribute of switchstring''
validateEventWhether to trigger form validationbooleantrue
beforeChangeHook before switch state changes, return false or return Promise and be rejected to stop toggleFunction—
idInput idstring—
tabindexInput tabindexstring / number—
ariaLabelEquivalent to native input aria-label attributestring—
activeColorBackground color when in on state (recommended to use CSS var --el-switch-on-color)string''
inactiveColorBackground color in off state (recommended to use CSS var --el-switch-off-color)string''
borderColorBorder color of switch (recommended to use CSS var --el-switch-border-color)string''
labelEquivalent to native input aria-label attributestring—

Events ​

Event NameDescriptionType
changeCallback function when switch state changesFunction

Slots ​

NameDescription
active-actionCustom active behavior
inactive-actionCustom inactive behavior

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