189 lines
9.5 KiB
TypeScript
189 lines
9.5 KiB
TypeScript
import type { ReactNode } from 'react'
|
||
import { cn } from '../../lib/cn'
|
||
|
||
/**
|
||
* 控件栏体系(docs/UI规范.md §1~§5 的唯一实现)。
|
||
*
|
||
* 症状:筛选栏 / 工具条 / 抽屉字段里「标签在上、控件在下」,一个字段吃掉整行;
|
||
* 同一行搜索框 38、下拉 31、复选框 16,中心线各走各的;chip 行贴住下方面板。
|
||
* 做法:布局只在这几个组件里定义,页面只给内容——
|
||
* <Ctlbar> 含控件的横条(一行、垂直居中、紧凑高度、下边距 12)
|
||
* <Field> 行内标签字段(标签在左、控件在右;grow / wide / narrow 调宽)
|
||
* <BarRight> 需要右靠的一组控件(替代写在控件上的 margin-left:auto)
|
||
* <ChipBar> chip 行(与下方内容强制呼吸 12px)
|
||
* <FormRow> 短字段栅格(auto-fit 自己降列,页面不写 @media)
|
||
* <Pager> 分页条(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 (
|
||
<As className={cn('[--ctl-h:var(--ctl-h-md)] flex items-center flex-wrap gap-y-2 gap-x-[10px] min-w-0 mb-3',
|
||
'[&>*]:min-w-0', LABEL_CHILD, className)}>
|
||
{children}
|
||
</As>
|
||
)
|
||
}
|
||
|
||
/** 卡片内部的动作行:布局同 Ctlbar,但不带 12px 下边距(否则卡片里凭空多一段空白) */
|
||
export function CardActions({ children, className }: { children: ReactNode; className?: string }) {
|
||
return (
|
||
<div className={cn('[--ctl-h:var(--ctl-h-md)] flex items-center flex-wrap gap-y-2 gap-x-[10px] min-w-0 [&>*]:min-w-0', className)}>
|
||
{children}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
export function BarRight({ children, className }: { children: ReactNode; className?: string }) {
|
||
return <div className={cn('ml-auto flex items-center gap-2 flex-wrap min-w-0', className)}>{children}</div>
|
||
}
|
||
|
||
type FieldWidth = 'default' | 'grow' | 'narrow' | 'wide' | 'full'
|
||
const WIDTHS: Record<FieldWidth, string> = {
|
||
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'
|
||
}
|
||
|
||
/** 行内标签字段:`<Field label="类型">…</Field>`;stack 场景交给 <FormRow stack> 统一翻向 */
|
||
export function Field({ label, required, children, width = 'default', className, hint }: {
|
||
label?: ReactNode; required?: boolean; children: ReactNode; width?: FieldWidth; className?: string; hint?: ReactNode
|
||
}) {
|
||
return (
|
||
<label className={cn(LABEL, FIELD_INNER, WIDTHS[width], 'min-w-0', className)}>
|
||
{label && <span className="flex-none">{label}{required && <Req />}</span>}
|
||
{children}
|
||
{hint}
|
||
</label>
|
||
)
|
||
}
|
||
|
||
/** 必填星号:和标签文字同一个 flex item,否则列方向 label 会把「*」顶到下一行 */
|
||
export function Req() {
|
||
return <b className="text-danger font-bold ml-[2px]">*</b>
|
||
}
|
||
|
||
/** 长字段(标签在上):抽屉/对话框里的多行文本、上传区用这个 */
|
||
export function FieldBlock({ label, required, children, className }: {
|
||
label?: ReactNode; required?: boolean; children: ReactNode; className?: string
|
||
}) {
|
||
return (
|
||
<label className={cn('flex flex-col gap-[5px] mb-[10px] min-w-0', className)}>
|
||
<span className="flex items-center gap-1 text-muted text-tiny tracking-[.3px] flex-wrap whitespace-nowrap">
|
||
{label}{required && <Req />}
|
||
</span>
|
||
{children}
|
||
</label>
|
||
)
|
||
}
|
||
|
||
/** 短字段栅格:列数由容器决定,页面不写 @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 (
|
||
<div className={cn('grid mb-[10px] min-w-0 [&>*]: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}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** chip / 标签行(规范 §2):与下方内容强制呼吸 12px */
|
||
export function ChipBar({ children, label, className, top }: { children: ReactNode; label?: ReactNode; className?: string; top?: boolean }) {
|
||
return (
|
||
<div className={cn('[--ctl-h:var(--ctl-h-md)] flex items-center gap-1.5 flex-wrap mb-3 min-w-0', top && 'mt-3', className)}>
|
||
{label && <span className="flex-none text-muted text-tiny tracking-[.6px] font-bold">{label}</span>}
|
||
{children}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** 筛选/开关胶囊(.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 <a href={href} title={title} className={classes}>{children}</a>
|
||
return <button type="button" title={title} disabled={disabled} onClick={onClick} className={classes}>{children}</button>
|
||
}
|
||
|
||
/** 选中态胶囊 + 移除叉(多选回显) */
|
||
export function TagChip({ children, onRemove, className }: { children: ReactNode; onRemove?: () => void; className?: string }) {
|
||
return (
|
||
<span className={cn('inline-flex items-center justify-center gap-[5px] h-(--ctl-h-md) px-[9px] text-tiny rounded-pill '
|
||
+ 'border bg-accent-fill border-accent-fill text-on-accent font-bold whitespace-nowrap shrink-0', className)}>
|
||
{children}
|
||
{onRemove && (
|
||
<button type="button" onClick={onRemove} aria-label="移除"
|
||
className="grid place-items-center w-[17px] h-[17px] ml-[5px] p-0 flex-none border-0 rounded-full bg-[color-mix(in_srgb,var(--c-on-accent)_20%,transparent)] text-inherit text-[12px] leading-none hover:bg-danger hover:text-danger-ink">×</button>
|
||
)}
|
||
</span>
|
||
)
|
||
}
|
||
|
||
/** 分页条(规范 §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 (
|
||
<div className="[--ctl-h:var(--ctl-h-md)] flex items-center justify-center gap-[10px] flex-wrap mt-3 text-dim text-tiny">
|
||
<button type="button" className="h-(--ctl-h-md) px-2 rounded-control border border-line-strong bg-raise text-ink-3 text-tiny disabled:opacity-35"
|
||
disabled={page <= 1} onClick={() => onPage(page - 1)}>上一页</button>
|
||
<span>{page} / {last}{total !== undefined ? ` · 共 ${total} 条` : ''}</span>
|
||
<button type="button" className="h-(--ctl-h-md) px-2 rounded-control border border-line-strong bg-raise text-ink-3 text-tiny disabled:opacity-35"
|
||
disabled={page >= last} onClick={() => onPage(page + 1)}>下一页</button>
|
||
{onPageSize && (
|
||
<label className="flex items-center gap-1.5 m-0 text-muted text-tiny whitespace-nowrap">
|
||
<span className="flex-none">每页</span>
|
||
<select value={pageSize} onChange={(e) => onPageSize(Number(e.target.value))} className="w-auto min-w-[64px]">
|
||
{[12, 20, 24, 30, 48, 60, 120].map((n) => <option key={n} value={n}>{n}</option>)}
|
||
</select>
|
||
</label>
|
||
)}
|
||
{extra}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** 复选(.check):勾选框 + 文案,整块可点 */
|
||
export function Check({ checked, onChange, children, disabled, className }: {
|
||
checked: boolean; onChange: (next: boolean) => void; children: ReactNode; disabled?: boolean; className?: string
|
||
}) {
|
||
return (
|
||
<label className={cn('inline-flex flex-row items-center gap-[7px] text-caption text-ink-3 cursor-pointer flex-none whitespace-nowrap', className)}>
|
||
<input type="checkbox" className="w-auto" checked={checked} disabled={disabled} onChange={(e) => onChange(e.target.checked)} />
|
||
{children}
|
||
</label>
|
||
)
|
||
}
|