395 lines
20 KiB
TypeScript
395 lines
20 KiB
TypeScript
/**
|
||
* 控件栏体系(阶段 6 自 `Controls.tsx` 拆出,实现原样搬移):
|
||
* 「一行控件怎么排、动作怎么分组、筛选怎么放」的容器与胶囊——
|
||
* Dense / Ctlbar / OptionGrid / CardActions / Actions / ActionGroup / BarRight
|
||
* / ChipBar / Chip / TagChip / Pager。
|
||
* 字段本身(Field / FieldBlock / FormGrid / FormRow / Check / RadioGroup / FilePick)在 `Fields.tsx`。
|
||
* 出口不变:`components/ui/index.ts` 把两个文件一起再导出,业务页 0 改动。
|
||
*/
|
||
import { cloneElement, isValidElement, useLayoutEffect, useRef, useState, type MouseEvent, type ReactElement, type ReactNode } from 'react'
|
||
import { ConfigProvider, Flex, Pagination, Row, Tag, Typography } from 'antd'
|
||
import { cn } from '../../lib/cn'
|
||
import { IconClose } from '../icons'
|
||
import { Select } from '../forms/Select'
|
||
import { ROW_STEPS, colSpans, toCols, FILL_AT } 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>
|
||
)
|
||
}
|
||
|
||
|
||
/** 可点的筛选胶囊行(与下方内容强制呼吸 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 表示不分页)。
|
||
*/
|
||
/**
|
||
* 分页首末键的可读名称(2026-11-08 · S-2)。
|
||
*
|
||
* antd 的上一页/下一页是一颗只装了箭头的 <button>,箭头自己带 `aria-label=left|right`,
|
||
* 读屏念出来就是英文字形名「left」「right」——用户不知道那是翻页。
|
||
* `itemRender` 是 antd 给的正门:拿它把原元素 cloneElement 一下、补一句 aria-label,
|
||
* DOM 结构、类名、皮肤、禁用逻辑一律不动(rc-pagination 拿到返回值之后照样
|
||
* cloneElement({disabled}),翻页到头的禁用还是它自己算)。
|
||
* simple 档没有 jump-prev / jump-next(那是完整分页的「•••」),所以只登记这两类。
|
||
*/
|
||
const pagerLabel = (el: React.ReactNode, type: 'page' | 'prev' | 'next' | 'jump-prev' | 'jump-next') => {
|
||
if (!((type === 'prev' || type === 'next') && isValidElement(el))) return el
|
||
return cloneElement(el as ReactElement<{ 'aria-label'?: string }>,
|
||
{ 'aria-label': type === 'prev' ? '上一页' : '下一页' })
|
||
}
|
||
|
||
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
|
||
itemRender={(p, type, el) => pagerLabel(el, type)} />
|
||
{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>
|
||
)
|
||
}
|