iter(g19): textarea 字数统计

This commit is contained in:
HIsheR
2026-10-26 11:06:34 +08:00
parent bf05e991ba
commit 34abe3f1e8
9 changed files with 39 additions and 10 deletions
+11 -2
View File
@@ -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);
}
+1
View File
@@ -177,6 +177,7 @@ export interface TextareaProps {
onInput?: (value: string) => void; // 输入回调
prefix?: string | HTMLElement; // 上方附加块(如标题、计数说明);缺省不渲染
suffix?: string | HTMLElement; // 下方附加块(如字数提示);缺省不渲染
maxCount?: number; // 字数上限;传了即设原生 maxlength 并在下方显示实时计数
}
/*** 数字输入入参 ***/