iter(g18): textField 可清空按钮
This commit is contained in:
@@ -2,7 +2,7 @@ import { effect, h } from '@ui-arch/frame';
|
||||
import { addonWrap } from './addons.js';
|
||||
import type { TextFieldProps } from './types.js';
|
||||
|
||||
/*** 文本输入:value 信号双向绑定,disabled 支持布尔或信号禁用,onInput 为输入回调;prefix/suffix 包附加组 ***/
|
||||
/*** 文本输入:value 信号双向绑定,disabled 支持布尔或信号禁用,onInput 为输入回调;prefix/suffix 包附加组,clearable 一键清空 ***/
|
||||
export function textField(props: TextFieldProps): HTMLElement {
|
||||
const el = h('input', { class: 'ua-input', placeholder: props.placeholder ?? '', disabled: props.disabled }) as HTMLInputElement; // false/undefined 移除属性,Signal 走响应式
|
||||
if (props.value) {
|
||||
@@ -13,5 +13,23 @@ export function textField(props: TextFieldProps): HTMLElement {
|
||||
});
|
||||
}
|
||||
if (props.onInput) el.addEventListener('input', () => props.onInput!(el.value));
|
||||
return addonWrap(el, props.prefix, props.suffix);
|
||||
let clearBtn: HTMLElement | null = null; // 一键清空按钮;clearable 关闭时不创建
|
||||
if (props.clearable) {
|
||||
clearBtn = h(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
class: 'ua-input-clear is-hidden', // 初始无值隐藏
|
||||
'aria-label': '清空输入',
|
||||
tabIndex: -1, // 不进 Tab 序,清空走鼠标/触屏
|
||||
onClick: () => { el.value = ''; props.value?.(''); props.onInput?.(''); el.focus(); }, // 清空并同步信号与回调
|
||||
},
|
||||
'×',
|
||||
);
|
||||
const syncClear = (): void => { clearBtn!.classList.toggle('is-hidden', el.value === ''); }; // 有值才显示
|
||||
el.addEventListener('input', syncClear); // 用户输入时同步
|
||||
effect(() => { if (props.value) props.value(); syncClear(); }); // 外部写回时同步(读信号建依赖)
|
||||
}
|
||||
const suffix = clearBtn ? h('span', { class: 'ua-input-clear-group' }, clearBtn, props.suffix ?? null) : props.suffix; // 清空按钮并入后置附加组
|
||||
return addonWrap(el, props.prefix, suffix);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user