diff --git a/ui/README.md b/ui/README.md index a57aec3..0763614 100644 --- a/ui/README.md +++ b/ui/README.md @@ -41,6 +41,8 @@ Tell Tailwind v4 to read the classes the components use, next to the tokens impo | `Avatar` | A person's initials in a quiet disc, next to their name (`initials(name)` is exported too) | | `SelectionBar` | Floating actions on a list selection; announces the count, Escape clears | | `SegmentedControl` | 2–5 exclusive choices with a sliding indicator and arrow-key navigation; `null` = no choice | +| `FactList` | Facts as label / value pairs in a `
`; empty values left out, or `emptyLabel` for them | +| `DateChip` | A day as a small tile, month over day, beside a row that says the full date | | `FormSection` | One part of a long form: title, kicker and actions left, fields right (`col-span-full`) | | `SaveBar` | A form's save actions at the bottom, with the unsaved-changes dot and an announced label | | `Panel` | A bordered block of a page: title and actions, rows edge to edge or `padded` fields, footer | diff --git a/ui/package.json b/ui/package.json index 0ab11e9..7ecfb04 100644 --- a/ui/package.json +++ b/ui/package.json @@ -1,6 +1,6 @@ { "name": "@unitae/ui", - "version": "0.6.0", + "version": "0.7.0", "description": "Unitae UI components: stat tiles, chart kit (with table twins), status, avatars, selection bar, segmented control, form sections, save bar, panels, section headings and the brand mark. Built on @unitae/tokens.", "license": "MIT", "type": "module", diff --git a/ui/src/date-chip.tsx b/ui/src/date-chip.tsx new file mode 100644 index 0000000..60f6636 --- /dev/null +++ b/ui/src/date-chip.tsx @@ -0,0 +1,25 @@ +import { cn } from './utils.ts' + +export interface DateChipProps { + /** The month, already formatted in the reader's language (« mars », « nov. »). Shown in capitals. */ + month: string + /** The day of the month. */ + day: string + className?: string +} + +/** A day as a small tile, the month over the day. Decorative: the row beside it says the full date. */ +export function DateChip({ month, day, className }: DateChipProps) { + return ( + + ) +} diff --git a/ui/src/fact-list.tsx b/ui/src/fact-list.tsx new file mode 100644 index 0000000..b73328f --- /dev/null +++ b/ui/src/fact-list.tsx @@ -0,0 +1,46 @@ +import { Fragment, type ReactNode } from 'react' +import { cn } from './utils.ts' + +export interface Fact { + label: ReactNode + /** Left out, or replaced by the list's `emptyLabel`, when it is null, undefined or an empty string. */ + value: ReactNode + /** A quiet line under the value: an age, a relative date, who leads the group. */ + hint?: ReactNode + /** A stable key, when the label is not a unique string. */ + key?: string +} + +export interface FactListProps { + facts: Fact[] + /** The words for a missing value, e.g. « Non renseigné ». Without them, a fact without a value is left out. */ + emptyLabel?: ReactNode + className?: string +} + +function isEmpty(value: ReactNode) { + return value == null || value === '' || value === false +} + +/** + * Facts about one thing as label / value pairs: a person's contact details, a group's leaders. A description list, + * labels in a column of their own width, values taking the rest and wrapping anywhere (addresses, e-mails). + */ +export function FactList({ facts, emptyLabel, className }: FactListProps) { + const shown = emptyLabel == null ? facts.filter((fact) => !isEmpty(fact.value)) : facts + if (shown.length === 0) return null + + return ( +
+ {shown.map((fact, index) => ( + +
{fact.label}
+
+ {isEmpty(fact.value) ? {emptyLabel} : fact.value} + {fact.hint != null && {fact.hint}} +
+
+ ))} +
+ ) +} diff --git a/ui/src/index.ts b/ui/src/index.ts index 780501b..a388075 100644 --- a/ui/src/index.ts +++ b/ui/src/index.ts @@ -26,6 +26,8 @@ export { type LineChartProps, type LineSeries, } from './charts.tsx' +export { DateChip, type DateChipProps } from './date-chip.tsx' +export { type Fact, FactList, type FactListProps } from './fact-list.tsx' export { FormSection, type FormSectionProps } from './form-section.tsx' export { Lockup, type LockupProps, Mark, type MarkProps } from './mark.tsx' export { Panel, type PanelProps } from './panel.tsx' diff --git a/ui/test/components.test.tsx b/ui/test/components.test.tsx index 68200d1..fda5194 100644 --- a/ui/test/components.test.tsx +++ b/ui/test/components.test.tsx @@ -8,6 +8,8 @@ import { BarList, ChartCard, ColumnChart, + DateChip, + FactList, FormSection, LineChart, Mark, @@ -568,3 +570,53 @@ describe('stacked columns and dashed lines', () => { expect(screen.getByRole('img', { name: "Cumul de l'année" })).toBeTruthy() }) }) + +describe('FactList', () => { + it('pairs each label with its value in a description list, the hint under the value', () => { + render( + , + ) + const terms = screen.getAllByRole('term').map((term) => term.textContent) + const definitions = screen.getAllByRole('definition').map((definition) => definition.textContent) + expect(terms).toEqual(['Téléphone', 'Naissance']) + expect(definitions).toEqual(['06 12 34 56 78', '14 mai 198145 ans']) + expect(screen.getByText('45 ans')).toBeTruthy() + }) + + it('leaves out a fact without a value', () => { + render( + , + ) + expect(screen.getAllByRole('term').map((term) => term.textContent)).toEqual(['E-mail']) + }) + + it('says a value is missing when it is given the words for it', () => { + render() + expect(screen.getByRole('definition').textContent).toBe('Non renseigné') + }) + + it('draws nothing when no fact is left', () => { + const { container } = render() + expect(container.querySelector('dl')).toBeNull() + }) +}) + +describe('DateChip', () => { + it('stacks the month over the day and stays decorative, the row saying the full date', () => { + const { container } = render() + const chip = container.firstElementChild as HTMLElement + expect(chip.getAttribute('aria-hidden')).toBe('true') + expect(chip.textContent).toBe('mars19') + }) +})