diff --git a/apps/frontend/src/components/Button/Button.spec.tsx b/apps/frontend/src/components/Button/Button.spec.tsx new file mode 100644 index 000000000..e89f44cbc --- /dev/null +++ b/apps/frontend/src/components/Button/Button.spec.tsx @@ -0,0 +1,145 @@ +import { fireEvent, screen } from '@testing-library/react'; +import { describe, expect, it, vi } from 'vitest'; +import { renderWithProviders } from '../../test/render'; +import { Button } from './Button'; +import { BUTTON_VARIANTS, type ButtonVariant } from './variants'; + +const VARIANTS = Object.keys(BUTTON_VARIANTS) as ButtonVariant[]; + +describe('Button', () => { + it.each(VARIANTS)('renders the label for variant "%s"', (variant) => { + renderWithProviders(); + expect(screen.getByRole('button', { name: 'Label' })).toBeTruthy(); + }); + + it('defaults to the primary variant and md size', () => { + renderWithProviders(); + const button = screen.getByRole('button', { name: 'Upload' }); + expect(button.getAttribute('data-variant')).toBe('primary'); + expect(button.getAttribute('data-size')).toBe('md'); + }); + + it('calls onClick when clicked', () => { + const onClick = vi.fn(); + renderWithProviders(); + fireEvent.click(screen.getByRole('button', { name: 'Go' })); + expect(onClick).toHaveBeenCalledTimes(1); + }); + + it('does not call onClick when disabled', () => { + const onClick = vi.fn(); + renderWithProviders( + , + ); + const button = screen.getByRole('button', { name: 'Go' }); + expect(button).toHaveProperty('disabled', true); + fireEvent.click(button); + expect(onClick).not.toHaveBeenCalled(); + }); + + it('is disabled, busy and ignores clicks while loading', () => { + const onClick = vi.fn(); + renderWithProviders( + , + ); + const button = screen.getByRole('button'); + expect(button).toHaveProperty('disabled', true); + expect(button.getAttribute('aria-busy')).toBe('true'); + fireEvent.click(button); + expect(onClick).not.toHaveBeenCalled(); + }); + + it('shows loadingText while loading', () => { + renderWithProviders( + , + ); + expect(screen.getByText('Submitting')).toBeTruthy(); + }); + + it('is not busy when not loading', () => { + renderWithProviders(); + expect(screen.getByRole('button').getAttribute('aria-busy')).not.toBe( + 'true', + ); + }); + + it('renders leftIcon and rightIcon, hidden from assistive tech', () => { + renderWithProviders( + , + ); + const left = screen.getByTestId('left'); + const right = screen.getByTestId('right'); + expect(left.closest('[aria-hidden="true"]')).not.toBeNull(); + expect(right.closest('[aria-hidden="true"]')).not.toBeNull(); + // The icons don't change the accessible name. + expect(screen.getByRole('button', { name: 'Source: All' })).toBeTruthy(); + }); + + it('is reachable by aria-label when icon-only', () => { + renderWithProviders( + ); + expect(screen.getByRole('button', { name: 'A' }).getAttribute('type')).toBe( + 'button', + ); + unmount(); + renderWithProviders(); + expect(screen.getByRole('button', { name: 'B' }).getAttribute('type')).toBe( + 'submit', + ); + }); + + it('renders a real link when href is given', () => { + renderWithProviders( + , + ); + const link = screen.getByRole('link', { name: 'Today’s Entry' }); + expect(link.tagName).toBe('A'); + expect(link.getAttribute('href')).toBe('/entry/today'); + expect(screen.queryByRole('button')).toBeNull(); + }); + + it('adds rel for links that open in a new tab', () => { + renderWithProviders( + , + ); + expect(screen.getByRole('link', { name: 'Docs' }).getAttribute('rel')).toBe( + 'noopener noreferrer', + ); + }); + + it('works as the child of asChild with a custom link', () => { + renderWithProviders( + , + ); + const link = screen.getByRole('link', { name: 'Custom' }); + expect(link.getAttribute('href')).toBe('/x'); + expect(link.hasAttribute('type')).toBe(false); + }); +}); diff --git a/apps/frontend/src/components/Button/Button.tsx b/apps/frontend/src/components/Button/Button.tsx new file mode 100644 index 000000000..18d23d913 --- /dev/null +++ b/apps/frontend/src/components/Button/Button.tsx @@ -0,0 +1,142 @@ +import { + Button as ChakraButton, + type ButtonProps as ChakraButtonProps, +} from '@chakra-ui/react'; +import type { ReactNode } from 'react'; +import { + BUTTON_SIZES, + BUTTON_VARIANTS, + type ButtonSize, + type ButtonVariant, +} from './variants'; + +type BaseProps = Omit< + ChakraButtonProps, + 'variant' | 'size' | 'loading' | 'loadingText' | 'children' +> & { + /** Look of the button. Label and icons are passed by the caller. */ + variant?: ButtonVariant; + /** `md` is Figma's 40px. `sm` is a compact option (not in Figma). */ + size?: ButtonSize; + leftIcon?: ReactNode; + rightIcon?: ReactNode; + /** Shows a spinner, disables the button and keeps its width. */ + loading?: boolean; + loadingText?: ReactNode; + /** Renders a real `` styled as the button (use for navigation). */ + href?: string; + target?: string; + rel?: string; +}; + +// With no children the button is icon-only and needs an accessible name. +export type ButtonProps = BaseProps & + ({ children: ReactNode } | { children?: undefined; 'aria-label': string }); + +const Icon = ({ children }: { children: ReactNode }) => ( + +); + +/** + * Shared button. Built on Chakra's `Button` and styled with the tokens in + * `styles/tokens.css` to match the Figma "Buttons" frame (156:1559). + * + * - `variant` is the look (`primary`, `secondary`, `dark`, `link`, `ghost`). + * The caller passes the label and icons, so there is no variant per label. + * - `size` is `md` (Figma's 40px) or `sm` (compact, not in Figma). + * - `loading` shows a spinner, disables the button and sets `aria-busy`; + * `loadingText` replaces the label while loading. + * - `href` renders a real ``. `asChild` wraps your own element (e.g. a + * router Link) and leaves its content and `type` alone. + * - `type` defaults to `"button"` so a button in a form never submits by + * accident. Pass `type="submit"` for Submit. + * - With no children it is icon-only and `aria-label` is required. + * + * Other Chakra props (`onClick`, `disabled`, `aria-*`, ...) pass through. + * + * @example + * + * + * + ))} + + ))} + + ))} + + + + ))} + link variant as a real anchor +
+ +
+ +); + +/** Both Filter types (date and uploader) with mock options, the selected + * value printed below each, plus the disabled and `sm` states. */ +export const FiltersDemo: React.FC = () => { + const [date, setDate] = useState(DEFAULT_DATE); + const [uploader, setUploader] = useState(DEFAULT_UPLOADER); + return ( +
+
+ } + width="442px" + /> + selected: {date} +
+
+ + selected: {uploader} +
+
+ + disabled +
+
+ + size sm +
+
+ ); +}; + +/** Filters composed into a bar: labels, an entry count + * driven by the mock data, and Clear Filters when nothing matches. */ +export const FilterBarDemo: React.FC = () => { + const [date, setDate] = useState(DEFAULT_DATE); + const [uploader, setUploader] = useState(DEFAULT_UPLOADER); + const count = filterEntries(MOCK_ENTRIES, date, uploader).length; + const reset = () => { + setDate(DEFAULT_DATE); + setUploader(DEFAULT_UPLOADER); + }; + return ( + <> +
+ } + width="442px" + /> + + + {count} {count === 1 ? 'entry' : 'entries'} + +
+ {count === 0 ? ( +
+ + No entries match these filters. +
+ ) : null} + + Tip: pick “Sep 1 - Sep 30, 2026” to see 0 entries and Clear Filters. + + + ); +}; diff --git a/apps/frontend/src/containers/design.tsx b/apps/frontend/src/containers/design.tsx index 166b27e33..92cedaf62 100644 --- a/apps/frontend/src/containers/design.tsx +++ b/apps/frontend/src/containers/design.tsx @@ -9,6 +9,7 @@ import { type ErrorLogSource, } from '@components/ErrorLogTable'; import { SubmittedEntriesTable } from '@components/SubmittedEntriesTable'; +import { ButtonsDemo, FiltersDemo, FilterBarDemo } from './button-filter-demo'; const SHADES = [100, 200, 300, 400, 500, 600, 700, 800]; const PALETTE: [string, number[]][] = [ @@ -231,6 +232,21 @@ const Design: React.FC = () => (

Tables

+ +
+

Buttons

+ +
+ +
+

Filters

+ +
+ +
+

Filter bar

+ +
); diff --git a/apps/frontend/src/containers/mockFilters.ts b/apps/frontend/src/containers/mockFilters.ts new file mode 100644 index 000000000..ff381a9e3 --- /dev/null +++ b/apps/frontend/src/containers/mockFilters.ts @@ -0,0 +1,69 @@ +import type { FilterOption } from '@components/Filter'; + +// Mock data for the /dev/buttons preview. Dates are fixed so the page is the +// same every day. + +export interface MockEntry { + id: number; + date: string; // ISO yyyy-mm-dd + uploader: 'staff' | 'admin'; +} + +export const MOCK_ENTRIES: MockEntry[] = [ + { id: 1, date: '2026-10-02', uploader: 'staff' }, + { id: 2, date: '2026-10-05', uploader: 'admin' }, + { id: 3, date: '2026-10-12', uploader: 'staff' }, + { id: 4, date: '2026-10-19', uploader: 'staff' }, + { id: 5, date: '2026-10-23', uploader: 'admin' }, + { id: 6, date: '2026-10-27', uploader: 'staff' }, + { id: 7, date: '2026-10-30', uploader: 'admin' }, + { id: 8, date: '2026-11-01', uploader: 'staff' }, +]; + +/** Preset date ranges. A real range picker can replace this later: keep the + * `{ value, label }` shape so `Filter` doesn't change. [from, to], inclusive. */ +export const DATE_PRESETS: (FilterOption & { range: [string, string] })[] = [ + { + value: 'oct-nov', + label: 'Oct 1 - Nov 1, 2026', + range: ['2026-10-01', '2026-11-01'], + }, + { + value: 'last-7', + label: 'Last 7 days', + range: ['2026-10-26', '2026-11-01'], + }, + { + value: 'last-30', + label: 'Last 30 days', + range: ['2026-10-03', '2026-11-01'], + }, + { + value: 'sep', + label: 'Sep 1 - Sep 30, 2026', + range: ['2026-09-01', '2026-09-30'], + }, +]; + +export const UPLOADER_OPTIONS: FilterOption[] = [ + { value: 'all', label: 'All uploaders' }, + { value: 'staff', label: 'Staff' }, + { value: 'admin', label: 'Admin' }, +]; + +export const DEFAULT_DATE = 'oct-nov'; +export const DEFAULT_UPLOADER = 'all'; + +export function filterEntries( + entries: MockEntry[], + dateValue: string, + uploader: string, +): MockEntry[] { + const preset = DATE_PRESETS.find((p) => p.value === dateValue); + return entries.filter((e) => { + const inRange = preset + ? e.date >= preset.range[0] && e.date <= preset.range[1] + : true; + return inRange && (uploader === 'all' || e.uploader === uploader); + }); +}