Skip to content

Commit 44afcec

Browse files
committed
fix(table): improve stories #122
1 parent c006c6d commit 44afcec

1 file changed

Lines changed: 56 additions & 4 deletions

File tree

src/tedi/components/content/table/table.stories.tsx

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { CSS } from '@dnd-kit/utilities';
2222
import type { Meta, StoryObj } from '@storybook/react';
2323
import type { ColumnDef, ColumnOrderState } from '@tanstack/react-table';
2424
import { createContext, useCallback, useContext, useMemo, useRef, useState } from 'react';
25+
import type { DateRange } from 'react-day-picker';
2526

2627
import { Icon } from '../../base/icon/icon';
2728
import { Heading } from '../../base/typography/heading/heading';
@@ -30,6 +31,7 @@ import Button from '../../buttons/button/button';
3031
import { ClosingButton } from '../../buttons/closing-button/closing-button';
3132
import InfoButton from '../../buttons/info-button/info-button';
3233
import { Checkbox } from '../../form/checkbox/checkbox';
34+
import { DateField } from '../../form/date-field/date-field';
3335
import { TextField } from '../../form/textfield/textfield';
3436
import { VerticalSpacing } from '../../layout/vertical-spacing';
3537
import { EmptyState } from '../../misc/empty-state';
@@ -147,20 +149,37 @@ type Story = StoryObj<TableProps<Person>>;
147149

148150
interface Booking {
149151
id: string;
150-
dateRange: string;
152+
dateRange: DateRange;
151153
hour: string;
152154
duration: string;
153155
location: string;
154156
}
155157

158+
const bookingDateRange: DateRange = {
159+
from: new Date(2029, 2, 22),
160+
to: new Date(2029, 2, 29),
161+
};
162+
156163
const bookings: Booking[] = Array.from({ length: 28 }, (_, index) => ({
157164
id: String(index + 1),
158-
dateRange: '22.03.2029 – 29.03.2029',
165+
dateRange: bookingDateRange,
159166
hour: '11:14',
160167
duration: '6 min',
161168
location: 'Harjumaa',
162169
}));
163170

171+
const dateRangeFormatter = new Intl.DateTimeFormat('et-EE', {
172+
day: '2-digit',
173+
month: '2-digit',
174+
year: 'numeric',
175+
});
176+
177+
const formatDateRange = (range: DateRange | undefined): string => {
178+
if (!range?.from) return '';
179+
const from = dateRangeFormatter.format(range.from);
180+
return range.to ? `${from}${dateRangeFormatter.format(range.to)}` : from;
181+
};
182+
164183
interface Doctor {
165184
id: string;
166185
name: string;
@@ -328,6 +347,39 @@ function EditableTextCell<T extends { id: string }>({
328347
);
329348
}
330349

350+
/** Cell renderer that flips to a `<DateField mode="range">` when its row is editing. */
351+
function EditableDateRangeCell<T extends { id: string }>({
352+
row,
353+
field,
354+
label,
355+
}: {
356+
row: T;
357+
field: keyof T & string;
358+
label: string;
359+
}) {
360+
const editor = useEditor<T>();
361+
const isEditing = row.id === editor.editingId && editor.draft;
362+
const value = row[field] as DateRange | undefined;
363+
if (!isEditing) {
364+
return <>{formatDateRange(value)}</>;
365+
}
366+
const draftValue = (editor.draft as T)[field] as DateRange | undefined;
367+
return (
368+
<DateField
369+
id={`${row.id}-${field}`}
370+
mode="range"
371+
label={label}
372+
inputProps={{ hideLabel: true }}
373+
selected={draftValue}
374+
onSelect={(next) =>
375+
editor.setDraft((prev: T | null) =>
376+
prev ? { ...prev, [field]: (next as DateRange | undefined) ?? { from: undefined } } : prev
377+
)
378+
}
379+
/>
380+
);
381+
}
382+
331383
/**
332384
* Each cell flips into a TextField when its row is being edited; the actions column
333385
* swaps the Muuda link for cancel / commit buttons. The cells read editor
@@ -339,7 +391,7 @@ const bookingShowcaseColumns: ColumnDef<Booking>[] = [
339391
id: 'dateRange',
340392
header: 'Kuupäev',
341393
accessorKey: 'dateRange',
342-
cell: ({ row }) => <EditableTextCell row={row.original} field="dateRange" label="Kuupäev" icon="calendar_today" />,
394+
cell: ({ row }) => <EditableDateRangeCell row={row.original} field="dateRange" label="Kuupäev" />,
343395
},
344396
{
345397
id: 'hour',
@@ -640,7 +692,7 @@ const mergedCellsColumns: ColumnDef<Booking>[] = [
640692
</span>
641693
);
642694
},
643-
cell: ({ row }) => <EditableTextCell row={row.original} field="dateRange" label="Kuupäev" icon="calendar_today" />,
695+
cell: ({ row }) => <EditableDateRangeCell row={row.original} field="dateRange" label="Kuupäev" />,
644696
},
645697
{
646698
id: 'aeg',

0 commit comments

Comments
 (0)