Skip to content

Commit 354210d

Browse files
committed
refactor: data table
1 parent b23bfa1 commit 354210d

File tree

3 files changed

+33
-34
lines changed

3 files changed

+33
-34
lines changed

src/components/DataTable/DataTable.stories.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ export const Default: Story = {
4646
args: {
4747
columns,
4848
data,
49-
headerAction: {
50-
label: 'Do Something',
51-
onClick: () => {
52-
alert('Something!');
49+
headerActions: [
50+
{
51+
label: 'Do Something',
52+
onClick: () => {
53+
alert('Something!');
54+
}
5355
}
54-
},
56+
],
5557
rowActions: [
5658
{
5759
destructive: true,

src/components/DataTable/DataTable.tsx

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Fragment, useEffect, useMemo, useState } from 'react';
1+
import { useEffect, useMemo, useState } from 'react';
22

33
import {
44
flexRender,
@@ -45,10 +45,10 @@ type DataTableColumn<TData extends { [key: string]: unknown }> =
4545
type DataTableProps<TData extends { [key: string]: unknown }> = {
4646
columns: DataTableColumn<TData>[];
4747
data: TData[];
48-
headerAction?: {
48+
headerActions?: {
4949
label: string;
5050
onClick: () => void;
51-
};
51+
}[];
5252
rowActions?: RowAction<TData>[];
5353
search?: {
5454
key: Extract<keyof TData, string>;
@@ -65,7 +65,7 @@ function isStaticColumn<TData extends { [key: string]: unknown }>(
6565
export const DataTable = <TData extends { [key: string]: unknown }>({
6666
columns,
6767
data,
68-
headerAction,
68+
headerActions,
6969
rowActions,
7070
search
7171
}: DataTableProps<TData>) => {
@@ -173,7 +173,7 @@ export const DataTable = <TData extends { [key: string]: unknown }>({
173173
const pageIndexOptions = range(start, end);
174174

175175
return (
176-
<Fragment>
176+
<div className="flex flex-col">
177177
<DestructiveActionDialog
178178
destructiveActionPending={destructiveActionPending}
179179
setDestructiveActionPending={setDestructiveActionPending}
@@ -186,10 +186,14 @@ export const DataTable = <TData extends { [key: string]: unknown }>({
186186
value={searchValue}
187187
onValueChange={setSearchValue}
188188
/>
189-
{headerAction && (
190-
<Button type="button" variant="outline" onClick={headerAction.onClick}>
191-
{headerAction.label}
192-
</Button>
189+
{headerActions && (
190+
<div className="flex gap-2">
191+
{headerActions.map(({ label, onClick }, i) => (
192+
<Button key={i} type="button" variant="outline" onClick={onClick}>
193+
{label}
194+
</Button>
195+
))}
196+
</div>
193197
)}
194198
</div>
195199
)}
@@ -230,30 +234,29 @@ export const DataTable = <TData extends { [key: string]: unknown }>({
230234
</Table.Body>
231235
</Table>
232236
</div>
233-
<div className="mx-auto flex w-min pt-6 pb-4">
237+
<div className="flex w-min gap-0.5 py-4 [&>button]:h-9">
234238
<Button
235-
className="flex gap-1"
236239
disabled={!table.getCanPreviousPage()}
240+
size="icon"
237241
type="button"
238242
variant="ghost"
239243
onClick={() => table.firstPage()}
240244
>
241-
<ChevronsLeftIcon className="-ml-1 h-4 w-4" />
242-
<span>{t('pagination.first')}</span>
245+
<ChevronsLeftIcon className="h-4 w-4" />
243246
</Button>
244247
<Button
245-
className="mr-1 flex gap-1"
246248
disabled={!table.getCanPreviousPage()}
249+
size="icon"
247250
type="button"
248251
variant="ghost"
249252
onClick={() => table.previousPage()}
250253
>
251-
<ChevronLeftIcon className="-ml-1 h-4 w-4" />
252-
<span>{t('pagination.previous')}</span>
254+
<ChevronLeftIcon className="h-4 w-4" />
253255
</Button>
254256
{pageIndexOptions.map((index) => (
255257
<Button
256258
key={index}
259+
size="icon"
257260
type="button"
258261
variant={index === pagination.pageIndex ? 'outline' : 'ghost'}
259262
onClick={() => table.setPageIndex(index)}
@@ -262,27 +265,25 @@ export const DataTable = <TData extends { [key: string]: unknown }>({
262265
</Button>
263266
))}
264267
<Button
265-
className="ml-1 flex gap-1"
266268
disabled={!table.getCanNextPage()}
269+
size="icon"
267270
type="button"
268271
variant="ghost"
269272
onClick={() => table.nextPage()}
270273
>
271-
<span>{t('pagination.next')}</span>
272-
<ChevronRightIcon className="-mr-1 h-4 w-4" />
274+
<ChevronRightIcon className="h-4 w-4" />
273275
</Button>
274276
<Button
275-
className="flex gap-1"
276277
disabled={!table.getCanNextPage()}
278+
size="icon"
277279
type="button"
278280
variant="ghost"
279281
onClick={() => table.lastPage()}
280282
>
281-
<span>{t('pagination.last')}</span>
282-
<ChevronsRightIcon className="-mr-1 h-4 w-4" />
283+
<ChevronsRightIcon className="h-4 w-4" />
283284
</Button>
284285
</div>
285-
</Fragment>
286+
</div>
286287
);
287288
};
288289

src/components/DataTable/DestructiveActionDialog.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* eslint-disable @typescript-eslint/no-misused-promises */
2-
/* eslint-disable jsx-a11y/no-autofocus */
32

43
import type React from 'react';
54

@@ -26,7 +25,7 @@ export const DestructiveActionDialog: React.FC<{
2625
}
2726
}}
2827
>
29-
<Dialog.Content>
28+
<Dialog.Content onOpenAutoFocus={(event) => event.preventDefault()}>
3029
<Dialog.Header>
3130
<Dialog.Title>
3231
{t({
@@ -44,9 +43,8 @@ export const DestructiveActionDialog: React.FC<{
4443
<Dialog.Footer>
4544
<Button
4645
className="min-w-16"
47-
size="sm"
4846
type="button"
49-
variant="outline"
47+
variant="danger"
5048
onClick={async () => {
5149
await destructiveActionPending?.();
5250
setDestructiveActionPending(null);
@@ -55,9 +53,7 @@ export const DestructiveActionDialog: React.FC<{
5553
{t('libui.yes')}
5654
</Button>
5755
<Button
58-
autoFocus={true}
5956
className="min-w-16"
60-
size="sm"
6157
type="button"
6258
variant="primary"
6359
onClick={() => setDestructiveActionPending(null)}

0 commit comments

Comments
 (0)