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);
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ export interface TextFieldProps {
|
||||
onInput?: (value: string) => void; // 输入回调
|
||||
prefix?: string | HTMLElement; // 输入框内侧前置附加块(如图标、货币符);缺省不渲染
|
||||
suffix?: string | HTMLElement; // 输入框内侧后置附加块(如单位);缺省不渲染
|
||||
clearable?: boolean; // 开启后有值时显示一键清空按钮;缺省不渲染
|
||||
}
|
||||
|
||||
/*** 卡片入参 ***/
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
"entry": "src/index.ts",
|
||||
"exports": [
|
||||
"button(props):按钮,variant = primary | ghost | danger,disabled 支持布尔或信号禁用",
|
||||
"textField(props):文本输入,value 信号双向绑定,disabled 支持布尔或信号,onInput 输入回调;prefix/suffix 可选渲染为输入框内侧附加块",
|
||||
"textField(props):文本输入,value 信号双向绑定,disabled 支持布尔或信号,onInput 输入回调;prefix/suffix 可选渲染为输入框内侧附加块,clearable 开启后有值即显一键清空按钮",
|
||||
"inputNumber(props):数字输入,−/+ 按钮与键盘 ↑↓ 调节,min/max/step 夹取;prefix/suffix 可选渲染为整组前后附加块",
|
||||
"textarea(props):多行输入,value 信号双向绑定,disabled 支持布尔或信号,rows 控行数;prefix/suffix 可选渲染为上下附加块",
|
||||
"slider(props):滑块,value 信号双向绑定,min/max/step 可配,原生 range 承载键盘交互,右侧可显示当前值",
|
||||
|
||||
@@ -1669,6 +1669,26 @@ button.ua-step-dot:hover {
|
||||
font-size: var(--ua-font-size-md);
|
||||
user-select: none;
|
||||
}
|
||||
.ua-input-clear-group {
|
||||
display: inline-flex; /* 清空按钮 + 原有 suffix 并排 */
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
}
|
||||
.ua-input-clear {
|
||||
border: none; /* 一键清空:小圆钮,弱化视觉 */
|
||||
background: none;
|
||||
padding: 0 2px;
|
||||
color: var(--ua-color-text-muted);
|
||||
font-size: var(--ua-font-size-md);
|
||||
line-height: 1;
|
||||
cursor: pointer;
|
||||
}
|
||||
.ua-input-clear:hover {
|
||||
color: var(--ua-color-text);
|
||||
}
|
||||
.ua-input-clear.is-hidden {
|
||||
display: none; /* 空值时隐藏 */
|
||||
}
|
||||
|
||||
/* —— 时间线:纵向轴线 + 语义色节点,mode=right 时轴线居右 —— */
|
||||
.ua-tl {
|
||||
|
||||
Reference in New Issue
Block a user