Skip to content

Switch

Rules

Basic Example

js
const rule = {
    type:"switch",
    title:"Is Listed",
    field:"is_show",
    value:"1",
    props: {
        checkedChildren:"1",
        unCheckedChildren:"0",
    },
}

Props Configuration Examples

Custom Value

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

Custom Display Content

js
const rule = {
    type:"switch",
    title:"Notification Switch",
    field:"notification",
    value:true,
    props: {
        checkedChildren: "On",
        unCheckedChildren: "Off",
    }
}

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: "small",
    }
}

Events Examples

Listen to Switch Changes

js
const rule = {
    type:"switch",
    title:"Is Listed",
    field:"is_show",
    value:"1",
    props: {
        checkedChildren:"1",
        unCheckedChildren:"0",
    },
    on: {
        change: (value) => {
            console.log('Switch state changed:', value);
        },
    },
}

Linkage Update Other Fields

js
const rule = [
    {
        type:"switch",
        title:"Enable Discount",
        field:"enableDiscount",
        value:false,
        props: {
            value: true,
            defaultValue: false,
        },
        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: TDesign_Switch

value :Number | String

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