import type { ReactNode } from 'react' import { cn } from '../../lib/cn' /** * 控件栏体系(docs/UI规范.md §1~§5 的唯一实现)。 * * 症状:筛选栏 / 工具条 / 抽屉字段里「标签在上、控件在下」,一个字段吃掉整行; * 同一行搜索框 38、下拉 31、复选框 16,中心线各走各的;chip 行贴住下方面板。 * 做法:布局只在这几个组件里定义,页面只给内容—— * 含控件的横条(一行、垂直居中、紧凑高度、下边距 12) * 行内标签字段(标签在左、控件在右;grow / wide / narrow 调宽) * 需要右靠的一组控件(替代写在控件上的 margin-left:auto) * chip 行(与下方内容强制呼吸 12px) * 短字段栅格(auto-fit 自己降列,页面不写 @media) * 分页条(page_size 唯一的家) * 高度一律不写在控件上:容器把 --ctl-h 压到紧凑档,行内控件跟着一起降。 */ const LABEL = 'flex flex-row items-center gap-1.5 m-0 text-muted text-tiny tracking-[.3px] whitespace-nowrap' /** 控件吃满标签右侧宽度;标签文字本身不拉伸(否则被挤成两行) */ const LABEL_CHILD = '[&>label]:flex [&>label]:flex-row [&>label]:items-center [&>label]:gap-1.5 [&>label]:m-0 ' + '[&>label]:text-muted [&>label]:text-tiny [&>label]:tracking-[.3px] [&>label]:whitespace-nowrap' const FIELD_INNER = '[&>*]:min-w-0 [&>*]:flex-1 [&_span:first-child]:flex-none [&_input[type=checkbox]]:flex-none [&_input[type=checkbox]]:w-auto [&_input[type=radio]]:flex-none' export function Ctlbar({ children, className, as: As = 'div' }: { children: ReactNode; className?: string; as?: any }) { return ( *]:min-w-0', LABEL_CHILD, className)}> {children} ) } /** 卡片内部的动作行:布局同 Ctlbar,但不带 12px 下边距(否则卡片里凭空多一段空白) */ export function CardActions({ children, className }: { children: ReactNode; className?: string }) { return (
*]:min-w-0', className)}> {children}
) } export function BarRight({ children, className }: { children: ReactNode; className?: string }) { return
{children}
} type FieldWidth = 'default' | 'grow' | 'narrow' | 'wide' | 'full' const WIDTHS: Record = { default: 'basis-[170px] shrink grow-0', grow: 'basis-[240px] grow shrink', narrow: 'basis-[122px] shrink grow-0', /** .wide = 「宽字段」而不是「吃满整行」:1360px 抽屉里的下拉会撑到整行 89% */ wide: 'basis-[330px] grow shrink max-w-[min(460px,58%)]', full: 'basis-full grow shrink' } /** 行内标签字段:`…`;stack 场景交给 统一翻向 */ export function Field({ label, required, children, width = 'default', className, hint }: { label?: ReactNode; required?: boolean; children: ReactNode; width?: FieldWidth; className?: string; hint?: ReactNode }) { return ( ) } /** 必填星号:和标签文字同一个 flex item,否则列方向 label 会把「*」顶到下一行 */ export function Req() { return * } /** 长字段(标签在上):抽屉/对话框里的多行文本、上传区用这个 */ export function FieldBlock({ label, required, children, className }: { label?: ReactNode; required?: boolean; children: ReactNode; className?: string }) { return ( ) } /** 短字段栅格:列数由容器决定,页面不写 @media;stack 把标签退回控件上方 */ export function FormRow({ children, cols, tight, stack, className }: { children: ReactNode cols?: 2 | 3 | 4 | 6 tight?: boolean stack?: boolean className?: string }) { const tracks = cols === undefined ? 'repeat(auto-fit,minmax(190px,1fr))' : cols === 6 ? 'repeat(6,minmax(78px,1fr))' : `repeat(${cols},minmax(0,1fr))` return (
*]:min-w-0', cols === undefined && 'max-mid:grid-cols-2', (cols === 3 || cols === 4) && 'max-mid:grid-cols-2', (cols === 4 || cols === 6) && 'max-nav:grid-cols-2', stack && 'gap-y-[10px] [&>*]:flex-col [&>*]:items-stretch [&>*]:gap-[5px] [&>*]:flex-none [&>*]:w-auto [&>*:not(span)]:w-full', className)} style={{ gridTemplateColumns: tracks, gap: tight ? 8 : undefined }}> {children}
) } /** chip / 标签行(规范 §2):与下方内容强制呼吸 12px */ export function ChipBar({ children, label, className, top }: { children: ReactNode; label?: ReactNode; className?: string; top?: boolean }) { return (
{label && {label}} {children}
) } /** 筛选/开关胶囊(.chip) */ export function Chip({ active, children, onClick, className, title, disabled, as = 'button', href }: { active?: boolean; children: ReactNode; onClick?: () => void; className?: string; title?: string disabled?: boolean; as?: 'button' | 'a'; href?: string }) { const classes = cn('inline-flex items-center justify-center gap-[5px] h-(--ctl-h-md) px-2 text-tiny rounded-pill ' + 'border whitespace-nowrap shrink-0 no-underline transition-colors duration-150', active ? 'bg-accent-fill border-accent-fill text-on-accent font-bold' : 'bg-raise border-line-strong text-ink-3 hover:bg-active hover:text-ink', disabled && 'opacity-40 cursor-not-allowed', className) if (as === 'a') return {children} return } /** 选中态胶囊 + 移除叉(多选回显) */ export function TagChip({ children, onRemove, className }: { children: ReactNode; onRemove?: () => void; className?: string }) { return ( {children} {onRemove && ( )} ) } /** 分页条(规范 §5):page_size 唯一的家,筛选栏里不再出现「每页」 */ export function Pager({ page, pages, total, pageSize, onPageSize, onPage, extra }: { page: number; pages: number; total?: number; pageSize?: number onPageSize?: (size: number) => void; onPage: (page: number) => void; extra?: ReactNode }) { const last = Math.max(1, pages) return (
{page} / {last}{total !== undefined ? ` · 共 ${total} 条` : ''} {onPageSize && ( )} {extra}
) } /** 复选(.check):勾选框 + 文案,整块可点 */ export function Check({ checked, onChange, children, disabled, className }: { checked: boolean; onChange: (next: boolean) => void; children: ReactNode; disabled?: boolean; className?: string }) { return ( ) }