Skip to content

Switch 开关

规则

基础示例

js
const rule = {
    type:"switch",
    title:"是否上架",
    field:"is_show",
    value:"1",
    props: {
        checkedChildren:"1",
        unCheckedChildren:"0",
    },
}

Props 配置示例

自定义值

js
const rule = {
    type:"switch",
    title:"商品状态",
    field:"status",
    value:true,
    props: {
        value: true,
        defaultValue: false,
    }
}

自定义显示内容

js
const rule = {
    type:"switch",
    title:"通知开关",
    field:"notification",
    value:true,
    props: {
        checkedChildren: "开",
        unCheckedChildren: "关",
    }
}

加载状态

js
const rule = {
    type:"switch",
    title:"自动保存",
    field:"autoSave",
    value:false,
    props: {
        loading: false,
    }
}

不同尺寸

js
const rule = {
    type:"switch",
    title:"开关",
    field:"switch",
    value:true,
    props: {
        size: "small",
    }
}

Events 事件示例

监听开关变化

js
const rule = {
    type:"switch",
    title:"是否上架",
    field:"is_show",
    value:"1",
    props: {
        checkedChildren:"1",
        unCheckedChildren:"0",
    },
    on: {
        change: (value) => {
            console.log('开关状态改变:', value);
        },
    },
}

联动更新其他字段

js
const rule = [
    {
        type:"switch",
        title:"启用折扣",
        field:"enableDiscount",
        value:false,
        props: {
            value: true,
            defaultValue: false,
        },
        inject: true,
        on: {
            change: ($inject, value) => {
                // 启用折扣时,自动设置默认折扣率
                if (value) {
                    $inject.api.setValue('discount', 0.9);
                } else {
                    $inject.api.setValue('discount', 1);
                }
            },
        },
    },
    {
        type:"input-number",
        title:"折扣率",
        field:"discount",
        props: {
            min: 0,
            max: 1,
            step: 0.1,
            precision: 2,
            disabled: false,
        },
    },
]

完整配置项:TDesign_Switch

value :Number | String

FormCreate 是一个开源项目,基于 MIT 许可证发布,欢迎个人和企业用户免费使用