feat(design): add IMS Admin primitives (Button, StatusTag, Checkbox, FilterChip, Segmented, Avatar, Drawer) and restyle Field, Card, Modal
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
export function initials(name: string): string {
|
||||
return name
|
||||
.split(' ')
|
||||
.map(w => w[0] ?? '')
|
||||
.join('')
|
||||
.toUpperCase()
|
||||
.slice(0, 2)
|
||||
}
|
||||
|
||||
export function Avatar({ name }: { name: string }) {
|
||||
return (
|
||||
<span className="inline-grid place-items-center w-6 h-6 rounded-[6px] bg-accent-200 text-accent-700 text-[11px] font-[650]">
|
||||
{initials(name)}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
@@ -1,25 +1,36 @@
|
||||
import type { ButtonHTMLAttributes } from 'react'
|
||||
import { cx } from './cx'
|
||||
|
||||
type Variant = 'primary' | 'secondary' | 'ghost' | 'icon'
|
||||
type Variant = 'primary' | 'secondary' | 'icon'
|
||||
|
||||
const BASE =
|
||||
'inline-flex items-center gap-1.5 text-left font-extrabold text-sm leading-tight ' +
|
||||
'border border-transparent cursor-pointer transition-colors ' +
|
||||
'inline-flex items-center justify-center gap-1.5 whitespace-nowrap transition-colors cursor-pointer ' +
|
||||
'disabled:opacity-45 disabled:cursor-not-allowed'
|
||||
|
||||
const VARIANTS: Record<Variant, string> = {
|
||||
primary: 'justify-start bg-accent text-ground px-3.5 py-2 hover:bg-accent-600 active:bg-accent-700',
|
||||
secondary: 'justify-start border-divider text-ink px-3.5 py-2 hover:bg-ink/7 active:bg-ink/15',
|
||||
ghost: 'justify-start text-accent-700 px-1 py-2 hover:bg-accent/10 active:bg-accent/20',
|
||||
icon: 'justify-center w-9 h-9 p-0 text-ink hover:bg-ink/7 active:bg-ink/15',
|
||||
primary: 'h-8 px-3 rounded-md bg-ink-2 text-white text-[13px] font-semibold shadow-primary hover:bg-ink active:bg-black',
|
||||
secondary: 'h-8 px-3 rounded-md bg-surface text-ink text-[13px] font-medium shadow-secondary hover:bg-surface-2 active:bg-rule',
|
||||
icon: 'w-7 h-7 rounded-md text-ink-3 hover:bg-rule',
|
||||
}
|
||||
|
||||
type Props = ButtonHTMLAttributes<HTMLButtonElement> & {
|
||||
variant?: Variant
|
||||
block?: boolean
|
||||
mobile?: boolean
|
||||
}
|
||||
|
||||
export function Button({ variant = 'primary', block = false, className, type = 'button', ...rest }: Props) {
|
||||
return <button type={type} className={cx(BASE, VARIANTS[variant], block && 'w-full', className)} {...rest} />
|
||||
export function Button({ variant = 'primary', block = false, mobile = false, className, type = 'button', ...rest }: Props) {
|
||||
return (
|
||||
<button
|
||||
type={type}
|
||||
className={cx(
|
||||
BASE,
|
||||
VARIANTS[variant],
|
||||
mobile && variant !== 'icon' && 'h-12 rounded-xl text-sm',
|
||||
block && 'w-full',
|
||||
className,
|
||||
)}
|
||||
{...rest}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -12,11 +12,11 @@ export function Card({
|
||||
children?: ReactNode
|
||||
}) {
|
||||
return (
|
||||
<div className="flex flex-col gap-2 p-3 bg-surface">
|
||||
{kicker && <span className="text-[10px] tracking-[0.1em] uppercase text-accent-700">{kicker}</span>}
|
||||
{title && <h3 className="text-[17px] leading-tight font-extrabold">{title}</h3>}
|
||||
{children && <div className="flex-1 text-[13px] opacity-80">{children}</div>}
|
||||
{meta && <div className="flex items-center gap-1.5 text-[11px] text-ink/50">{meta}</div>}
|
||||
<div className="flex flex-col gap-2 p-4 bg-surface rounded-xl shadow-card">
|
||||
{kicker && <span className="text-[11px] font-semibold tracking-wide uppercase text-muted">{kicker}</span>}
|
||||
{title && <h3 className="text-base font-[650]">{title}</h3>}
|
||||
{children && <div className="flex-1 text-[13px] text-ink-3">{children}</div>}
|
||||
{meta && <div className="flex items-center gap-1.5 text-xs text-faint">{meta}</div>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import { Check, Minus } from 'lucide-react'
|
||||
import { cx } from './cx'
|
||||
|
||||
export function Checkbox({
|
||||
checked,
|
||||
indeterminate = false,
|
||||
onChange,
|
||||
label,
|
||||
}: {
|
||||
checked: boolean
|
||||
indeterminate?: boolean
|
||||
onChange: (next: boolean) => void
|
||||
label: string
|
||||
}) {
|
||||
const on = checked || indeterminate
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
role="checkbox"
|
||||
aria-checked={indeterminate ? 'mixed' : checked}
|
||||
aria-label={label}
|
||||
onClick={e => {
|
||||
e.stopPropagation()
|
||||
onChange(!checked)
|
||||
}}
|
||||
className={cx(
|
||||
'inline-grid place-items-center w-4 h-4 rounded-[5px] cursor-pointer',
|
||||
on ? 'bg-accent text-white' : 'bg-surface shadow-[inset_0_0_0_1.5px_var(--color-faint)]',
|
||||
)}
|
||||
>
|
||||
{indeterminate ? <Minus size={12} strokeWidth={3} aria-hidden /> : checked ? <Check size={12} strokeWidth={3} aria-hidden /> : null}
|
||||
</button>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
'use client'
|
||||
|
||||
import { useEffect } from 'react'
|
||||
import { X } from 'lucide-react'
|
||||
import { Button } from './button'
|
||||
|
||||
export function Drawer({
|
||||
open,
|
||||
onClose,
|
||||
title,
|
||||
actions,
|
||||
children,
|
||||
}: {
|
||||
open: boolean
|
||||
onClose: () => void
|
||||
title: string
|
||||
actions?: React.ReactNode
|
||||
children: React.ReactNode
|
||||
}) {
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose() }
|
||||
document.addEventListener('keydown', onKey)
|
||||
return () => document.removeEventListener('keydown', onKey)
|
||||
}, [open, onClose])
|
||||
|
||||
if (!open) return null
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 z-50">
|
||||
<div className="absolute inset-0 bg-black/30" onClick={onClose} aria-hidden />
|
||||
<aside
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-label={title}
|
||||
className="absolute top-2 right-2 bottom-2 w-[420px] max-w-[calc(100vw-16px)] flex flex-col overflow-hidden rounded-2xl bg-surface shadow-drawer"
|
||||
>
|
||||
<div className="flex items-center justify-between px-4 pt-3.5 pb-2.5">
|
||||
<h2 className="text-base font-[650]">{title}</h2>
|
||||
<div className="flex items-center gap-1">
|
||||
{actions}
|
||||
<Button variant="icon" aria-label="Close" onClick={onClose}>
|
||||
<X size={16} strokeWidth={2} aria-hidden />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex-1 overflow-y-auto">{children}</div>
|
||||
</aside>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -2,14 +2,13 @@ import type { InputHTMLAttributes, ReactNode, SelectHTMLAttributes, TextareaHTML
|
||||
import { cx } from './cx'
|
||||
|
||||
export const INPUT_CLASS =
|
||||
'w-full min-h-9 px-2.5 py-1.5 text-sm text-ink caret-accent bg-surface border border-divider ' +
|
||||
'hover:border-ink/45 focus-visible:border-accent focus-visible:outline-offset-0 ' +
|
||||
'disabled:opacity-45 disabled:cursor-not-allowed'
|
||||
'w-full h-9 px-3 rounded-md bg-surface text-[13px] text-ink caret-accent shadow-input ' +
|
||||
'placeholder:text-faint disabled:opacity-45 disabled:cursor-not-allowed'
|
||||
|
||||
export function Field({ label, htmlFor, children }: { label: string; htmlFor: string; children: ReactNode }) {
|
||||
return (
|
||||
<div>
|
||||
<label htmlFor={htmlFor} className="block text-xs mb-[5px] text-ink/70">
|
||||
<label htmlFor={htmlFor} className="block text-xs text-muted mb-[5px]">
|
||||
{label}
|
||||
</label>
|
||||
{children}
|
||||
@@ -22,7 +21,7 @@ export function Input({ className, ...rest }: InputHTMLAttributes<HTMLInputEleme
|
||||
}
|
||||
|
||||
export function Textarea({ className, ...rest }: TextareaHTMLAttributes<HTMLTextAreaElement>) {
|
||||
return <textarea className={cx(INPUT_CLASS, 'min-h-[90px] resize-y', className)} {...rest} />
|
||||
return <textarea className={cx(INPUT_CLASS, 'h-auto min-h-[90px] py-2 resize-y', className)} {...rest} />
|
||||
}
|
||||
|
||||
export function Select({ className, ...rest }: SelectHTMLAttributes<HTMLSelectElement>) {
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import { ChevronDown, X } from 'lucide-react'
|
||||
import { cx } from './cx'
|
||||
|
||||
export function FilterChip({
|
||||
label,
|
||||
value,
|
||||
options,
|
||||
onChange,
|
||||
}: {
|
||||
label: string
|
||||
value: string
|
||||
options: Array<{ value: string; label: string }>
|
||||
onChange: (value: string) => void
|
||||
}) {
|
||||
const selected = options.find(o => o.value === value)
|
||||
const set = Boolean(value && selected)
|
||||
return (
|
||||
<label
|
||||
className={cx(
|
||||
'relative inline-flex items-center gap-1 h-7 px-2.5 rounded-md text-[12.5px] whitespace-nowrap cursor-pointer',
|
||||
set ? 'bg-accent-tint-2 text-accent-700 shadow-chip-set' : 'bg-surface text-ink-2 shadow-chip',
|
||||
)}
|
||||
>
|
||||
<span>{set ? `${label}: ${selected!.label}` : label}</span>
|
||||
{!set && <ChevronDown size={12} strokeWidth={2.5} aria-hidden />}
|
||||
<select
|
||||
aria-label={label}
|
||||
value={value}
|
||||
onChange={e => onChange(e.target.value)}
|
||||
className="absolute inset-0 w-full h-full opacity-0 cursor-pointer"
|
||||
>
|
||||
<option value="">{label}</option>
|
||||
{options.map(o => (
|
||||
<option key={o.value} value={o.value}>{o.label}</option>
|
||||
))}
|
||||
</select>
|
||||
{set && (
|
||||
<button
|
||||
type="button"
|
||||
aria-label={`Clear ${label}`}
|
||||
onClick={() => onChange('')}
|
||||
className="relative z-10 inline-grid place-items-center w-4 h-4 cursor-pointer"
|
||||
>
|
||||
<X size={12} strokeWidth={2.5} aria-hidden />
|
||||
</button>
|
||||
)}
|
||||
</label>
|
||||
)
|
||||
}
|
||||
+10
-13
@@ -2,6 +2,7 @@
|
||||
|
||||
import { useEffect } from 'react'
|
||||
import { X } from 'lucide-react'
|
||||
import { Button } from './button'
|
||||
|
||||
interface ModalProps {
|
||||
open: boolean
|
||||
@@ -26,21 +27,17 @@ export function Modal({ open, onClose, title, children, footer, width = 'md' }:
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center p-4" role="dialog" aria-modal="true">
|
||||
<div className="absolute inset-0 bg-neutral-900/50" onClick={onClose} />
|
||||
<div className={`relative w-full ${maxW} bg-surface shadow-lg`}>
|
||||
<div className="flex items-center justify-between px-6 py-4 border-b border-divider">
|
||||
<h2 className="text-xl font-extrabold text-ink">{title}</h2>
|
||||
<button
|
||||
onClick={onClose}
|
||||
aria-label="Close"
|
||||
className="inline-flex w-9 h-9 items-center justify-center text-ink cursor-pointer transition-colors hover:bg-ink/7 active:bg-ink/15"
|
||||
>
|
||||
<X size={18} strokeWidth={2} aria-hidden />
|
||||
</button>
|
||||
<div className="absolute inset-0 bg-black/30" onClick={onClose} />
|
||||
<div className={`relative w-full ${maxW} bg-surface rounded-xl shadow-float`}>
|
||||
<div className="flex items-center justify-between px-5 py-3.5 border-b border-rule">
|
||||
<h2 className="text-base font-[650]">{title}</h2>
|
||||
<Button variant="icon" aria-label="Close" onClick={onClose}>
|
||||
<X size={16} strokeWidth={2} aria-hidden />
|
||||
</Button>
|
||||
</div>
|
||||
<div className="px-6 py-5">{children}</div>
|
||||
<div className="px-5 py-4">{children}</div>
|
||||
{footer && (
|
||||
<div className="px-6 py-4 border-t border-divider flex justify-end gap-2">
|
||||
<div className="px-5 py-3.5 border-t border-rule flex justify-end gap-2">
|
||||
{footer}
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import type { ReactNode } from 'react'
|
||||
import { cx } from './cx'
|
||||
|
||||
export function Segmented({
|
||||
value,
|
||||
onChange,
|
||||
options,
|
||||
}: {
|
||||
value: string
|
||||
onChange: (v: string) => void
|
||||
options: Array<{ value: string; label: ReactNode }>
|
||||
}) {
|
||||
return (
|
||||
<div className="inline-flex p-0.5 rounded-[10px] bg-rule-2">
|
||||
{options.map(o => (
|
||||
<button
|
||||
key={o.value}
|
||||
type="button"
|
||||
aria-pressed={o.value === value}
|
||||
onClick={() => onChange(o.value)}
|
||||
className={cx(
|
||||
'inline-flex items-center gap-1.5 px-2.5 py-[5px] rounded-md text-[13px] cursor-pointer whitespace-nowrap',
|
||||
o.value === value ? 'bg-surface font-semibold shadow-sm' : 'text-ink-3',
|
||||
)}
|
||||
>
|
||||
{o.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,72 +1,60 @@
|
||||
import type { ReactNode } from 'react'
|
||||
import {
|
||||
AlarmClock,
|
||||
BadgeCheck,
|
||||
Circle,
|
||||
CircleAlert,
|
||||
CircleCheck,
|
||||
ClipboardList,
|
||||
ListChecks,
|
||||
Loader,
|
||||
RotateCcw,
|
||||
Search,
|
||||
type LucideIcon,
|
||||
} from 'lucide-react'
|
||||
import { Tag, type TagVariant } from './tag'
|
||||
import { cx } from './cx'
|
||||
|
||||
const TONES: Record<string, TagVariant> = {
|
||||
reported: 'accent',
|
||||
triaged: 'outline',
|
||||
investigating: 'outline',
|
||||
capa_pending: 'outline',
|
||||
verification: 'outline',
|
||||
closed: 'neutral',
|
||||
open: 'accent',
|
||||
in_progress: 'outline',
|
||||
overdue: 'accent',
|
||||
pending_verification: 'outline',
|
||||
verified: 'neutral',
|
||||
reopened: 'accent',
|
||||
export type StatusHue = 'amber' | 'blue' | 'violet' | 'orange' | 'indigo' | 'green' | 'grey' | 'red'
|
||||
|
||||
const HUES: Record<string, StatusHue> = {
|
||||
reported: 'amber', triaged: 'blue', investigating: 'violet', capa_pending: 'orange',
|
||||
verification: 'indigo', closed: 'green',
|
||||
open: 'grey', in_progress: 'blue', overdue: 'red', pending_verification: 'indigo',
|
||||
verified: 'green', reopened: 'red',
|
||||
critical: 'red', high: 'orange', med: 'amber', medium: 'amber', low: 'grey', active: 'green',
|
||||
}
|
||||
|
||||
const ICONS: Record<string, LucideIcon> = {
|
||||
reported: CircleAlert,
|
||||
triaged: ListChecks,
|
||||
investigating: Search,
|
||||
capa_pending: ClipboardList,
|
||||
verification: BadgeCheck,
|
||||
closed: CircleCheck,
|
||||
open: Circle,
|
||||
in_progress: Loader,
|
||||
overdue: AlarmClock,
|
||||
pending_verification: BadgeCheck,
|
||||
verified: CircleCheck,
|
||||
reopened: RotateCcw,
|
||||
const LABELS: Record<string, string> = {
|
||||
capa_pending: 'CAPA pending', pending_verification: 'Verification', in_progress: 'In progress', med: 'Medium',
|
||||
}
|
||||
|
||||
export function statusTone(status: string): TagVariant {
|
||||
return TONES[status] ?? 'neutral'
|
||||
// Full literal class strings so Tailwind can see them.
|
||||
const HUE_CLASS: Record<StatusHue, string> = {
|
||||
amber: 'bg-st-amber-bg text-st-amber-fg',
|
||||
blue: 'bg-st-blue-bg text-st-blue-fg',
|
||||
violet: 'bg-st-violet-bg text-st-violet-fg',
|
||||
orange: 'bg-st-orange-bg text-st-orange-fg',
|
||||
indigo: 'bg-st-indigo-bg text-st-indigo-fg',
|
||||
green: 'bg-st-green-bg text-st-green-fg',
|
||||
grey: 'bg-st-grey-bg text-st-grey-fg',
|
||||
red: 'bg-st-red-bg text-st-red-fg',
|
||||
}
|
||||
|
||||
function humanize(value: string): string {
|
||||
return value
|
||||
.split('_')
|
||||
.map(w => (w === 'capa' ? 'CAPA' : w.charAt(0).toUpperCase() + w.slice(1)))
|
||||
.join(' ')
|
||||
export function statusHue(key: string): StatusHue {
|
||||
return HUES[key] ?? 'grey'
|
||||
}
|
||||
|
||||
export function StatusTag({ status, children }: { status: string; children?: ReactNode }) {
|
||||
const Icon = ICONS[status]
|
||||
function label(key: string): string {
|
||||
if (LABELS[key]) return LABELS[key]
|
||||
const words = key.split('_').join(' ')
|
||||
return words.charAt(0).toUpperCase() + words.slice(1)
|
||||
}
|
||||
|
||||
function Pill({ hue, className, children }: { hue: StatusHue; className?: string; children: ReactNode }) {
|
||||
return (
|
||||
<Tag variant={statusTone(status)}>
|
||||
{Icon && <Icon size={12} strokeWidth={2} aria-hidden />}
|
||||
{children ?? humanize(status)}
|
||||
</Tag>
|
||||
<span
|
||||
className={cx(
|
||||
'inline-flex items-center px-2 py-0.5 rounded-md text-xs font-[550] whitespace-nowrap',
|
||||
HUE_CLASS[hue],
|
||||
className,
|
||||
)}
|
||||
>
|
||||
{children}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
const PRIORITY_TONES: Record<string, TagVariant> = { high: 'accent', med: 'outline', low: 'neutral' }
|
||||
export function StatusTag({ status, children }: { status: string; children?: ReactNode }) {
|
||||
return <Pill hue={statusHue(status)}>{children ?? label(status)}</Pill>
|
||||
}
|
||||
|
||||
export function PriorityTag({ priority, children }: { priority: string; children?: ReactNode }) {
|
||||
return <Tag variant={PRIORITY_TONES[priority] ?? 'neutral'}>{children ?? humanize(priority)}</Tag>
|
||||
return <Pill hue={statusHue(priority)}>{children ?? label(priority)}</Pill>
|
||||
}
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
export const TABLE = 'w-full border-collapse text-sm'
|
||||
export const TH =
|
||||
'text-left text-[11px] uppercase tracking-[0.08em] text-ink/60 px-2 py-2 border-b-2 border-divider'
|
||||
export const TR = 'hover:bg-ink/4'
|
||||
export const TD = 'px-2 py-2 border-b border-divider'
|
||||
@@ -1,24 +0,0 @@
|
||||
import type { ReactNode } from 'react'
|
||||
import { cx } from './cx'
|
||||
|
||||
export type TagVariant = 'accent' | 'neutral' | 'outline'
|
||||
|
||||
const VARIANTS: Record<TagVariant, string> = {
|
||||
accent: 'bg-accent-100 text-accent-800',
|
||||
neutral: 'bg-neutral-100 text-neutral-800',
|
||||
outline: 'border border-accent text-accent-700',
|
||||
}
|
||||
|
||||
export function Tag({ variant, children, className }: { variant: TagVariant; children: ReactNode; className?: string }) {
|
||||
return (
|
||||
<span
|
||||
className={cx(
|
||||
'inline-flex items-center gap-1 text-[11px] font-medium tracking-wide px-2.5 py-[3px] whitespace-nowrap',
|
||||
VARIANTS[variant],
|
||||
className,
|
||||
)}
|
||||
>
|
||||
{children}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
@@ -667,8 +667,8 @@ describe('FilterChip', () => {
|
||||
const options = [{ value: 'open', label: 'Open' }, { value: 'closed', label: 'Closed' }]
|
||||
|
||||
it('unset shows the label and a select with an empty option', () => {
|
||||
render(<FilterChip label="Status" value="" options={options} onChange={() => {}} />)
|
||||
expect(screen.getByText('Status')).toBeTruthy()
|
||||
const { container } = render(<FilterChip label="Status" value="" options={options} onChange={() => {}} />)
|
||||
expect(container.querySelector('label > span')?.textContent).toBe('Status')
|
||||
expect((screen.getByLabelText('Status') as HTMLSelectElement).value).toBe('')
|
||||
})
|
||||
|
||||
|
||||
@@ -2,49 +2,27 @@ import { describe, it, expect, vi } from 'vitest'
|
||||
import { render, screen, fireEvent } from '@testing-library/react'
|
||||
import { Modal } from '@/components/ui/modal'
|
||||
|
||||
describe('Modal (Modernist dialog)', () => {
|
||||
describe('Modal', () => {
|
||||
it('renders nothing when closed', () => {
|
||||
const { container } = render(
|
||||
<Modal open={false} onClose={() => {}} title="T">
|
||||
body
|
||||
</Modal>,
|
||||
)
|
||||
const { container } = render(<Modal open={false} onClose={() => {}} title="T">body</Modal>)
|
||||
expect(container.firstChild).toBeNull()
|
||||
})
|
||||
|
||||
it('renders a dialog with title, body and footer on the surface fill', () => {
|
||||
render(
|
||||
<Modal open onClose={() => {}} title="Confirm" footer={<span>actions</span>}>
|
||||
body text
|
||||
</Modal>,
|
||||
)
|
||||
it('renders a 12px card dialog with the floating shadow and a 1px header rule', () => {
|
||||
render(<Modal open onClose={() => {}} title="Confirm" footer={<span>actions</span>}>body text</Modal>)
|
||||
const dialog = screen.getByRole('dialog')
|
||||
expect(dialog).toBeTruthy()
|
||||
expect(screen.getByText('Confirm')).toBeTruthy()
|
||||
expect(screen.getByText('body text')).toBeTruthy()
|
||||
expect(screen.getByText('actions')).toBeTruthy()
|
||||
expect(dialog.innerHTML).toContain('bg-surface')
|
||||
expect(dialog.innerHTML).toContain('shadow-lg')
|
||||
})
|
||||
|
||||
it('has no rounded corners and uses a 2px title rule', () => {
|
||||
render(
|
||||
<Modal open onClose={() => {}} title="T">
|
||||
x
|
||||
</Modal>,
|
||||
)
|
||||
const html = screen.getByRole('dialog').innerHTML
|
||||
expect(html).not.toMatch(/rounded/)
|
||||
expect(html).toContain('border-b')
|
||||
expect(dialog.innerHTML).toContain('rounded-xl')
|
||||
expect(dialog.innerHTML).toContain('shadow-float')
|
||||
expect(dialog.innerHTML).toContain('border-b border-rule')
|
||||
expect(dialog.innerHTML).not.toContain('border-b-2')
|
||||
})
|
||||
|
||||
it('closes from the X button and on Escape', () => {
|
||||
const onClose = vi.fn()
|
||||
render(
|
||||
<Modal open onClose={onClose} title="T">
|
||||
x
|
||||
</Modal>,
|
||||
)
|
||||
render(<Modal open onClose={onClose} title="T">x</Modal>)
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Close' }))
|
||||
fireEvent.keyDown(document, { key: 'Escape' })
|
||||
expect(onClose).toHaveBeenCalledTimes(2)
|
||||
|
||||
@@ -1,132 +1,176 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { render, screen } from '@testing-library/react'
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { render, screen, fireEvent } from '@testing-library/react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Tag } from '@/components/ui/tag'
|
||||
import { StatusTag, PriorityTag, statusTone } from '@/components/ui/status-tag'
|
||||
import { StatusTag, PriorityTag, statusHue } from '@/components/ui/status-tag'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import { FilterChip } from '@/components/ui/filter-chip'
|
||||
import { Segmented } from '@/components/ui/segmented'
|
||||
import { Avatar } from '@/components/ui/avatar'
|
||||
import { Drawer } from '@/components/ui/drawer'
|
||||
import { Field, Input } from '@/components/ui/field'
|
||||
import { Card } from '@/components/ui/card'
|
||||
import { TABLE, TH, TR, TD } from '@/components/ui/table-styles'
|
||||
|
||||
describe('Button', () => {
|
||||
it('renders a primary accent button that is type=button and flush left', () => {
|
||||
render(<Button variant="primary">Save</Button>)
|
||||
const btn = screen.getByRole('button', { name: 'Save' })
|
||||
expect(btn.getAttribute('type')).toBe('button')
|
||||
expect(btn.className).toContain('bg-accent')
|
||||
expect(btn.className).toContain('justify-start')
|
||||
expect(btn.className).not.toContain('justify-center')
|
||||
it('primary is the dark 32px button, type=button by default', () => {
|
||||
render(<Button>Report incident</Button>)
|
||||
const b = screen.getByRole('button', { name: 'Report incident' })
|
||||
expect(b.getAttribute('type')).toBe('button')
|
||||
expect(b.className).toContain('bg-ink-2')
|
||||
expect(b.className).toContain('h-8')
|
||||
expect(b.className).toContain('rounded-md')
|
||||
expect(b.className).toContain('shadow-primary')
|
||||
})
|
||||
|
||||
it('block buttons span the row and stay flush left', () => {
|
||||
render(<Button block>Go</Button>)
|
||||
const btn = screen.getByRole('button', { name: 'Go' })
|
||||
expect(btn.className).toContain('w-full')
|
||||
expect(btn.className).toContain('justify-start')
|
||||
it('secondary is white with a ring', () => {
|
||||
render(<Button variant="secondary">Export</Button>)
|
||||
const b = screen.getByRole('button', { name: 'Export' })
|
||||
expect(b.className).toContain('bg-surface')
|
||||
expect(b.className).toContain('shadow-secondary')
|
||||
})
|
||||
|
||||
it('icon buttons are square and centered', () => {
|
||||
render(<Button variant="icon" aria-label="Close" />)
|
||||
const btn = screen.getByRole('button', { name: 'Close' })
|
||||
expect(btn.className).toContain('w-9')
|
||||
expect(btn.className).toContain('justify-center')
|
||||
expect(btn.className).not.toContain('justify-start')
|
||||
it('icon is a 28px ghost button', () => {
|
||||
render(<Button variant="icon" aria-label="More" />)
|
||||
const b = screen.getByRole('button', { name: 'More' })
|
||||
expect(b.className).toContain('w-7')
|
||||
expect(b.className).toContain('h-7')
|
||||
})
|
||||
|
||||
it('never uses a rounded class', () => {
|
||||
render(<Button variant="secondary">x</Button>)
|
||||
expect(screen.getByRole('button').className).not.toMatch(/rounded/)
|
||||
it('mobile and block make a full-width 48px button', () => {
|
||||
render(<Button block mobile>Submit</Button>)
|
||||
const b = screen.getByRole('button', { name: 'Submit' })
|
||||
expect(b.className).toContain('w-full')
|
||||
expect(b.className).toContain('h-12')
|
||||
expect(b.className).toContain('rounded-xl')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Tag', () => {
|
||||
describe('StatusTag and PriorityTag', () => {
|
||||
it.each([
|
||||
['accent', 'bg-accent-100'],
|
||||
['neutral', 'bg-neutral-100'],
|
||||
['outline', 'border-accent'],
|
||||
] as const)('%s variant carries %s', (variant, cls) => {
|
||||
render(<Tag variant={variant}>Label</Tag>)
|
||||
expect(screen.getByText('Label').className).toContain(cls)
|
||||
['reported', 'amber'], ['triaged', 'blue'], ['investigating', 'violet'], ['capa_pending', 'orange'],
|
||||
['verification', 'indigo'], ['closed', 'green'], ['open', 'grey'], ['in_progress', 'blue'],
|
||||
['overdue', 'red'], ['pending_verification', 'indigo'], ['verified', 'green'], ['reopened', 'red'],
|
||||
['high', 'orange'], ['med', 'amber'], ['low', 'grey'], ['nonsense', 'grey'],
|
||||
])('%s uses the %s pair', (key, hue) => {
|
||||
expect(statusHue(key)).toBe(hue)
|
||||
})
|
||||
})
|
||||
|
||||
describe('statusTone', () => {
|
||||
it.each([
|
||||
['reported', 'accent'],
|
||||
['triaged', 'outline'],
|
||||
['investigating', 'outline'],
|
||||
['capa_pending', 'outline'],
|
||||
['verification', 'outline'],
|
||||
['closed', 'neutral'],
|
||||
['open', 'accent'],
|
||||
['in_progress', 'outline'],
|
||||
['overdue', 'accent'],
|
||||
['pending_verification', 'outline'],
|
||||
['verified', 'neutral'],
|
||||
['reopened', 'accent'],
|
||||
['something_else', 'neutral'],
|
||||
])('%s maps to %s', (status, tone) => {
|
||||
expect(statusTone(status)).toBe(tone)
|
||||
})
|
||||
})
|
||||
|
||||
describe('StatusTag', () => {
|
||||
it('shows a humanized text label plus an icon, never color alone', () => {
|
||||
const { container } = render(<StatusTag status="capa_pending" />)
|
||||
expect(screen.getByText('CAPA Pending')).toBeTruthy()
|
||||
expect(container.querySelector('svg')).not.toBeNull()
|
||||
it('renders a dark-on-tint pill with a text label', () => {
|
||||
render(<StatusTag status="capa_pending" />)
|
||||
const el = screen.getByText('CAPA pending')
|
||||
expect(el.className).toContain('bg-st-orange-bg')
|
||||
expect(el.className).toContain('text-st-orange-fg')
|
||||
expect(el.className).toContain('rounded-md')
|
||||
})
|
||||
|
||||
it('accepts a label override', () => {
|
||||
render(<StatusTag status="overdue">Late</StatusTag>)
|
||||
expect(screen.getByText('Late')).toBeTruthy()
|
||||
expect(screen.getByText('Late').className).toContain('bg-st-red-bg')
|
||||
})
|
||||
|
||||
it('priority med reads Medium on the amber pair', () => {
|
||||
render(<PriorityTag priority="med" />)
|
||||
expect(screen.getByText('Medium').className).toContain('bg-st-amber-bg')
|
||||
})
|
||||
|
||||
it('never uses white text on a saturated fill', () => {
|
||||
const { container } = render(<StatusTag status="closed" />)
|
||||
expect(container.innerHTML).not.toContain('text-white')
|
||||
})
|
||||
})
|
||||
|
||||
describe('PriorityTag', () => {
|
||||
it.each([
|
||||
['high', 'bg-accent-100'],
|
||||
['med', 'border-accent'],
|
||||
['low', 'bg-neutral-100'],
|
||||
])('%s priority uses %s', (priority, cls) => {
|
||||
const { container } = render(<PriorityTag priority={priority} />)
|
||||
expect(container.firstElementChild?.className).toContain(cls)
|
||||
describe('Checkbox', () => {
|
||||
it('toggles and reports the next value', () => {
|
||||
const onChange = vi.fn()
|
||||
render(<Checkbox checked={false} onChange={onChange} label="Select row" />)
|
||||
const cb = screen.getByRole('checkbox', { name: 'Select row' })
|
||||
expect(cb.getAttribute('aria-checked')).toBe('false')
|
||||
fireEvent.click(cb)
|
||||
expect(onChange).toHaveBeenCalledWith(true)
|
||||
})
|
||||
|
||||
it('shows checked and indeterminate states', () => {
|
||||
const { rerender } = render(<Checkbox checked onChange={() => {}} label="a" />)
|
||||
expect(screen.getByRole('checkbox').getAttribute('aria-checked')).toBe('true')
|
||||
expect(screen.getByRole('checkbox').className).toContain('bg-accent')
|
||||
rerender(<Checkbox checked={false} indeterminate onChange={() => {}} label="a" />)
|
||||
expect(screen.getByRole('checkbox').getAttribute('aria-checked')).toBe('mixed')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Field and Input', () => {
|
||||
it('associates the label with the input', () => {
|
||||
render(
|
||||
<Field label="Email" htmlFor="email">
|
||||
<Input id="email" />
|
||||
</Field>,
|
||||
)
|
||||
describe('FilterChip', () => {
|
||||
const options = [{ value: 'open', label: 'Open' }, { value: 'closed', label: 'Closed' }]
|
||||
|
||||
it('unset shows the label and a select with an empty option', () => {
|
||||
const { container } = render(<FilterChip label="Status" value="" options={options} onChange={() => {}} />)
|
||||
expect(container.querySelector('label > span')?.textContent).toBe('Status')
|
||||
expect((screen.getByLabelText('Status') as HTMLSelectElement).value).toBe('')
|
||||
})
|
||||
|
||||
it('changes through the select', () => {
|
||||
const onChange = vi.fn()
|
||||
render(<FilterChip label="Status" value="" options={options} onChange={onChange} />)
|
||||
fireEvent.change(screen.getByLabelText('Status'), { target: { value: 'closed' } })
|
||||
expect(onChange).toHaveBeenCalledWith('closed')
|
||||
})
|
||||
|
||||
it('set shows Label: value in the teal tint with a clear button', () => {
|
||||
const onChange = vi.fn()
|
||||
render(<FilterChip label="Status" value="closed" options={options} onChange={onChange} />)
|
||||
expect(screen.getByText('Status: Closed').closest('label')?.className).toContain('bg-accent-tint-2')
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Clear Status' }))
|
||||
expect(onChange).toHaveBeenCalledWith('')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Segmented', () => {
|
||||
it('marks the active option and reports changes', () => {
|
||||
const onChange = vi.fn()
|
||||
render(<Segmented value="list" onChange={onChange} options={[{ value: 'list', label: 'List' }, { value: 'board', label: 'Board' }]} />)
|
||||
expect(screen.getByRole('button', { name: 'List' }).getAttribute('aria-pressed')).toBe('true')
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Board' }))
|
||||
expect(onChange).toHaveBeenCalledWith('board')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Avatar', () => {
|
||||
it('shows up to two initials on the teal tile', () => {
|
||||
render(<Avatar name="Ada Lovelace King" />)
|
||||
const el = screen.getByText('AL')
|
||||
expect(el.className).toContain('bg-accent-200')
|
||||
expect(el.className).toContain('rounded-[6px]')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Drawer', () => {
|
||||
it('renders nothing when closed', () => {
|
||||
const { container } = render(<Drawer open={false} onClose={() => {}} title="Notifications">x</Drawer>)
|
||||
expect(container.firstChild).toBeNull()
|
||||
})
|
||||
|
||||
it('renders a labelled dialog, closes on the X button and on Escape', () => {
|
||||
const onClose = vi.fn()
|
||||
render(<Drawer open onClose={onClose} title="Notifications" actions={<span>Mark all read</span>}>body</Drawer>)
|
||||
expect(screen.getByRole('dialog', { name: 'Notifications' })).toBeTruthy()
|
||||
expect(screen.getByText('Mark all read')).toBeTruthy()
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Close' }))
|
||||
fireEvent.keyDown(document, { key: 'Escape' })
|
||||
expect(onClose).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Field, Input, Card', () => {
|
||||
it('Input is 36px with the faint ring and is labelled', () => {
|
||||
render(<Field label="Email" htmlFor="email"><Input id="email" /></Field>)
|
||||
const input = screen.getByLabelText('Email')
|
||||
expect(input.className).toContain('bg-surface')
|
||||
expect(input.className).toContain('border-divider')
|
||||
expect(input.className).toContain('h-9')
|
||||
expect(input.className).toContain('shadow-input')
|
||||
expect(input.className).toContain('rounded-md')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Card', () => {
|
||||
it('renders kicker, title, body and meta on the surface fill', () => {
|
||||
const { container } = render(
|
||||
<Card kicker="Open" title="Forklift" meta="Due Friday">
|
||||
Body text
|
||||
</Card>,
|
||||
)
|
||||
expect(screen.getByText('Open')).toBeTruthy()
|
||||
it('Card is a white 12px card with the card shadow', () => {
|
||||
const { container } = render(<Card title="Forklift" kicker="Open" meta="Due Friday">Body</Card>)
|
||||
expect(container.firstElementChild?.className).toContain('rounded-xl')
|
||||
expect(container.firstElementChild?.className).toContain('shadow-card')
|
||||
expect(screen.getByText('Forklift')).toBeTruthy()
|
||||
expect(screen.getByText('Body text')).toBeTruthy()
|
||||
expect(screen.getByText('Due Friday')).toBeTruthy()
|
||||
expect(container.firstElementChild?.className).toContain('bg-surface')
|
||||
})
|
||||
})
|
||||
|
||||
describe('table styles', () => {
|
||||
it('uses a 2px header rule and 1px row rules', () => {
|
||||
expect(TH).toContain('border-b-2')
|
||||
expect(TH).toContain('border-divider')
|
||||
expect(TD).toContain('border-b')
|
||||
expect(TR).toContain('hover:bg-ink/4')
|
||||
expect(TABLE).toContain('w-full')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user