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; // 变更回调
}
+8 -2
View File
@@ -19,7 +19,7 @@
"textarea(props):多行输入,value 信号双向绑定,disabled 支持布尔或信号,rows 控行数;prefix/suffix 可选渲染为上下附加块,maxCount 显示实时字数并设原生上限",
"slider(props):滑块,value 信号双向绑定,min/max/step 可配,原生 range 承载键盘交互,右侧可显示当前值",
"checkbox(props):复选框,checked 信号双向绑定,indeterminate 半选态(读屏播报 mixed),disabled 支持布尔或信号禁用",
"switchButton(props):开关,checked 信号双向绑定,disabled 支持布尔或信号禁用",
"switchButton(props):开关,checked 信号双向绑定,loading 加载锁(滑块转圈 + aria-busy),disabled 支持布尔或信号禁用",
"select(props):自绘下拉选择(非原生 select),value 信号双向绑定,点外部 / Esc 关闭,选项弹层带微动画,disabled 支持布尔或信号禁用,空间不足自动翻转,键入字母快跳选项;searchable 开启弹层搜索框,clearable 有值时给清空按钮",
"menu(props):下拉菜单,触发器 + 自绘弹层,条目支持禁用 / 危险 / 分隔线,↑↓ 环形导航,点外部 / Esc 关闭,空间不足自动翻转,键入字母快跳菜单项",
"flip(trigger, panel, preferred):浮层上下翻转判定,按触发器与面板的视口几何决定 preferred 方位放不下时是否翻转,返回实际生效方位 'top' | 'bottom'",
@@ -503,6 +503,11 @@
"t": "string",
"d": "左侧文案"
},
{
"n": "loading",
"t": "boolean | Signal<boolean>",
"d": "加载中:锁定交互、滑块换成转圈并挂 aria-busy(异步提交防误点);与 disabled 同时生效"
},
{
"n": "disabled",
"t": "boolean | Signal<boolean>",
@@ -519,7 +524,8 @@
"steps": [
"创建布尔信号并传入 checked",
"轨道与滑块的动画由 CSS 完成,无需干预",
"状态变化在 onChange 里做副作用(如切换主题)"
"状态变化在 onChange 里做副作用(如切换主题)",
"切换要等异步请求就传 loading(布尔或信号):请求期间滑块转圈、不可点,完成后自动恢复"
]
},
{
+18
View File
@@ -293,6 +293,24 @@ body.ua-col-resizing {
.ua-switch-label {
font-size: var(--ua-font-size-md);
}
.ua-switch.is-loading {
cursor: progress; /* 加载中:光标改忙碌 */
}
.ua-switch.is-loading .ua-switch-thumb {
box-sizing: border-box; /* 滑块换成转圈环:border 计入尺寸 */
background: transparent;
border: 2px solid color-mix(in srgb, var(--ua-color-bg) 40%, transparent);
border-top-color: var(--ua-color-bg);
animation: ua-switch-spin 0.7s linear infinite;
}
@keyframes ua-switch-spin {
to { transform: rotate(360deg); }
}
@media (prefers-reduced-motion: reduce) {
.ua-switch.is-loading .ua-switch-thumb {
animation: none; /* 减少动效:转圈静态化为实心环 */
}
}
/* —— 下拉选择(自绘弹层) —— */
.ua-select {