SuperAPI AI 工具集

sgds-components-switch

前端与建站

查看项目

更新于 10-15 21:54

技能介绍

用途:<sgds-switch> 是用于二元开/关状态的拨动开关,标签可放在右侧(默认 slot)或左侧(left-label slot),可用 size 与相邻 UI 元素匹配。

前置条件:安装与框架集成参见 sgds-components-setup(React 19+ 与 React ≤18、Vue、Angular)。该组件不开放自定义属性和 CSS part,不能修改 CSS 样式。

主要能力:checked 预设开启,disabled 禁用,icon 在开关滑块内显示 X/对勾图标,size 取 sm、md、lg(默认 md)。默认 slot 显示右侧标签,left-label slot 显示左侧标签。触发时派发 sgds-change 事件,detail 为 { checked: boolean },true 表示开、false 表示关。

用法示例:<sgds-switch>Enable notifications</sgds-switch>;<sgds-switch checked icon>Dark mode</sgds-switch>;<sgds-switch><span slot="left-label">Auto-save</span></sgds-switch>;<sgds-switch disabled>Unavailable option</sgds-switch>;<sgds-switch size="sm"></sgds-switch>。监听事件:document.getElementById("my-switch").addEventListener("sgds-change", e => { console.log("Checked:", e.detail.checked); });

限制:没有 label、hintText、name、validation 等属性,不是表单字段集成组件;如需表单提交,应配合一个反映其 checked 状态的隐藏 input 元素。该组件没有公开方法。