import type { Meta, StoryObj } from '@storybook/react-webpack5'; import { expect } from 'storybook/test'; import { useRtlDiffI18n, withRTLDiff } from '../../../.storybook/decorators/RtlDiffDecorator'; import { Layout } from '../../common'; import DefinitionList, { type DefinitionListDefinition, type DefinitionListLayout } from '..'; const longValues = { amount: '1,234,567.89 GBP from your Wise Business account ending in 4821', arrival: 'Tuesday, 14 July 2026 at 18:30 Eastern European Summer Time', method: 'Business account ending in 4821 · Manual bank transfer', recipients: 'Thirty-two recipients across twelve destination currencies', }; const longTitles = { amount: "Total amount we'll convert before sending this batch payment", arrival: 'Estimated arrival time for all recipients in this batch', method: 'Payment method used to fund this batch transfer', recipients: 'Number of recipients included in this batch payment', }; const longTitlesLongValues: DefinitionListDefinition[] = [ { key: 'amount', title: longTitles.amount, value: longValues.amount }, { key: 'arrival', title: longTitles.arrival, value: longValues.arrival }, { key: 'method', title: longTitles.method, value: longValues.method }, { key: 'recipients', title: longTitles.recipients, value: longValues.recipients }, ]; const longTitlesShortValues: DefinitionListDefinition[] = [ { key: 'amount', title: longTitles.amount, value: '1,234.56 GBP' }, { key: 'arrival', title: longTitles.arrival, value: 'Today' }, { key: 'method', title: longTitles.method, value: 'Bank transfer' }, { key: 'recipients', title: longTitles.recipients, value: '32' }, ]; const shortTitlesLongValues: DefinitionListDefinition[] = [ { key: 'amount', title: 'Amount', value: longValues.amount }, { key: 'arrival', title: 'Arrival', value: longValues.arrival }, { key: 'method', title: 'Method', value: longValues.method }, { key: 'recipients', title: 'Recipients', value: longValues.recipients }, ]; const mixedContentLengths: DefinitionListDefinition[] = [ { key: 'ticket-regression', title: "Total amount we'll convert", value: '1,234.56 GBP' }, { key: 'short-short', title: 'Fees', value: '12.34 GBP' }, { key: 'long-short', title: longTitles.arrival, value: 'Today' }, { key: 'short-long', title: 'Method', value: longValues.method }, { key: 'long-long', title: longTitles.recipients, value: longValues.recipients }, { key: 'styled-content', title: Current available balance before this payment, value: 98,765.43 GBP, }, ]; type TestCaseProps = { definitions: DefinitionListDefinition[]; label: string; width?: number; }; const TestCase = ({ definitions, label, width = 640 }: TestCaseProps) => (

{label}

); const meta = { component: DefinitionList, parameters: { controls: { disable: true } }, tags: ['!autodocs', '!manifest'], title: 'Typography/DefinitionList/Tests', } satisfies Meta; export default meta; type Story = StoryObj; const assertSharedColumnBoundary = async (canvasElement: HTMLElement) => { const lists = [ ...canvasElement.querySelectorAll('.tw-definition-list--horizontal-right-aligned'), ]; await expect(lists.length).toBeGreaterThan(0); for (const list of lists) { const rows = [...list.querySelectorAll('.tw-definition-list__item')]; const columns = rows.map((row) => ({ row, title: row.querySelector('dt'), value: row.querySelector('dd'), })); await expect(columns.every(({ title, value }) => title && value)).toBe(true); const horizontalColumns = columns.filter(({ row }) => ['flex', 'grid'].includes(getComputedStyle(row).display), ); if (horizontalColumns.length > 0) { const titleRects = horizontalColumns.map(({ title }) => title?.getBoundingClientRect()); const valueRects = horizontalColumns.map(({ value }) => value?.getBoundingClientRect()); const isRtl = getComputedStyle(list).direction === 'rtl'; if (isRtl) { const valueColumnEnd = Math.max(...valueRects.map((rect) => rect?.right ?? 0)); const titleColumnStart = Math.min( ...titleRects.map((rect) => rect?.left ?? Number.POSITIVE_INFINITY), ); await expect(valueColumnEnd).toBeLessThanOrEqual(titleColumnStart + 1); await expect(titleColumnStart - valueColumnEnd).toBeGreaterThanOrEqual(51); } else { const titleColumnEnd = Math.max(...titleRects.map((rect) => rect?.right ?? 0)); const valueColumnStart = Math.min( ...valueRects.map((rect) => rect?.left ?? Number.POSITIVE_INFINITY), ); await expect(titleColumnEnd).toBeLessThanOrEqual(valueColumnStart + 1); await expect(valueColumnStart - titleColumnEnd).toBeGreaterThanOrEqual(51); } } else { for (const { title, value } of columns) { if (title && value) { const titleEnd = title.getBoundingClientRect().bottom; const valueStart = value.getBoundingClientRect().top; await expect(titleEnd).toBeLessThanOrEqual(valueStart + 1); } } } for (const { title, value } of columns) { if (title && value) { await expect(title.scrollWidth).toBeLessThanOrEqual(title.clientWidth); await expect(value.scrollWidth).toBeLessThanOrEqual(value.clientWidth); } } } }; const verifySharedColumnBoundary: NonNullable = async ({ canvasElement, step }) => { await step('titles and values stay inside separate shared columns', async () => { await assertSharedColumnBoundary(canvasElement); }); }; export const HORIZONTAL_RIGHT_ALIGNED: Story = { name: 'HORIZONTAL_RIGHT_ALIGNED', render: () => (
), play: verifySharedColumnBoundary, }; const layouts: DefinitionListLayout[] = [ 'VERTICAL_TWO_COLUMN', 'VERTICAL_ONE_COLUMN', 'HORIZONTAL_JUSTIFIED', 'HORIZONTAL_LEFT_ALIGNED', 'HORIZONTAL_RIGHT_ALIGNED', ]; export const RTLDiff: Story = { render: () => { const i18n = useRtlDiffI18n(); const rtlDefinitions: DefinitionListDefinition[] = [ { key: 'name', title: i18n.fullName, value: i18n.customerName }, { key: 'amount', title: i18n.amountSent, value: i18n.amountValue }, { key: 'status', title: i18n.transferStatus, value: i18n.statusValue }, ]; const rtlDefinitionsWithAction: DefinitionListDefinition[] = [ { key: 'name', title: i18n.fullName, value: i18n.customerName }, { key: 'amount', title: i18n.amountSent, value: i18n.amountValue, action: { label: i18n.editAction }, }, { key: 'status', title: i18n.transferStatus, value: i18n.statusValue, action: { label: i18n.viewDetailsAction }, }, ]; return (
{layouts.map((layout) => (

{layout}

))} {layouts.map((layout) => (

{layout} {i18n.withActionButtons}

))}
); }, ...withRTLDiff({ fixedRows: { heights: 'auto' }, i18n: { fullName: { en: 'Full name', ar: 'الاسم الكامل' }, customerName: { en: 'Alice Smith', ar: 'أليس سميث' }, amountSent: { en: 'Amount sent', ar: 'المبلغ المُرسل' }, amountValue: { en: '1,234.56 GBP', ar: '1,234.56 جنيه إسترليني' }, transferStatus: { en: 'Transfer status', ar: 'حالة التحويل' }, statusValue: { en: 'Completed successfully', ar: 'اكتمل بنجاح' }, editAction: { en: 'Edit', ar: 'تعديل' }, viewDetailsAction: { en: 'View details', ar: 'عرض التفاصيل' }, withActionButtons: { en: 'with action buttons', ar: 'مع أزرار الإجراءات' }, }, }), };