iter(g19): textarea 字数统计
This commit is contained in:
@@ -2,7 +2,7 @@ import { effect, h } from '@ui-arch/frame';
|
||||
import { addonWrap } from './addons.js';
|
||||
import type { TextareaProps } from './types.js';
|
||||
|
||||
/*** 多行输入:value 信号双向绑定,disabled 支持布尔或信号禁用,onInput 为输入回调;prefix/suffix 包纵向附加组 ***/
|
||||
/*** 多行输入:value 信号双向绑定,disabled 支持布尔或信号禁用,onInput 为输入回调;prefix/suffix 包纵向附加组,maxCount 显示字数统计 ***/
|
||||
export function textarea(props: TextareaProps): HTMLElement {
|
||||
const el = h('textarea', { class: 'ua-textarea', rows: props.rows ?? 3, placeholder: props.placeholder ?? '', disabled: props.disabled }) as HTMLTextAreaElement; // false/undefined 移除属性,Signal 走响应式
|
||||
if (props.value) {
|
||||
@@ -13,5 +13,14 @@ export function textarea(props: TextareaProps): HTMLElement {
|
||||
});
|
||||
}
|
||||
if (props.onInput) el.addEventListener('input', () => props.onInput!(el.value));
|
||||
return addonWrap(el, props.prefix, props.suffix, true);
|
||||
let counter: HTMLElement | null = null; // 字数统计块;maxCount 未传不创建
|
||||
if (props.maxCount) {
|
||||
el.setAttribute('maxlength', String(props.maxCount)); // 原生拦截超限输入
|
||||
counter = h('span', { class: 'ua-textarea-count' });
|
||||
const syncCount = (): void => { counter!.textContent = `${el.value.length}/${props.maxCount}`; }; // 实时 n/limit
|
||||
el.addEventListener('input', syncCount); // 用户输入时同步
|
||||
effect(() => { if (props.value) props.value(); syncCount(); }); // 初始与外部写回时同步(读信号建依赖)
|
||||
}
|
||||
const suffix = counter ? h('span', { class: 'ua-textarea-count-group' }, counter, props.suffix ?? null) : props.suffix; // 计数并入下方附加组
|
||||
return addonWrap(el, props.prefix, suffix, true);
|
||||
}
|
||||
|
||||
@@ -177,6 +177,7 @@ export interface TextareaProps {
|
||||
onInput?: (value: string) => void; // 输入回调
|
||||
prefix?: string | HTMLElement; // 上方附加块(如标题、计数说明);缺省不渲染
|
||||
suffix?: string | HTMLElement; // 下方附加块(如字数提示);缺省不渲染
|
||||
maxCount?: number; // 字数上限;传了即设原生 maxlength 并在下方显示实时计数
|
||||
}
|
||||
|
||||
/*** 数字输入入参 ***/
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
"button(props):按钮,variant = primary | ghost | danger,disabled 支持布尔或信号禁用",
|
||||
"textField(props):文本输入,value 信号双向绑定,disabled 支持布尔或信号,onInput 输入回调;prefix/suffix 可选渲染为输入框内侧附加块,clearable 开启后有值即显一键清空按钮",
|
||||
"inputNumber(props):数字输入,−/+ 按钮与键盘 ↑↓ 调节,min/max/step 夹取;prefix/suffix 可选渲染为整组前后附加块",
|
||||
"textarea(props):多行输入,value 信号双向绑定,disabled 支持布尔或信号,rows 控行数;prefix/suffix 可选渲染为上下附加块",
|
||||
"textarea(props):多行输入,value 信号双向绑定,disabled 支持布尔或信号,rows 控行数;prefix/suffix 可选渲染为上下附加块,maxCount 显示实时字数并设原生上限",
|
||||
"slider(props):滑块,value 信号双向绑定,min/max/step 可配,原生 range 承载键盘交互,右侧可显示当前值",
|
||||
"checkbox(props):复选框,checked 信号双向绑定,disabled 支持布尔或信号禁用",
|
||||
"switchButton(props):开关,checked 信号双向绑定,disabled 支持布尔或信号禁用",
|
||||
|
||||
@@ -696,6 +696,17 @@
|
||||
font-size: var(--ua-font-size-md);
|
||||
resize: vertical;
|
||||
}
|
||||
.ua-textarea-count-group {
|
||||
display: flex; /* 字数统计 + 原有 suffix 并排靠右 */
|
||||
justify-content: flex-end;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
}
|
||||
.ua-textarea-count {
|
||||
color: var(--ua-color-text-muted);
|
||||
font-size: var(--ua-font-size-sm);
|
||||
font-variant-numeric: tabular-nums; /* 数字等宽,跳动不抖 */
|
||||
}
|
||||
|
||||
/* —— 数字输入 —— */
|
||||
.ua-input-number {
|
||||
|
||||
Reference in New Issue
Block a user