Skip to content

Switch 开关

规则

基础示例

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

Props 配置示例

带文字描述

js
const rule = {
    type:"switch",
    title:"商品状态",
    field:"status",
    value:true,
    props: {
        activeText: "上架",
        inactiveText: "下架",
        activeValue: true,
        inactiveValue: false,
    },
}

带图标

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

加载状态

js
const rule = {
    type:"switch",
    title:"自动保存",
    field:"autoSave",
    value:false,
    props: {
        loading: false,
        activeText: "开启",
        inactiveText: "关闭",
    },
    on: {
        change: (value) => {
            // 切换时显示加载状态
            // 需要通过其他方式控制 loading 状态
        },
    },
}

不同尺寸

js
const rule = {
    type:"switch",
    title:"通知开关",
    field:"notification",
    value:true,
    props: {
        size: "large",
        activeText: "开启",
        inactiveText: "关闭",
    },
}

自定义宽度

js
const rule = {
    type:"switch",
    title:"自动续费",
    field:"autoRenew",
    value:false,
    props: {
        width: 60,
        activeText: "是",
        inactiveText: "否",
    },
}

Events 事件示例

监听开关变化

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

切换前确认

js
const rule = {
    type:"switch",
    title:"删除商品",
    field:"allowDelete",
    value:false,
    props: {
        activeText: "允许",
        inactiveText: "禁止",
        beforeChange: () => {
            // 返回 Promise,可以用于异步确认
            return new Promise((resolve) => {
                if (confirm('确定要修改删除权限吗?')) {
                    resolve(true);
                } else {
                    resolve(false);
                }
            });
        },
    },
}

联动更新其他字段

js
const rule = [
    {
        type:"switch",
        title:"启用折扣",
        field:"enableDiscount",
        value:false,
        props: {
            activeText: "启用",
            inactiveText: "禁用",
        },
        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,
        },
    },
]

完整配置项:Element_Switch

value :Number | String

Props

属性名说明类型Default
disabled是否禁用booleanfalse
loading是否显示加载中booleanfalse
sizeswitch 的大小enum''
widthswitch 的宽度number / string''
inlinePrompt无论图标或文本是否显示在点内,只会呈现文本的第一个字符booleanfalse
activeIconswitch 状态为 on 时所显示图标,设置此项会忽略 active-textstring / Component
inactiveIconswitch 状态为 off 时所显示图标,设置此项会忽略 inactive-textstring / Component
activeActionIconon状态下显示的图标组件string / Component
inactiveActionIconoff状态下显示的图标组件string / Component
activeTextswitch 打开时的文字描述string''
inactiveTextswitch 的状态为 off 时的文字描述string''
activeValueswitch 状态为 on 时的值boolean / string / numbertrue
inactiveValueswitch的状态为 off 时的值boolean / string / numberfalse
nameswitch 对应的 name 属性string''
validateEvent是否触发表单验证booleantrue
beforeChangeswitch 状态改变前的钩子, 返回 false 或者返回 Promise 且被 reject 则停止切换Function
idinput 的 idstring
tabindexinput 的 tabindexstring / number
ariaLabel等价于原生 input aria-label 属性string
activeColor当在 on 状态时的背景颜色(推荐使用 CSS var --el-switch-on-color )string''
inactiveColoroff 状态时的背景颜色(推荐使用 CSS var --el-switch-off-color )string''
borderColor开关的边框颜色 ( 推荐使用 CSS var --el-switch-border-color )string''
label等价于原生 input aria-label 属性string

Events

事件名说明Type
changeswitch 状态发生变化时的回调函数Function

Slots

名称说明
active-action自定义 active 行为
inactive-action自定义 inactive 行为

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