iter(g83): 开关改 switch 语义并补可访问名

This commit is contained in:
HIsheR
2026-10-26 13:01:16 +08:00
parent ab0f1a5286
commit 151b54243e
7 changed files with 32 additions and 4 deletions
+7 -1
View File
@@ -3,7 +3,13 @@ import type { SwitchProps } from './types.js';
/*** 开关:checked 信号双向绑定,disabled 支持布尔或信号禁用,轨道 + 滑块的纯 CSS 实现 ***/
export function switchButton(props: SwitchProps): HTMLElement {
const input = h('input', { type: 'checkbox', class: 'ua-switch-input', disabled: props.disabled }) as HTMLInputElement; // false/undefined 移除属性,Signal 走响应式
const input = h('input', {
type: 'checkbox',
class: 'ua-switch-input',
role: 'switch', // 用开关语义而非复选框:读屏播报「开关」并跟随选中态
'aria-label': props.ariaLabel ?? props.label, // 无可见文案时也要有可访问名
disabled: props.disabled, // false/undefined 移除属性,Signal 走响应式
}) as HTMLInputElement;
if (props.checked) {
const checked = props.checked;
input.addEventListener('change', () => checked(input.checked)); // 用户切换写回信号
+2 -1
View File
@@ -91,7 +91,8 @@ export interface CheckboxProps {
/*** 开关入参 ***/
export interface SwitchProps {
label?: string; // 左侧文案;缺省不渲染
label?: string; // 左侧文案;缺省不渲染(此时读屏名靠 ariaLabel 兜底)
ariaLabel?: string; // 无可见文案时的可访问名,如纯图标开关;缺省取 label
checked?: Signal<boolean>; // 开关状态信号,双向绑定
disabled?: boolean | Signal<boolean>; // 禁用态:布尔直接置位,传信号则动态同步
onChange?: (checked: boolean) => void; // 变更回调
+5
View File
@@ -478,6 +478,11 @@
"t": "Signal<boolean>",
"d": "开关状态信号,双向绑定"
},
{
"n": "ariaLabel",
"t": "string",
"d": "无可见文案时的可访问名(如纯图标开关);缺省取 label"
},
{
"n": "label",
"t": "string",