572 lines
30 KiB
TypeScript
572 lines
30 KiB
TypeScript
/**
|
||
* 布局层:表单、工具条、筛选行(Ant Design Form·Row·Col·Flex·Tag 的唯一封装)。
|
||
*
|
||
* 症状(2026-11 第二轮重写的对象):第一轮只换了原子控件,**字段与排布仍是自绘**——
|
||
* `.nex-field` 用 CSS Grid 把标签摆到控件左边,标签列宽靠私有变量 `--label-w`,
|
||
* `FormGrid`/`FormRow` 又各自一套 12 等分轨道。结果是资产库抽屉里
|
||
* 「标签在左」的三列字段和「标签在上」的长字段混排:列边界参差、
|
||
* 只有一个字段的行把输入框缩成一小截、`TEXTAREA_TALL` 把 rows=2 撑到 150px。
|
||
* 用户说的「布局还是不对」,病根全在这三个文件级的自绘栅格里。
|
||
* 做法:**字段就是 antd Form.Item,标签一律在上(layout="vertical")**。
|
||
* 已验证 antd 5.29 会为一个孤立的 `Form.Item layout="vertical"` 生成
|
||
* `.ant-form-item-vertical` 的完整样式(不需要外层 `<Form>`),
|
||
* 所以全站只有一种字段形态,抽屉、对话框、工具条共用同一条标签基线,
|
||
* 「对齐」这件事交还给 antd 的表单栅格,私有变量整层删除。
|
||
* <Ctlbar> 含字段的横条 = Dense + Flex(align=end, wrap)
|
||
* <FormGrid> 表单作用域(只管标记类名,间距由 Form.Item 自己给)
|
||
* <FormRow> 表单里的一行 = Row + Col(断点沿用 antd Grid,见 grid.tsx)
|
||
* <ChipBar> 可点筛选行 = Flex + Tag.CheckableTag
|
||
* <Pager> 分页条(page_size 唯一的家)
|
||
* 高度不写在控件上:紧凑容器只做一件事——`<ConfigProvider componentSize="small">`
|
||
* 下发 antd 第二档;自绘的那几处读同一个 `--ant-control-height[-sm]`,两条路同一个数。
|
||
*/
|
||
import { cloneElement, isValidElement, useLayoutEffect, useRef, useState, type CSSProperties, type MouseEvent, type ReactElement, type ReactNode } from 'react'
|
||
import { ConfigProvider, Checkbox as AntCheckbox, Flex, Form, Pagination, Radio as AntRadio, Row, Tag, Typography } from 'antd'
|
||
import { cn } from '../../lib/cn'
|
||
import { Button } from './Button'
|
||
import { Select } from '../forms/Select'
|
||
import { IconClose } from '../icons'
|
||
import { colSpans, toCols, FILL_AT, type GridStep } from './grid'
|
||
|
||
/** 分组说明(ChipBar / TagGroup 行首那句「筛选:」):全站只此一种小标签字 */
|
||
export const GROUP_LABEL_CLS = 'text-xs text-text-tertiary'
|
||
|
||
/**
|
||
* 紧凑档作用域:工具条 / 分页条 / 卡片动作行里所有 antd 控件降一档
|
||
* (antd 自己的第二档 controlHeightSM = 24)。页面不需要、也不准再给单个控件写 size。
|
||
*
|
||
* 除了下发 ConfigProvider,还落下一个 .nex-dense 类名:自绘的东西(checkable tag 的
|
||
* 高度)不在 antd 的组件树里,读不到 componentSize,只能靠这个标记对齐到同一个 token。
|
||
*/
|
||
export function Dense({ children, className }: { children: ReactNode; className?: string }) {
|
||
return (
|
||
<ConfigProvider componentSize="small">
|
||
<div className={cn('nex-dense', className)}>{children}</div>
|
||
</ConfigProvider>
|
||
)
|
||
}
|
||
|
||
/**
|
||
* 筛选条 / 工具条:一条横排,控件在同一条中心线上,能一行放完就一行放完。
|
||
*
|
||
* 症状(2026-11 用户第三轮截图):项目列表的「搜索 + 状态」是两个字段的活,
|
||
* 却因为字段一律「标签在上」而占掉两行——第一行两个标签,第二行两个控件,
|
||
* 右边还空着半条工具条。筛选这种事本来是一次扫读,被排成了表单。
|
||
* 做法:这里的字段用 <Field inline>(antd Form.Item 的 horizontal 布局:标签在左、
|
||
* 控件在右,同一行),整条 Ctlbar 垂直居中(align=center)而不是贴底;
|
||
* 放不下时才折行(Flex wrap)。字段仍不带下边距(app.css 归零)。
|
||
* 为什么还给 Ctlbar 而不是新建一只组件:它表达的语义就是「含字段的横条」,
|
||
* 要改的是「字段这一横条里该长成什么样」,不是多出第八种容器。
|
||
*
|
||
* `cols={N}`(2026-11 第七轮):**三个以上同类字段并排时必须是栅格**,不再是 flex-wrap
|
||
* 自然排列。自然排列下每个 inline 字段按「标签宽 + 控件宽」定尺,标签字数不一样
|
||
* (状态 / 素材情况 / 排序)盒子就参差,同排控件的右边缘各在一个位置上——
|
||
* 资产库那条筛选条实测 330 / 170 / 170 / 142 / 177。铺成 Row·Col 之后
|
||
* 列边界由栅格给,控件列跟着格子伸缩,同排右边缘必然对齐。
|
||
* 只有「几颗按钮 / 一个字段 + 动作」这种杂项横条才留在自然排列。
|
||
*/
|
||
export function Ctlbar({ children, className, as = 'div', cols }: {
|
||
children: ReactNode; className?: string; as?: 'div' | 'nav' | 'section'
|
||
/** 这一条铺成 N 等分栅格(窄屏按 ROW_STEPS 自动降列);不写=按钮/杂项横条,自然排列 */
|
||
cols?: 2 | 3 | 4 | 5 | 6
|
||
}) {
|
||
return (
|
||
<Dense className="contents">
|
||
{cols ? (
|
||
<Row gutter={[12, 8]} className={cn('nex-ctlbar nex-ctlbar-grid min-w-0', className)}>
|
||
{tailRight(toCols(children, colSpans(cols, undefined, ROW_STEPS[cols] || []), 'min-w-0'))}
|
||
</Row>
|
||
) : (
|
||
<Flex component={as} wrap align="center" gap={8} className={cn('nex-ctlbar min-w-0', className)}>
|
||
{children}
|
||
</Flex>
|
||
)}
|
||
</Dense>
|
||
)
|
||
}
|
||
|
||
/**
|
||
* 栅格工具条的最后一格:如果坐的是 <BarRight>(贴右缘的动作组),让它伸缩到行尾。
|
||
*
|
||
* 见 grid.tsx 的 FILL_AT:固定 span 的格子只能贴**列**边界,块数除不尽列数时
|
||
* 末尾那格停在半空中(分镜页的「导出」实测落在 75% 处)。整条的右缘要靠格子自己吃掉
|
||
* 剩余宽度。只认 BarRight——末尾是普通字段时它就该老老实实占一格,不该被拉宽。
|
||
*/
|
||
function tailRight(cells: ReactNode[]): ReactNode[] {
|
||
const last = cells[cells.length - 1]
|
||
if (!isValidElement(last)) return cells
|
||
const inner = (last.props as { children?: ReactNode }).children
|
||
if (!isValidElement(inner) || inner.type !== BarRight) return cells
|
||
const props = last.props as { className?: string }
|
||
const next = cells.slice()
|
||
next[next.length - 1] = cloneElement(last as ReactElement<{ className?: string }>,
|
||
{ className: cn(props.className, 'nex-ctlbar-tail'), ...FILL_AT })
|
||
return next
|
||
}
|
||
|
||
/**
|
||
* 同一组的**可枚举选项**(出片预设、画质档位…)=等宽格子,不是 chip 流。
|
||
*
|
||
* 症状:出片预设用 ChipBar,6 个预设宽度 59–168 全凭文案长短,折行点随文案漂移,
|
||
* 第二行右侧空出一段,读者没法用「第几行第几个」定位,也数不清选了几个。
|
||
* 做法:选项是有限集合就是栅格——标签在上、每格等宽、胶囊撑满格子,
|
||
* 选中态(CheckableTag)保持不变,只是坐在格子里。
|
||
* 与 ChipBar 的分工:**筛已有内容**(标签云、状态快切)用 ChipBar 的胶囊流;
|
||
* **从一组固定值里挑**(预设 / 档位 / 模板)用 OptionGrid。
|
||
*/
|
||
/**
|
||
* 分组选择区(预设、模式、开关组)。
|
||
*
|
||
* `labelTitle` 是给组标签用的悬浮说明:按 G2,「可多选,依次入队」这类操作说明
|
||
* 不该在标签上挂一个括号常驻版面,也不该像 `hint` 那样再占右侧一格——它和
|
||
* `Field` 的 `title` 是同一个位置(悬停才出现),所以单独开一个 prop,
|
||
* 而不是让调用点自己把标签写成 JSX 套 span(那等于每页各画一遍 tooltip)。
|
||
*/
|
||
export function OptionGrid({ children, cols = 3, label, labelTitle, className, hint }: {
|
||
children: ReactNode; cols?: 2 | 3 | 4; label?: ReactNode; className?: string; hint?: ReactNode
|
||
labelTitle?: string
|
||
}) {
|
||
return (
|
||
<div className={cn('min-w-0', className)}>
|
||
{(label || hint) && (
|
||
<div className="mb-1.5 flex flex-wrap items-baseline justify-between gap-2">
|
||
{label && <span className={GROUP_LABEL_CLS} title={labelTitle}>{label}</span>}
|
||
{hint && <span className={GROUP_LABEL_CLS}>{hint}</span>}
|
||
</div>
|
||
)}
|
||
<Dense className="contents">
|
||
<Row gutter={[8, 8]} className="min-w-0 nex-opt-grid">
|
||
{toCols(children, colSpans(cols, undefined, ROW_STEPS[cols] || []), 'min-w-0 nex-opt-cell')}
|
||
</Row>
|
||
</Dense>
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/**
|
||
* 卡片 / 列表行 / 抽屉段落末尾的动作行:横排、换行、间距 8,档位由容器下发(24)。
|
||
*
|
||
* 【为什么这里曾有两个实现】历史上 `Panel.Actions` 不包 Dense(按钮全 md 32)、
|
||
* `CardActions` 包了 Dense(全 sm 24),页面又在 Actions 里补 size="sm",
|
||
* 于是素材墙一张卡底下「AI 生成」24、「上传」32、「删除」32。
|
||
* 现在只有一个实现:页面既不用、也不准再给行里的单个控件写 size。
|
||
*/
|
||
export function CardActions({ children, className }: { children: ReactNode; className?: string }) {
|
||
return (
|
||
<Dense className="contents">
|
||
<Flex wrap align="center" gap={8} className={cn('nex-card-actions min-w-0', className)}>
|
||
{children}
|
||
</Flex>
|
||
</Dense>
|
||
)
|
||
}
|
||
|
||
/** `CardActions` 的旧名字:同上,只多一个 `mt-2` 的段落间距 */
|
||
export function Actions({ children, className }: { children: ReactNode; className?: string }) {
|
||
return <CardActions className={cn('mt-2', className)}>{children}</CardActions>
|
||
}
|
||
|
||
/** 组内子项的间距:横排竖排同一个数,和 CardActions / Ctlbar 的 gap 对齐 */
|
||
const AG_GAP = 8
|
||
const px = (v: string) => parseFloat(v) || 0
|
||
|
||
/**
|
||
* 「装不下就整组竖排」的自动档:量整组的自然宽度和能给它的最窄祖先,放不下就翻竖排。
|
||
*
|
||
* 症状(2026-11 第九轮实测,`scripts/ui/btnrowcheck.py`):分镜导入那三颗
|
||
* 「文本预览 → 导入预演(含图片匹配)→ 正式导入」在 1024 折成 2+1
|
||
* (tops 1108/1108/1140)、860 折成三行;时间线 1024 档「补齐缺失配音」
|
||
* 单独掉第二排。`Flex wrap` 折的是**组内子项**,读者看到的是
|
||
* 「这一排怎么少了一颗」,而它们明明是同一条流程的三步。
|
||
* 做法:两条尺子,页面都不用自己判断——
|
||
* ① 横排时把盒子钉在 `max-content`(只在父级会折行时钉:栅格格子 min-w-0 里
|
||
* 钉会把列宽捅破),父容器于是把整组当**一个** item,放不下整组挪下一行;
|
||
* ② 整组连一整行都放不下(窄卡、窄栏、430 手机档)时整组改竖排等宽,
|
||
* 和页面手写的 `stack` 同一个视觉,不再是参差的 2 + 1。
|
||
* 自然宽度只有横排时可测(竖排后子项被拉伸到等宽),所以量一次缓存,
|
||
* 容器重新宽过缓存值就自动恢复横排。
|
||
* 用法:`ActionGroup` 内部用,页面不用管。第 17 轮之前只有 `PageHead` 传过
|
||
* `auto={false}`(它自带一套「整组满宽 + 按钮等分」),可那套等分恰恰把
|
||
* `natural()` 的量法喂死了(子项 flex-grow>0 就返回 0),于是页头照旧折成
|
||
* 2 + 1;页头改回自动档之后站内再没有容器要关它,这个开关连着删了——
|
||
* 窄屏怎么排只该有一个答案,多留一条路就是多一处会烂掉的豁免。
|
||
* 为什么把结果做成 state 而不是直接写 `el.style`:className 由 React 拥有,
|
||
* 竖排↔横排切换时 React 会重写 className,命令式加上去的类会被抹掉,
|
||
* 之后没有 resize 就再也补不回来——症状是「窄屏看久了又长出孤儿按钮」。
|
||
*/
|
||
function useAutoStack() {
|
||
const host = useRef<HTMLDivElement | null>(null)
|
||
const mode = useRef(false) // 当前是否竖排:effect 里读它,避免闭包读到旧 state
|
||
const need = useRef(0) // 横排需要的自然宽度(只在横排时量得到)
|
||
const [vertical, setVertical] = useState(false)
|
||
const [atom, setAtom] = useState(false)
|
||
|
||
useLayoutEffect(() => {
|
||
const el = host.current
|
||
if (!el) return
|
||
const parents: HTMLElement[] = []
|
||
for (let n = el.parentElement; n && n !== document.documentElement; n = n.parentElement) parents.push(n)
|
||
/* 能容下整组的最窄祖先内容宽(从父级一层层往里收;display:contents 的 Dense 不算一层) */
|
||
const room = () => {
|
||
let least = Infinity
|
||
for (const n of parents) {
|
||
const cs = getComputedStyle(n)
|
||
if (cs.display === 'contents') continue
|
||
const w = n.clientWidth - px(cs.paddingLeft) - px(cs.paddingRight)
|
||
if (w > 0) least = Math.min(least, w)
|
||
}
|
||
return least
|
||
}
|
||
/* 整组横排的自然宽:子项都按内容定尺才可测,含弹性控件或没渲染完就返回 0(不判断) */
|
||
const natural = () => {
|
||
const kids = Array.from(el.children) as HTMLElement[]
|
||
const widths = kids.map((k) => k.getBoundingClientRect().width)
|
||
if (!kids.length || widths.some((w) => w <= 0)) return 0
|
||
if (kids.some((k) => px(getComputedStyle(k).flexGrow) > 0)) return 0
|
||
return widths.reduce((sum, w) => sum + w, 0) + AG_GAP * (kids.length - 1)
|
||
}
|
||
/* 只在布局父级会折行时钉 max-content:这样「挪行」由父容器做,整组一起挪。
|
||
`Dense` 挂的是 display:contents,它在 DOM 上是一层、在布局上不是,
|
||
直接从 parents[0] 读会把 Ctlbar / CardActions 看成 contents 而漏掉整组钉宽。 */
|
||
const styles = parents.map((n) => getComputedStyle(n))
|
||
let li = 0
|
||
while (styles[li] && styles[li].display === 'contents') li += 1
|
||
const atomic = !!styles[li] && styles[li].display === 'flex' && styles[li].flexWrap === 'wrap'
|
||
|
||
let raf = 0
|
||
const measure = () => {
|
||
raf = 0
|
||
if (!mode.current) { const n = natural(); if (n > 0) need.current = n }
|
||
const want = need.current > 0 && need.current > room() + 0.5
|
||
setAtom(atomic && !want)
|
||
if (want === mode.current) return
|
||
mode.current = want
|
||
setVertical(want)
|
||
}
|
||
const schedule = () => { if (!raf) raf = requestAnimationFrame(measure) }
|
||
|
||
const ro = new ResizeObserver(schedule)
|
||
;[el, ...parents.slice(0, 3)].forEach((n) => ro.observe(n))
|
||
window.addEventListener('resize', schedule)
|
||
measure()
|
||
return () => {
|
||
ro.disconnect()
|
||
window.removeEventListener('resize', schedule)
|
||
if (raf) cancelAnimationFrame(raf)
|
||
}
|
||
}, [])
|
||
|
||
return { host, vertical, atom }
|
||
}
|
||
|
||
/**
|
||
* 同一概念的一组动作 / 勾选项:整组对外是**一个** flex item,要换行就整组一起换行。
|
||
*
|
||
* 症状(2026-11 第四轮):`CardActions` / `Ctlbar` 是 `flex-wrap` 的自然排列,
|
||
* 谁先放不下谁掉下去,于是调度中心「异常处理」三颗按钮(合计 480px)
|
||
* 在 378px 的侧栏卡里折成 2 + 1、素材墙一张卡底下 6 颗生成按钮 + 「删除」
|
||
* 折成 6 + 1、分镜导入流程「文本预览 → 导入预演 → 正式导入」在 768 折成三行。
|
||
* 读者看到的是「这一排怎么少了一颗」,而不是「这三条是一组」。
|
||
* 做法:见 `useAutoStack`——横排钉 max-content(整组一起挪行)、
|
||
* 一整行都放不下时整组竖排等宽。组内 `gap:marginXS`。
|
||
* 用法:`<Actions><ActionGroup>…</ActionGroup><BarRight>…</BarRight></Actions>`;
|
||
* 档位由外层容器下发,这里不再包一层 `Dense`。
|
||
*/
|
||
export function ActionGroup({ children, className, stack }: {
|
||
children: ReactNode; className?: string
|
||
/** 竖排等宽:容器明确窄到放不下横排时直接写死(窄侧栏卡片里的多颗长标题按钮) */
|
||
stack?: boolean
|
||
}) {
|
||
const { host, vertical, atom } = useAutoStack()
|
||
return stack || vertical
|
||
? <Flex ref={host} vertical gap={AG_GAP} className={cn('nex-action-group min-w-0 max-w-full', className)}>{children}</Flex>
|
||
: <Flex ref={host} align="center" wrap gap={AG_GAP}
|
||
className={cn('nex-action-group min-w-0 max-w-full', atom && 'nex-atom', className)}>{children}</Flex>
|
||
}
|
||
|
||
/** 需要右靠的一组控件(替代写在控件上的 margin-left:auto) */
|
||
export function BarRight({ children, className }: { children: ReactNode; className?: string }) {
|
||
return (
|
||
<Flex wrap align="center" gap={8} className={cn('ml-auto min-w-0', className)}>{children}</Flex>
|
||
)
|
||
}
|
||
|
||
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>
|
||
)
|
||
}
|
||
|
||
/** 必填星号:交还给 antd(Form.Item 的 required 自己画 `*`),这里只留旧名字的占位实现 */
|
||
export function Req() {
|
||
return <b className="nex-req">*</b>
|
||
}
|
||
|
||
/**
|
||
* 长字段(旧「标签在上」专用形态):现在和 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>
|
||
}
|
||
|
||
/* 每行 N 个字段时的降列节奏:档位名就是 antd Grid 的断点,站内不再有 900/1100 这种私有宽度。
|
||
⚠ 档位按**正文宽**取,不是按视口宽:侧栏占 224(≤md 折叠成 64)+ 内容内边距 32,
|
||
768 视口的正文只有 512。原先 5 等分在 md 档给到 3 列,实测每格 158——
|
||
「素材类型」的标签 69 之后只剩 69 给下拉,选中值「全部类型」77px 被省略号裁成 53px
|
||
(visualcheck 768 档 /assets 三条红灯)。整体降一档后 ≤1600 四列、≤1200 三列、
|
||
≤992 两列、≤576 单列,最窄的一格也有 231px。 */
|
||
const ROW_STEPS: Record<number, GridStep[]> = {
|
||
2: [{ at: 'sm', cols: 1 }],
|
||
5: [{ at: '2xl', cols: 4 }, { at: 'xl', cols: 3 }, { at: 'lg', cols: 2 }, { at: 'sm', cols: 1 }],
|
||
3: [{ at: 'md', cols: 2 }, { at: 'sm', cols: 1 }],
|
||
4: [{ at: 'lg', cols: 3 }, { at: 'md', cols: 2 }, { at: 'sm', cols: 1 }],
|
||
6: [{ at: 'xl', cols: 4 }, { at: 'lg', cols: 3 }, { at: 'md', cols: 2 }, { at: 'sm', cols: 1 }]
|
||
}
|
||
|
||
/**
|
||
* 表单里的一行 = 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>
|
||
)
|
||
}
|
||
|
||
/** 可点的筛选胶囊行(与下方内容强制呼吸 12px);只读回显一律走 TagGroup */
|
||
export function ChipBar({ children, label, className, top }: {
|
||
children: ReactNode; label?: ReactNode; className?: string; top?: boolean
|
||
}) {
|
||
return (
|
||
<Dense className="contents">
|
||
<Flex wrap align="center" gap={6} className={cn('nex-chipbar min-w-0', top && 'mt-3', className)}>
|
||
{label && <span className={GROUP_LABEL_CLS}>{label}</span>}
|
||
{children}
|
||
</Flex>
|
||
</Dense>
|
||
)
|
||
}
|
||
|
||
/**
|
||
* 筛选 / 开关胶囊 = antd `Tag.CheckableTag`(Ant Design 的「标签选择」形态)。
|
||
*
|
||
* 只有**可点的**东西属于这一族;只读回显(计数、能力、状态)用 Tag / TagGroup。
|
||
* 5.29 的 CheckableTag 没有 disabled,所以禁用态自己贴语义:降透明度 + 屏蔽 onChange。
|
||
*/
|
||
export function Chip({ active, children, onClick, className, title, disabled, as = 'button', href, size = 'md' }: {
|
||
active?: boolean; children: ReactNode; className?: string; title?: string
|
||
disabled?: boolean; as?: 'button' | 'a' | 'span'; href?: string; size?: 'md' | 'sm'
|
||
/**
|
||
* 点击回调。`as="a"`(锚点胶囊——手册目录就是这种)也要收得到事件,
|
||
* 否则页面没法 preventDefault,点一次目录就往浏览器历史里塞一条。
|
||
* 参数可选:按钮分支是无参调用 `onClick?.()`。
|
||
*/
|
||
onClick?: (e?: MouseEvent<HTMLElement>) => void
|
||
}) {
|
||
const cls = cn('nex-chip', size === 'sm' && 'nex-chip-sm', className)
|
||
if (as === 'a') {
|
||
return (
|
||
<Tag className={cn(cls, 'nex-chip-link')} bordered>
|
||
<Typography.Link href={href} title={title} onClick={onClick} className="!text-inherit">{children}</Typography.Link>
|
||
</Tag>
|
||
)
|
||
}
|
||
if (as === 'span' || disabled) {
|
||
return (
|
||
<Tag className={cn(cls, 'is-off')} bordered {...{ title }}
|
||
style={{ opacity: 0.45, cursor: 'not-allowed' }}>{children}</Tag>
|
||
)
|
||
}
|
||
return (
|
||
<Tag.CheckableTag checked={!!active} {...{ title }} className={cls}
|
||
onChange={() => onClick?.()}>
|
||
{children}
|
||
</Tag.CheckableTag>
|
||
)
|
||
}
|
||
|
||
/** 选中态胶囊 + 移除叉(多选回显);removeTitle 解释叉掉之后会发生什么 */
|
||
export function TagChip({ children, onRemove, className, removeTitle }: {
|
||
children: ReactNode; onRemove?: () => void; className?: string; removeTitle?: string
|
||
}) {
|
||
return (
|
||
<Tag className={cn('nex-tag-chip', className)} bordered closable={!!onRemove}
|
||
closeIcon={onRemove ? <IconClose /> : undefined}
|
||
onClose={(e) => { e.preventDefault(); onRemove?.() }}>
|
||
<span className="min-w-0 wrap-anywhere">{children}</span>
|
||
{removeTitle && <span className="sr-only">{removeTitle}</span>}
|
||
</Tag>
|
||
)
|
||
}
|
||
|
||
/**
|
||
* 分页条(规范 §5):page_size 唯一的家,筛选栏里不再出现「每页」。
|
||
* 档位由页面给(卡片墙要的是 24/60/120/全部;0 表示不分页)。
|
||
*/
|
||
const DEFAULT_SIZES = [12, 20, 24, 30, 48, 60, 120]
|
||
export function Pager({ page, pages, total, pageSize, onPageSize, onPage, extra, sizes = DEFAULT_SIZES }: {
|
||
page: number; pages: number; total?: number; pageSize?: number
|
||
onPageSize?: (size: number) => void; onPage: (page: number) => void; extra?: ReactNode
|
||
sizes?: number[]
|
||
}) {
|
||
const last = Math.max(1, pages)
|
||
const opts = sizes.map((n) => ({ value: n, label: String(n || '全部') }))
|
||
return (
|
||
<Dense>
|
||
<Flex wrap align="center" gap={12}
|
||
className={cn('nex-pager min-w-0', (total === undefined || total === 0) && !extra && 'hidden')}>
|
||
<Typography.Text type="secondary" className="!text-xs">{page} / {last}{total !== undefined ? ` · 共 ${total} 条` : ''}</Typography.Text>
|
||
<Pagination current={page} total={Math.max(last * (pageSize || 1), total || 0)} pageSize={pageSize || 12}
|
||
showSizeChanger={false} onChange={(p) => onPage(p)} size="small" simple />
|
||
{onPageSize && (
|
||
<Flex align="center" gap={6}>
|
||
<Typography.Text type="secondary" className="!text-xs">每页</Typography.Text>
|
||
<Select size="sm" ariaLabel="每页条数" searchThreshold={0} placeholder="全部"
|
||
value={pageSize} onChange={(v: any) => onPageSize(Number(v))}
|
||
className="w-[72px]" options={opts} />
|
||
</Flex>
|
||
)}
|
||
{extra}
|
||
</Flex>
|
||
</Dense>
|
||
)
|
||
}
|
||
|
||
/** 复选(勾选框 + 文案整块可点):本体是 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>
|
||
)
|
||
}
|