Switch 开关
表示两种相互对立的状态间的切换,多用于触发「开/关」。
引入
html
<script type="module">
import "./node_modules/easy-component-ui/dist/components/ea-switch.js";
</script>js
import "easy-component-ui/ea-switch";自定义样式
移步到 CSS Part 和 CSS Custom Properties。
基础用法
查看代码
html
<div class="demo">
<ea-switch name="basic" value="true"></ea-switch>
<ea-switch
name="basicHasText"
value="true"
style="
--ea-switch-active-bg-color: #13ce66;
--ea-switch-inactive-bg-color: #ff4949;
"
></ea-switch>
</div>不同尺寸
提供 large、default、small 三种尺寸,通过 size 属性设置。
查看代码
html
<div class="demo">
<ea-switch
name="basic"
value="true"
size="large"
active-text="Open"
inactive-text="Close"
></ea-switch>
<br />
<ea-switch
name="basic"
value="true"
active-text="Open"
inactive-text="Close"
></ea-switch>
<br />
<ea-switch
name="basic"
value="true"
size="small"
active-text="Open"
inactive-text="Close"
></ea-switch>
</div>文字描述
使用 active-text 属性与 inactive-text 属性来设置开关的文字描述,也可通过 active 和 inactive 插槽自定义内容。
查看代码
html
<div class="demo">
<ea-switch
value="true"
active-text="Pay by month"
inactive-text="Pay by year"
></ea-switch>
<br />
<ea-switch
value="true"
active-text="Pay by month"
inactive-text="Pay by year"
style="
--ea-switch-active-bg-color: #13ce66;
--ea-switch-inactive-bg-color: #ff4949;
"
></ea-switch>
<br />
<ea-switch
value="true"
style="
--ea-switch-active-bg-color: #13ce66;
--ea-switch-inactive-bg-color: #ff4949;
"
>
<ea-icon name="ban" slot="inactive"></ea-icon>
<ea-icon name="check" slot="active"></ea-icon>
</ea-switch>
</div>扩展的 value 类型
你可以设置 active-value 和 inactive-value 属性,它们接受 Boolean、String 或 Number 类型的值。
查看代码
html
<div class="demo">
<ea-tooltip id="extTooltip" content="1111" placement="top-start">
<ea-switch
id="extSwitch"
slot="reference"
value="100"
style="
--ea-switch-active-bg-color: #13ce66;
--ea-switch-inactive-bg-color: #ff4949;
"
active-value="100"
inactive-value="0"
></ea-switch>
</ea-tooltip>
</div>js
const extExample = {
tooltip: document.querySelector("#extTooltip"),
switch: document.querySelector("#extSwitch"),
init() {
const getContentString = value =>
`Switch value: ${value === 100 ? "100" : "0"}`;
this.tooltip.setAttribute("content", getContentString(this.switch.value));
this.switch.addEventListener("change", e => {
this.tooltip.setAttribute("content", getContentString(e.detail.value));
});
},
};
extExample.init();禁用状态
设置 disabled 属性即可禁用开关。
查看代码
html
<div class="demo">
<ea-switch disabled></ea-switch>
<ea-switch
value="true"
active-text="Pay by month"
inactive-text="Pay by year"
disabled
></ea-switch>
</div>阻止切换
设置 beforeChange 属性,在切换前进行拦截。返回 Promise.resolve() 允许切换,返回 Promise.reject() 阻止切换。
查看代码
html
<div class="demo">
<ea-switch id="forbiddenSwitch1"></ea-switch>
<ea-switch id="forbiddenSwitch2"></ea-switch>
</div>js
const forbiddenExample = {
switch1: document.querySelector("#forbiddenSwitch1"),
switch2: document.querySelector("#forbiddenSwitch2"),
init() {
this.switch1.beforeChange = () => {
this.switch1.toggleAttribute("disabled", true);
return new Promise(resolve => {
setTimeout(() => {
this.switch1.toggleAttribute("disabled", false);
window.$message.success("Switch success");
return resolve(true);
}, 1000);
});
};
this.switch2.beforeChange = () => {
this.switch2.toggleAttribute("disabled", true);
return new Promise((resolve, reject) => {
setTimeout(() => {
this.switch2.toggleAttribute("disabled", false);
window.$message.error("Switch failed");
return reject(new Error("Error"));
}, 1000);
});
};
},
};
forbiddenExample.init();Switch API
Switch Attributes
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|---|---|---|---|---|
| label | 标签文本 | string | — | '' |
| name | 原生属性,若与 form 组合使用,则作为 FormData 的键名 | string | — | '' |
| value | 当前开关的值 | string / number / boolean | — | false |
| activeValue | 打开时的值 | string / number / boolean | — | true |
| inactiveValue | 关闭时的值 | string / number / boolean | — | false |
| size | 开关的尺寸 | string | large | default | small | default |
| inactiveText | 关闭时显示的文字 | string | — | '' |
| inactiveColor | 关闭时的背景色 | string | — | '' |
| activeText | 打开时显示的文字 | string | — | '' |
| activeColor | 打开时的背景色 | string | — | '' |
| disabled | 是否禁用 | boolean | — | false |
| required | 是否必填 | boolean | — | false |
| beforeChange | 切换前的回调函数,返回 Promise | Function | — | null |
Switch CSS Part
用法可参考 MDN ::part()伪类
| 名称 | 说明 |
|---|---|
| wrapper | 外层 label 容器 |
| label | 表单标签 |
| form-label | 表单标签(与 label 相同) |
| container | 开关内容容器 |
| original | 原生 checkbox 控件 |
| label-left | 左侧文字(关闭状态) |
| switch | 伪开关控件 |
| label-right | 右侧文字(打开状态) |
Switch Slots
| 名称 | 说明 |
|---|---|
| active | 打开状态时的内容 |
| inactive | 关闭状态时的内容 |
Switch Methods
| 方法名 | 说明 | 参数 |
|---|---|---|
| updateContainerClasslist | 更新容器类名 | — |
| checkValidity | 检查表单字段的有效性 | — |
| reportValidity | 报告表单字段的有效性 | — |
Switch Events
| 事件名 | 说明 | 回调参数 ( event.detail ) |
|---|---|---|
| change | 状态发生变化时触发 | { value: String | Number | Boolean } |
Switch CSS Custom Properties
| 属性名 | 说明 | 默认值 |
|---|---|---|
| --ea-switch-active-bg-color | 打开时的背景色 | var(--blue-500) |
| --ea-switch-inactive-bg-color | 关闭时的背景色 | var(--grey-300) |
| --ea-switch-active-text-color | 打开时的文字颜色 | var(--blue-500) |
| --ea-switch-inactive-text-color | 关闭时的文字颜色 | var(--grey-900) |
| --ea-switch-disabled-bg-color | 禁用时的背景色 | var(--grey-200) |
| --ea-switch-disabled-text-color | 禁用时的文字颜色 | var(--grey-300) |
| --ea-switch-disabled-checked-bg-color | 禁用且选中时的背景色 | var(--blue-300) |
| --ea-switch-disabled-checked-text-color | 禁用且选中时的文字颜色 | var(--blue-300) |
类型支持
组件提供完整的 TypeScript 类型声明文件,支持 HTML、Vue 和 React 的类型提示。
HTML
组件支持 HTMLElementTagNameMap 扩展:
typescript
const switchEl = document.createElement("ea-switch");
switchEl.value = true; // 类型安全
switchEl.activeValue = "yes";
switchEl.size = "large";Vue
组件支持 Vue 全局组件类型:
typescript
// 在模板中使用时有类型提示
<ea-switch :value="true" active-text="On" @change="handleChange" />React
组件支持 JSX 类型:
typescript
// 在 JSX 中使用时有类型检查
<ea-switch value={true} activeText="On" onChange={handleChange} />