iter(g125): 开关补 loading 加载态

This commit is contained in:
HIsheR
2026-10-26 14:44:37 +08:00
parent 56e5d18753
commit 490c9fa6ed
8 changed files with 70 additions and 18 deletions
+13 -2
View File
@@ -1,14 +1,15 @@
import { effect, h } from '@ui-arch/frame';
import type { SwitchProps } from './types.js';
/*** 开关:checked 信号双向绑定,disabled 支持布尔或信号禁用,轨道 + 滑块的纯 CSS 实现 ***/
/*** 开关:checked 信号双向绑定,loading 加载锁(转圈 + 禁点),disabled 支持布尔或信号禁用,轨道 + 滑块的纯 CSS 实现 ***/
export function switchButton(props: SwitchProps): HTMLElement {
const hasLoading = props.loading !== undefined; // 加载态接管禁用写入时,disabled 不再走 h() 双写
const input = h('input', {
type: 'checkbox',
class: 'ua-switch-input',
role: 'switch', // 用开关语义而非复选框:读屏播报「开关」并跟随选中态
'aria-label': props.ariaLabel ?? props.label, // 无可见文案时也要有可访问名
disabled: props.disabled, // false/undefined 移除属性,Signal 走响应式
disabled: hasLoading ? undefined : props.disabled, // false/undefined 移除属性,Signal 走响应式
}) as HTMLInputElement;
if (props.checked) {
const checked = props.checked;
@@ -20,6 +21,16 @@ export function switchButton(props: SwitchProps): HTMLElement {
if (props.onChange) input.addEventListener('change', () => props.onChange!(input.checked));
const track = h('span', { class: 'ua-switch-track' }, h('span', { class: 'ua-switch-thumb' }));
const el = h('label', { class: 'ua-switch' }, input, track);
if (hasLoading) {
const ld = props.loading; // 加载态:单一写入口同时管 disabled 与转圈,避免与 disabled 信号双写竞争
const dis = props.disabled;
effect(() => {
const busy = typeof ld === 'function' ? ld() : !!ld;
input.disabled = busy || (typeof dis === 'function' ? dis() : !!dis); // 加载中锁定交互
el.classList.toggle('is-loading', busy); // 滑块转圈由 CSS 承载
input.setAttribute('aria-busy', String(busy)); // 读屏可感知忙碌
});
}
if (props.label) el.append(h('span', { class: 'ua-switch-label' }, props.label));
return el;
}
+1
View File
@@ -99,6 +99,7 @@ export interface SwitchProps {
label?: string; // 左侧文案;缺省不渲染(此时读屏名靠 ariaLabel 兜底)
ariaLabel?: string; // 无可见文案时的可访问名,如纯图标开关;缺省取 label
checked?: Signal<boolean>; // 开关状态信号,双向绑定
loading?: boolean | Signal<boolean>; // 加载中:锁定交互、滑块换成转圈并挂 aria-busy,用于异步提交防误点
disabled?: boolean | Signal<boolean>; // 禁用态:布尔直接置位,传信号则动态同步
onChange?: (checked: boolean) => void; // 变更回调
}