nex_animation/frontend/src/components/ui/Fields.tsx

173 lines
8.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 字段体系(阶段 6 自 `Controls.tsx` 拆出,实现原样搬移):
* 表单里「一个标签 + 一个控件 + 一句校验」的唯一写法——
* Field / FieldBlock / FormGrid / FormRow / Check / RadioGroup / FilePick。
* 控件栏与胶囊在 `Ctlbar.tsx`;两类的度量都来自 `theme/antd.ts` 那把尺子,不另开档位。
*/
import { type ReactNode, type CSSProperties } from 'react'
import { Checkbox as AntCheckbox, Form, Radio as AntRadio, Row } from 'antd'
import { cn } from '../../lib/cn'
import { Button } from './Button'
import { ROW_STEPS, colSpans, toCols } from './grid'
type FieldWidth = 'default' | 'grow' | 'narrow' | 'wide' | 'full'
/**
* 字段宽度:宽度是**字段自己的属性**,列边界由 Row/Col 给(FormRow)。
* 旧的 `nf-w-*` 五条类名靠 `--label-w` 间接生效,改成 inline style 后
* 「这一格多宽」在 DOM 上直接看得见,工具条里的窄字段也不必再猜轨道宽度。
*/
const WIDTHS: Record<FieldWidth, CSSProperties> = {
default: {},
grow: { flex: '1 1 220px', minWidth: 150 },
narrow: { width: 128 },
wide: { width: 240 },
full: { width: '100%' }
}
/* 症状(visualcheck 430 档 dispatch):narrow/wide 把宽度写在**字段盒子**上,
而 inline 字段的标签是 nowrap、控件列 flex:none,两者自然宽度合计大于盒子时,
控件直接捅出工具条——「单次派发上限 / 排空等待」溢出 54px。
做法:inline 字段把同一个数交给控件列(--nex-ctl-w),盒子按内容定尺,
工具条的 flex-wrap 于是量得到真实宽度,放不下就整条换行(窄屏允许折行、不允许改竖排)。
为什么只改 inline:抽屉/表单里的字段坐在栅格里,盒子宽度就是列宽,那是对的。 */
const INLINE_CTL_W: Partial<Record<FieldWidth, number>> = { narrow: 128, wide: 240 }
/**
* 字段 = antd `Form.Item`。默认标签在上(抽屉 / 对话框 / 表单);
* `inline` 给筛选条这种「标签和控件必须同一行」的字段(horizontal 布局)。
*
* 已验证:孤立使用(外层没有 `<Form>`)时 antd 仍然渲染完整的
* `.ant-form-item-vertical` 结构与样式,所以字段不需要表单外壳也能独立成立;
* `FormGrid` 只提供一个作用域类名,用来给整片字段统一上间距与控件撑满规则。
* `title` 挂在标签文字上:版面里不放整段解释(规范 §2 G2),说明走悬浮。
*/
export function Field({ label, required, children, width = 'default', className, style, hint, title, inline }: {
label?: ReactNode; required?: boolean; children: ReactNode; width?: FieldWidth
className?: string; style?: CSSProperties; hint?: ReactNode; title?: string
/** 标签与控件同一行(筛选条 / 工具条);不写就是标签在上(表单 / 抽屉) */
inline?: boolean
}) {
return (
<Form.Item
layout={inline ? 'horizontal' : 'vertical'}
required={required}
label={label === undefined ? undefined : <span title={title}>{label}</span>}
extra={hint}
className={cn('nex-field min-w-0', inline && 'nex-field-inline', width !== 'default' && 'nex-field-fill', className)}
style={{ ...(inline && INLINE_CTL_W[width]
? { ['--nex-ctl-w' as string]: `${INLINE_CTL_W[width]}px` }
: WIDTHS[width]), ...style }}
>
{children}
</Form.Item>
)
}
/**
* 长字段(旧「标签在上」专用形态):现在和 Field 完全同一种形态,只是默认撑满整行。
* 保留这个名字是因为 54 处调用写着它,删名字要动 12 个页面而得不到任何东西。
*/
export function FieldBlock({ label, required, children, className, style, title, hint }: {
label?: ReactNode; required?: boolean; children: ReactNode; className?: string
style?: CSSProperties
/** 标签的含义/后果说明:分镜检查器里几乎每个长字段都要一句 */
title?: string; hint?: ReactNode
}) {
return (
<Field label={label} required={required} children={children} width="full"
className={className} style={style} title={title} hint={hint} />
)
}
/**
* 表单作用域:只落下 `.nex-form` 这个标记类(app.css 表单 glue ①=控件撑满列宽)。
*
* 旧版本在这里定义 12 等分轨道和 `--label-w`(「标题与控件对齐」的唯一机制)。
* 标签改到控件上方之后,对齐由 antd 表单栅格负责,这两套私有尺子整体退场——
* 列边界从此只有 Row/Col 一个来源(见 grid.tsx),不会再出现两行字段错开 110px。
*
* **`<FormRow>` 自己就带这一层**(见下面的 FormRow):一行字段本来就是表单,
* 作用域不该靠调用点记得再包一层。这一改之前站内有 6 处 FormRow 漏包,
* 症状都一样:同一格里下拉铺满、数字框按 antd 的默认 90px 缩成一截,
* 右边界参差不齐(时间线「混音与字幕设置」实测 214 的格子摆 90 的输入框)。
* 还要用它的场合只剩一个:抽屉里**竖排**的 FieldBlock 串(不排成栅格的那堆字段)。
*/
export function FormGrid({ children, className }: { children: ReactNode; className?: string }) {
return <div className={cn('nex-form min-w-0', className)}>{children}</div>
}
/**
* 表单里的一行 = antd Row + Col(cols = 这一行放几个字段)。
*
* 纵向间距不在 gutter 上:Form.Item 自带 24px 下边距,Row 再给纵向 gutter 就是 48。
* `tight` 是给「一行里全是窄字段」的场合(时间线/分镜的批量设置),
* 把下边距压到 8,见 app.css 的 `.nf-tight`。
*/
export function FormRow({ children, cols = 3, className, tight, gap = 16 }: {
children: ReactNode
/** 这一行放几个字段(窄屏按 ROW_STEPS 自动降列) */
cols?: 2 | 3 | 4 | 6
className?: string
tight?: boolean
/** 列间距:默认 16(antd 表单列间),窄屏由 Col 折行接管 */
gap?: number
}) {
return (
<Row gutter={[tight ? 12 : gap, 0]}
className={cn('nex-form nex-form-row min-w-0', tight && 'nf-tight', className)}>
{toCols(children, colSpans(cols, undefined, ROW_STEPS[cols] || []), 'min-w-0')}
</Row>
)
}
/** 复选(勾选框 + 文案整块可点):本体是 antd Checkbox,尺寸走 token */
export function Check({ checked, onChange, children, disabled, className, title }: {
checked: boolean; onChange: (next: boolean) => void; children: ReactNode; disabled?: boolean
className?: string; title?: string
}) {
return (
<AntCheckbox checked={checked} disabled={disabled} title={title} className={cn('nex-check', className)}
onChange={(e) => onChange(e.target.checked)}>
{children}
</AntCheckbox>
)
}
/** 单选组(互斥选项):antd Radio.Group;分段切换请用 Segmented */
export function RadioGroup({ value, onChange, options, className, disabled }: {
value: any; onChange: (next: any) => void; options: { value: any; label: ReactNode; disabled?: boolean }[]
className?: string; disabled?: boolean
}) {
return (
<Form.Item layout="vertical" className={cn('nex-field min-w-0 !mb-0', className)}>
<AntRadio.Group value={value} disabled={disabled}
onChange={(e) => onChange(e.target.value)}
options={options.map((o) => ({ label: o.label, value: o.value, disabled: o.disabled }))} />
</Form.Item>
)
}
/**
* 文件选择:整块按钮就是热区,点哪里都开文件对话框。
* 选完把 input 清空:否则连着两次选同一个文件 value 没变,onChange 不再触发
* (分镜导入要反复试同一份脚本)。
*/
export function FilePick({ label, icon, accept, multiple, onFiles, className, title, disabled }: {
label: ReactNode; accept?: string; multiple?: boolean; onFiles: (files: File[]) => void; className?: string
/** 悬浮说明「选出来的文件会落到哪里」:一页里上传点越多越需要这句话 */
title?: string; disabled?: boolean
/** 图标走 antd Button 的 icon 槽,不写进 label —— 否则间距由 label 里的空格决定 */
icon?: ReactNode
}) {
return (
<Button className={className} title={title} disabled={disabled} icon={icon}>
<span className="contents">{label}</span>
<input type="file" className="nex-file-input" accept={accept} multiple={multiple} title={undefined}
onChange={(e) => {
const files = Array.from(e.currentTarget.files || [])
e.currentTarget.value = ''
onFiles(files)
}} />
</Button>
)
}