249 lines
15 KiB
TypeScript
249 lines
15 KiB
TypeScript
/**
|
||
* 文本 / 数值输入:Ant Design 5 Input 的唯一封装。
|
||
*
|
||
* 症状:页面里散落着 130 多个裸 `<input>`/`<textarea>`,浏览器默认皮肤
|
||
* (白底、系统圆角、原生数字步进器)既不吃主题也不吃尺子,
|
||
* 和同一行里的 antd 下拉差着一整个档位。
|
||
* 做法:控件一律来自 antd,这里只补三件事——
|
||
* 1) 事件形状沿用 React 原生(Input/TextArea 的 onChange 仍是 event),
|
||
* 页面迁移只是把标签换掉,逻辑一行不动;
|
||
* 2) NumberInput 把 antd 的 (value:number|null) 收窄成页面惯用的数字;
|
||
* 3) 宽度统一由字段栅格(Form.Item 的控件列)撑满,页面不再写 w-[...]。
|
||
*
|
||
* ref 一律是 **DOM 节点**(HTMLInputElement / HTMLTextAreaElement),不是 antd 的句柄:
|
||
* React 18 必须 forwardRef,而且页面拿到节点才能 focus / setSelectionRange /
|
||
* scrollIntoView——契约与理由见 lib/ref.ts。
|
||
*/
|
||
import { forwardRef, useEffect, useRef } from 'react'
|
||
import { Button, Input as AntInput, InputNumber } from 'antd'
|
||
import { IconClear, IconSearch } from '../icons'
|
||
import { domInput, domTextarea, passRef } from '../../lib/ref'
|
||
import { markFieldControl, useFieldControlId } from './fieldId'
|
||
|
||
const { TextArea: AntTextArea, Search: AntSearch } = AntInput
|
||
|
||
/** 单行文本(原生 event 兼容:onChange={(e) => setX(e.target.value)}) */
|
||
export const Input = forwardRef<HTMLInputElement, React.ComponentProps<typeof AntInput>>(
|
||
function Input(props, ref) {
|
||
/* id 由宿主 Field 提供(T1-N6):`<label for>` 指向的本体就是这颗 input */
|
||
return <AntInput {...props} id={useFieldControlId(props.id)} ref={(h) => passRef(ref, domInput(h))} />
|
||
})
|
||
|
||
/**
|
||
* 多行文本:`rows` 是**起步高度**,框随内容自己长(antd 的 autoSize)。
|
||
*
|
||
* 症状(2026-11 用户第二轮截图):抽屉里的文本域要么固定 96px 起步(旧
|
||
* `.nex-textarea-tall` 给容器挂 min-height,rows=2 直接失效、内容少时剩半盒空白),
|
||
* 要么在任务指令那里被写死成一个占掉半屏的大框——两个方向都不对:
|
||
* 高度是内容决定的,不是版面决定的。
|
||
* 做法:把页面写下来的 `rows` 翻译成 `autoSize={{ minRows: rows, maxRows: rows×4 }}`,
|
||
* 超过上限内部滚动,抽屉的总高度因此可预期。
|
||
* 为什么在 kit 里翻译而不是让 12 个页面各自改写:`rows` 的语义本来就是
|
||
* 「这个字段默认几行」,把「会不会长」这件事收在一处,页面不用记第二条规矩。
|
||
*/
|
||
export const TextArea = forwardRef<HTMLTextAreaElement, React.ComponentProps<typeof AntTextArea>>(
|
||
function TextArea({ rows, autoSize, id, ...rest }, ref) {
|
||
const grow = autoSize ?? (rows ? { minRows: rows, maxRows: Math.max(4, rows * 4) } : undefined)
|
||
return <AntTextArea {...rest} id={useFieldControlId(id)} autoSize={grow} ref={(h) => passRef(ref, domTextarea(h))} />
|
||
})
|
||
|
||
/* ── 步进器的可读名称(2026-11-09 · 阶段 18 · S-3-2)────────────────────────────
|
||
症状:全站 antd 内置件里最后剩下的英文可读名称,就是数字框上下两颗——
|
||
读屏念「Increase Value」/「Decrease Value」(第 9 闸 LOCALE 的在册豁免 12 处)。
|
||
根因:`rc-input-number@9.5.0` 把这两条 `aria-label` **写死**在 handler `<span>` 上
|
||
(`es/StepHandler.js:87` 与 `:97`),而且写在 `...sharedHandlerProps` **之后**
|
||
⇒ antd 的 `upHandler` / `downHandler` 只换得到里面的图形,换不掉外层那颗按钮的名字;
|
||
antd 的 `zh_CN` 语言包**没有 InputNumber 这一段**(阶段 12 打印过它的 keys)
|
||
⇒ 组件属性通道与语言包通道**两条都不存在**,剩下的只有「自己把名字改对」。
|
||
做法:只改名字,**结构、类名、像素、鼠标与键盘行为一个字都不动**——挂载后把这两条
|
||
换成中文,再用 MutationObserver 兜住重挂载。写之前先比一次当前值,
|
||
已经是中文就不写 ⇒ 观察者不会自己咬自己(不会形成 attribute 回环)。
|
||
为什么需要观察者而不是只在挂载时改一次:handler 是 `controls` 为真时才渲染的
|
||
(`rc-input-number/es/InputNumber.js:492` 就是 `controls && <StepHandler>`),
|
||
条件变化换上来的是两颗全新的 `<span>`;这一版本 antd 的 InputNumber
|
||
**没有 mobile 分支**(实测 560 与 1440 两态 handler 一直在,`grep mobile
|
||
antd/es/input-number/index.js` 为空),所以观察者今天兜的是重挂载而不是断点。
|
||
为什么落在 kit 这一层:全站数字框只有下面两个组件(R8 把 antd 的 import 关在
|
||
`theme/` · `app/` · `components/{ui,forms}`),在这里收口 = 页面 0 改动、
|
||
`controls={false}` 与自绘步进器那两条「要动已验证视觉」的路一条都没走。
|
||
验收:`semanticcheck` 第 10 条判据 LOCALE 的豁免表**已清空**——这两条英文名再出现就是违规;
|
||
反向验证见 `gateprobe` 判据 ⑳(`pb-locnum` 坏形状必须点名 / `pg-locnumzh` 必须放行);
|
||
活体读数(DOM 名 + AX 整名 + 步进链路 + 明暗截图)见
|
||
`reports/ui_evidence/nex-p18/s3step.py`。*/
|
||
const STEP_LABEL_ZH: Record<string, string> = {
|
||
'Increase Value': '增加数值',
|
||
'Decrease Value': '减少数值'
|
||
}
|
||
|
||
/**
|
||
* 就地把 wrapper 里那两颗步进按钮的英文名字换成中文(只认精确等值,别的 aria-label 一律不碰),
|
||
* 并把**已具名宿主**内部的图形消音(2026-11-09 · 阶段 23 · S-2 收口)。
|
||
*
|
||
* 为什么要第二步:改名之后 AT 念的是「增加数值」,可 antd 给那颗按钮装的图形
|
||
* `<span role="img" aria-label="up" class="anticon anticon-up ant-input-number-handler-up-inner">`
|
||
* 在 AX 树里仍是**独立对象**(`rc-input-number/es/StepHandler.js:87`/`:97` 的 upNode),
|
||
* 读屏念完宿主再补一句英文「up」——`semanticcheck` 5c 实测 1440/430 各 34 颗
|
||
* (`reports/ui_evidence/nex-p23/predis/`,另有浮层里的 12 颗)。口径与图标登记表一致:
|
||
* **宿主具名 ⇒ 图形消音**(规范 §5.1.1 第 ⑤ 道闸)。
|
||
*
|
||
* 三条边界,防止这一刀把别的东西削掉:
|
||
* · 只在宿主 `aria-label` **非空**时消音——宿主自己没名字时,里面那颗图形可能是唯一
|
||
* 的名称来源,隐藏它就把控件念成「未命名」了(这一族在 wrapper 里今天不存在,
|
||
* 但判据不能依赖「今天不存在」);
|
||
* · `querySelectorAll` 只向下找后代 ⇒ 宿主自己就是 `role=img` 时不会被自己消音;
|
||
* · 只 `setAttribute`,不增删节点、不改类名 ⇒ 像素、鼠标按住连击、键盘步进全不动;
|
||
* 观察者盯的是 `attributeFilter:['aria-label']`,写 `aria-hidden` 不会自咬成回环。
|
||
* React 侧同样安全:这些 `aria-hidden` 不在 vDOM 属性里,React 更新时不会把它擦掉;
|
||
* handler 重挂载换上来的是全新节点,由 `childList` 那一路观察者重新补写。
|
||
*/
|
||
function renameStepHandlers(root: HTMLElement | null) {
|
||
if (!root || typeof root.querySelectorAll !== 'function') return
|
||
root.querySelectorAll<HTMLElement>('[aria-label]').forEach((el) => {
|
||
const label = el.getAttribute('aria-label') || ''
|
||
const next = STEP_LABEL_ZH[label]
|
||
if (next) el.setAttribute('aria-label', next)
|
||
if ((next ?? label).trim() === '') return
|
||
el.querySelectorAll<HTMLElement>('[role=img]').forEach((g) => g.setAttribute('aria-hidden', 'true'))
|
||
})
|
||
}
|
||
|
||
/** 产出一个可以直接放进 `<InputNumber ref>` 的回调:拿到 wrapper 节点就负责它的步进器名字 */
|
||
function useStepLabelRef() {
|
||
const box = useRef<{ el: HTMLElement | null; ob: MutationObserver | null }>({ el: null, ob: null })
|
||
useEffect(() => () => {
|
||
box.current.ob?.disconnect()
|
||
box.current = { el: null, ob: null }
|
||
}, [])
|
||
return (h: unknown) => {
|
||
const el = (h as { nativeElement?: HTMLElement } | null)?.nativeElement ?? null
|
||
if (!el || el === box.current.el) return
|
||
box.current.ob?.disconnect()
|
||
box.current.el = el
|
||
renameStepHandlers(el)
|
||
if (typeof MutationObserver !== 'undefined') {
|
||
box.current.ob = new MutationObserver(() => renameStepHandlers(el))
|
||
box.current.ob.observe(el, { subtree: true, childList: true, attributeFilter: ['aria-label'] })
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 数值输入:antd 的数字框(步进按钮在框内,不再露出系统控件)。
|
||
*
|
||
* 「清空」这件事有两种语义,页面原先就是这么用的,所以拆成两个组件而不是一个开关:
|
||
* · NumberInput 清空 = 回到 min(没有 min 就 0)——表单里的步数、fps、RPM 这类必填数;
|
||
* · MaybeNumberInput 清空 = null——「留空表示不改 / 未设置」(分镜批量改时长、字幕起止 ms)。
|
||
* 两个都把外来的字符串数字折成 number(接口经常回 "5"),页面拿到的一定是自己要的形态。
|
||
*/
|
||
export const NumberInput = forwardRef<HTMLInputElement, NumProps<number>>(function NumberInput(
|
||
{ value, onChange, min, max, step, id, ...rest }, ref) {
|
||
const stepRef = useStepLabelRef()
|
||
return (
|
||
<InputNumber
|
||
{...rest}
|
||
id={useFieldControlId(id)}
|
||
aria-valuetext={stepValueText(rest, value, min, max)}
|
||
ref={(h) => { stepRef(h); passRef(ref, domInput(h)) }}
|
||
value={toNum(value)}
|
||
min={numOrUndef(min)}
|
||
max={numOrUndef(max)}
|
||
step={numOrUndef(step)}
|
||
onChange={(next: number | string | null) => {
|
||
const n = toNum(next)
|
||
onChange?.(n === null ? (numOrUndef(min) ?? 0) : n)
|
||
}}
|
||
/>
|
||
)
|
||
})
|
||
|
||
/** 清空即 null:这一格留空=不改这个字段 */
|
||
export const MaybeNumberInput = forwardRef<HTMLInputElement, NumProps<number | null>>(
|
||
function MaybeNumberInput({ value, onChange, min, max, step, id, ...rest }, ref) {
|
||
const stepRef = useStepLabelRef()
|
||
return (
|
||
<InputNumber
|
||
{...rest}
|
||
id={useFieldControlId(id)}
|
||
aria-valuetext={stepValueText(rest, value, min, max)}
|
||
ref={(h) => { stepRef(h); passRef(ref, domInput(h)) }}
|
||
value={toNum(value)}
|
||
min={numOrUndef(min)}
|
||
max={numOrUndef(max)}
|
||
step={numOrUndef(step)}
|
||
onChange={(next: number | string | null) => onChange?.(toNum(next))}
|
||
/>
|
||
)
|
||
})
|
||
|
||
const toNum = (v: unknown): number | null => {
|
||
if (v === null || v === undefined || v === '') return null
|
||
const n = Number(v)
|
||
return Number.isNaN(n) ? null : n
|
||
}
|
||
const numOrUndef = (v?: number | string) => (v === undefined || v === '' ? undefined : Number(v))
|
||
|
||
type NumProps<Out> = {
|
||
/** 表单状态里数字经常是字符串(接口回 "5"),进来宽容 */
|
||
value?: number | string | null
|
||
onChange?: (next: Out) => void
|
||
min?: number | string
|
||
max?: number | string
|
||
step?: number | string
|
||
} & Omit<React.ComponentProps<typeof InputNumber>, 'value' | 'onChange' | 'min' | 'max' | 'step'>
|
||
|
||
/**
|
||
* 搜索框:放大镜 + 一键清空,行为与 Input 一致(回车提交由页面自己接 onPressEnter)。
|
||
*
|
||
* 尾部那颗放大镜按钮与清空键都是 antd 在**内部**渲染的图形,绕不过我们的登记表,
|
||
* 于是它们把英文字形名当成控件的可读名称交给读屏:「search」「close-circle」
|
||
* (2026-11-08 · 阶段 8.5 在册 AT-1 → S-2)。两处都在 kit 这一层收口,页面 0 改动:
|
||
* · `enterButton` 给一颗显式 Button:类名(ant-btn-sm ant-btn-icon-only
|
||
* ant-input-search-button)、尺寸(跟随 Dense 的 componentSize)、图形(IconSearch
|
||
* 与 antd 默认的 SearchOutlined 同一只)全部照原样,只多了一句 aria-label;
|
||
* `disabled` / `loading` 手动接上——antd 在不传元素时会自动接,传元素就交给调用方了;
|
||
* · `clearIcon` 换 IconClear(同一只 CloseCircleFilled,同一份 svg 与类名),名字改成「清空搜索」。
|
||
* 副作用只有一处、且不在默认态:传了 enterButton 元素,antd 会给外层挂
|
||
* `.ant-input-search-with-button`,它唯一的实际作用是悬停时把输入框的 z-index 从 1 降到 0
|
||
* (按钮那条 1px 左边框会压住输入框的悬停描边)。styles/app.css 里补回 1,悬停像素与改前一致。
|
||
*/
|
||
export const SearchInput = forwardRef<HTMLInputElement, React.ComponentProps<typeof AntSearch>>(
|
||
function SearchInput(props, ref) {
|
||
const { disabled, loading } = props
|
||
return <AntSearch allowClear={{ clearIcon: <IconClear aria-label="清空搜索" /> }}
|
||
prefix={<IconSearch />}
|
||
enterButton={<Button icon={<IconSearch />} aria-label="搜索"
|
||
disabled={disabled} loading={loading} />}
|
||
{...props}
|
||
id={useFieldControlId(props.id)}
|
||
ref={(h) => passRef(ref, domInput(h))} />
|
||
})
|
||
|
||
/**
|
||
* 区间数值框的朗读文本(2026-11-10 · 阶段 35 · T-1 / T1-N1)。
|
||
*
|
||
* 症状(阶段 9 · Safari + VoiceOver 实测,登记在 `docs/NEX-对照与例外.md` §6 T1-N1):
|
||
* 同时带 `min` 与 `max` 的 `spinbutton`,Safari 把「当前值」折算成**区间百分比**播报——
|
||
* 优先级 5/9 念成「55.6 %」、音量 (32−1)/255 念成「12.2 %」。
|
||
* 页面上写的是 5,读屏耳朵里是一个页面上根本不存在、也没人算过的数。
|
||
* 做法:只有 `min` 与 `max` **同时存在**时才补 `aria-valuetext`,内容=输入框里显示的那个数
|
||
* (所见即所念);调用点自己写了 `aria-valuetext` 时听它的。
|
||
* 没有上下限的数值框浏览器不做折算(读到的本来就是数字),一条 `aria-valuetext` 都不加,
|
||
* 免得给本来正确的读数再套一层可能过期的文本。
|
||
* 影响面:`role`、`aria-valuemin/max/now`、类名、像素、步进手感、键盘行为一字不动,
|
||
* 只多一个属性;带上下限的数值框在 kit 一处收口,页面 0 改动。
|
||
*/
|
||
function stepValueText(
|
||
rest: Record<string, any>, value: unknown, min?: number | string, max?: number | string,
|
||
): string | undefined {
|
||
const given = rest['aria-valuetext']
|
||
if (given !== undefined) return given
|
||
// min / max 在组件签名里就被解构走了,rest 里根本没有这两项
|
||
// —— 活体尺子 valuetext.py 抓到过只查 rest 的那一版一条都没补上(空转)。
|
||
if (numOrUndef(min) === undefined || numOrUndef(max) === undefined) return undefined
|
||
const n = toNum(value)
|
||
return n === null ? undefined : String(n)
|
||
}
|
||
|
||
/* 打上「可被宿主 Field 命名」的标记:只有这些 kit 件,`Field` 才敢写 `htmlFor`(见 fieldId.ts) */
|
||
;[Input, TextArea, NumberInput, MaybeNumberInput, SearchInput].forEach((c) => markFieldControl(c))
|