medkit/src/kit/content-tabs.tsx

111 lines
4.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import * as React from "react"
import { type LucideIcon } from "lucide-react"
import { cn } from "../lib/utils"
import { StatusDot, type StatusTone } from "./status-badge"
export interface ContentTabItem<T extends string = string> {
readonly id: T
readonly label: string
readonly icon?: LucideIcon
/** Счётчик справа от подписи; вкладка с нулём приглушена. */
readonly count?: number
/** Цветная точка статуса перед подписью — вкладки-фильтры по статусам. */
readonly tone?: StatusTone
readonly disabled?: boolean
}
/**
* Контентные вкладки внутри страницы/карточки.
* `line` — подчёркивание (разделы карточки), `pill` — заливка (переключатели в блоках).
* Вкладки-фильтры по статусам: `{ id, label, count, tone }`, первая — «Все» без тона.
* `actions` — кнопки справа в той же строке («Наклейки», «Excel»): у вкладок и кнопок одна
* нижняя линия, подчёркивание лежит прямо на ней. Не собирать такую строку вручную
* из ContentTabs с `border-b-0` — подчёркивание повиснет над линией.
*/
export function ContentTabs<T extends string>({
tabs,
value,
onValueChange,
variant = "line",
actions,
className,
}: {
tabs: readonly ContentTabItem<T>[]
value: T
onValueChange: (value: T) => void
variant?: "line" | "pill"
/** Кнопки справа в строке вкладок. */
actions?: React.ReactNode
className?: string
}) {
const list = (
<div
role="tablist"
className={cn(
// overflow-y-hidden: иначе overflow-x-auto делает строку прокручиваемой и по вертикали
"scrollbar-none flex shrink-0 items-center gap-1 overflow-x-auto overflow-y-hidden",
variant === "line" && "px-3",
variant === "line" && !actions && "border-b",
variant === "line" && actions && "min-w-0 flex-1 self-end",
variant === "pill" && "w-fit max-w-full rounded-xl bg-muted/70 p-1",
!actions && className,
)}
>
{tabs.map((tab) => {
const active = tab.id === value
return (
<button
key={tab.id}
type="button"
role="tab"
aria-selected={active}
disabled={tab.disabled}
onClick={() => onValueChange(tab.id)}
className={cn(
"relative flex shrink-0 items-center gap-2 text-sm font-medium whitespace-nowrap transition-colors outline-none focus-visible:ring-2 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-40",
variant === "line" && "h-10 px-3",
variant === "line" && (active ? "text-foreground" : "text-muted-foreground hover:text-foreground"),
variant === "pill" && "h-8 rounded-lg px-3",
variant === "pill" &&
(active ? "bg-card text-foreground shadow-xs" : "text-muted-foreground hover:text-foreground"),
tab.count === 0 && !active && "opacity-60",
)}
>
{tab.icon && <tab.icon className={cn("size-4", active && "text-primary")} />}
{tab.tone && <StatusDot tone={tab.tone} />}
{tab.label}
{tab.count !== undefined && (
<span
className={cn(
"rounded-md px-1.5 text-2xs leading-5 font-semibold tabular",
active ? "bg-primary/10 text-primary" : "bg-muted text-muted-foreground",
)}
>
{tab.count}
</span>
)}
{variant === "line" && active && (
<span className="absolute inset-x-2 bottom-0 h-0.5 rounded-full bg-primary" />
)}
</button>
)
})}
</div>
)
if (!actions) return list
return (
<div
className={cn(
"flex shrink-0 items-end gap-x-2",
variant === "line" && "border-b",
className,
)}
>
{list}
<div className="flex shrink-0 items-center gap-2 self-center py-1.5 pr-4">{actions}</div>
</div>
)
}