@eml-payments/ui-kit 0.1.17 → 0.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/components/Table/Pagination/PaginationControls.js +2 -2
- package/dist/src/components/Table/Table.js +1 -1
- package/dist/src/components/Table/Table.stories.d.ts +2 -0
- package/dist/src/components/Table/Table.stories.js +11 -1
- package/dist/src/components/Table/Table.types.d.ts +5 -4
- package/dist/src/components/Table/hooks/useTableController.d.ts +1 -1
- package/dist/src/components/Table/hooks/useTableController.js +22 -9
- package/dist/src/components/Table/table.helpers.js +6 -1
- package/package.json +2 -1
|
@@ -3,7 +3,7 @@ import { Button } from '../../Button';
|
|
|
3
3
|
import { SelectWrapper } from '../../SelectWrapper';
|
|
4
4
|
import { cn } from '../../../lib/utils';
|
|
5
5
|
export const PageSizeSelector = ({ id, value, onChange, options = ['5', '10', '20', '50', '100'], label = 'Rows per page:', className, size = 'default', }) => {
|
|
6
|
-
return (_jsxs("div", { className: cn('flex items-center gap-2 px-4 py-2 text-sm', className), children: [_jsx("label", { htmlFor: `page-size-select-${id}`, children: label }), _jsx("div", { className: "w-fit", children: _jsx(SelectWrapper, { size: size, value: value, onChange: onChange, options: options.map((option) => ({
|
|
6
|
+
return (_jsxs("div", { className: cn('flex items-center gap-2 px-4 py-2 text-sm', className), id: `page-size-select-${id}`, children: [_jsx("label", { htmlFor: `page-size-select-${id}`, children: label }), _jsx("div", { className: "w-fit", children: _jsx(SelectWrapper, { size: size, value: value, onChange: onChange, options: options.map((option) => ({
|
|
7
7
|
label: option,
|
|
8
8
|
value: option,
|
|
9
9
|
})) }) })] }));
|
|
@@ -15,5 +15,5 @@ export const PaginationFooter = ({ table, tableId, paginationMode = 'client', pa
|
|
|
15
15
|
const newSize = Number(val);
|
|
16
16
|
table.setPageSize(newSize);
|
|
17
17
|
onPageSizeChange === null || onPageSizeChange === void 0 ? void 0 : onPageSizeChange(newSize);
|
|
18
|
-
}, size: "small" }), table.getRowModel().rows.length > 0 && (_jsxs("div", { className: "flex justify-center gap-3", children: [_jsx(Button, { variant: "secondary", title: "Previous", onClick: () => table.previousPage(), disabled: !table.getCanPreviousPage() }), _jsx(Button, { variant: "secondary", title: "Next", onClick: () => table.nextPage(), disabled: !table.getCanNextPage() })] })), _jsxs("div", { className: "flex justify-end", children: ["Page ", pageIndex + 1, " of ", totalPages] })] }));
|
|
18
|
+
}, size: "small" }), table.getRowModel().rows.length > 0 && (_jsxs("div", { className: "flex justify-center gap-3", id: `pagination-controls-${tableId}`, children: [_jsx(Button, { variant: "secondary", title: "Previous", onClick: () => table.previousPage(), disabled: !table.getCanPreviousPage() }), _jsx(Button, { variant: "secondary", title: "Next", onClick: () => table.nextPage(), disabled: !table.getCanNextPage() })] })), _jsxs("div", { className: "flex justify-end", id: `page-indicator-${tableId}`, children: ["Page ", pageIndex + 1, " of ", totalPages] })] }));
|
|
19
19
|
};
|
|
@@ -19,7 +19,7 @@ export function Table(props) {
|
|
|
19
19
|
return (_jsx("td", { style: { width }, className: cell.column.id === 'select' ? 'p-1' : 'px-4 py-2', children: flexRender(cell.column.columnDef.cell, cell.getContext()) }, cell.id));
|
|
20
20
|
}) }, row.id)));
|
|
21
21
|
}
|
|
22
|
-
return (_jsxs("div", { className: "relative w-full overflow-auto", children: [props.isLoading && _jsx("div", { className: "mui-loader" }), _jsx("div", { className: "rounded-[var(--uikit-radius)] overflow-hidden border", children: _jsxs("table", { className: classNames('min-w-full text-sm table-fixed', props.className), role: "table", children: [_jsx("thead", { className: "bg-[var(--uikit-tertiary)]", children: table.getHeaderGroups().map((headerGroup) => (_jsx("tr", { children: headerGroup.headers.map((header) => {
|
|
22
|
+
return (_jsxs("div", { className: "relative w-full overflow-auto", children: [props.isLoading && _jsx("div", { className: "mui-loader" }), _jsx("div", { className: "rounded-[var(--uikit-radius)] overflow-hidden border", children: _jsxs("table", { className: classNames('min-w-full text-sm table-fixed', props.className), role: "table", id: props.id, children: [_jsx("thead", { className: "bg-[var(--uikit-tertiary)]", children: table.getHeaderGroups().map((headerGroup) => (_jsx("tr", { children: headerGroup.headers.map((header) => {
|
|
23
23
|
return (_jsx("th", { style: { width: header.getSize() }, className: classNames('select-none text-[16px]', header.id === 'select' ? 'p-0' : 'px-4 py-2 text-left cursor-pointer'), onClick: header.column.getCanSort()
|
|
24
24
|
? header.column.getToggleSortingHandler()
|
|
25
25
|
: undefined, children: _jsxs("div", { className: classNames('w-full h-full', header.id === 'select'
|
|
@@ -17,5 +17,7 @@ export declare const LoadingWithSkeleton: Story;
|
|
|
17
17
|
export declare const ClientPagination: Story;
|
|
18
18
|
export declare const WithCheckboxSelection: Story;
|
|
19
19
|
export declare const WithDisabledSelection: Story;
|
|
20
|
+
export declare const WithSingleRowSelection: Story;
|
|
21
|
+
export declare const WithDefaultRowsSelected: Story;
|
|
20
22
|
export declare const FlexColumnWidths: Story;
|
|
21
23
|
export declare const WithControlledSorting: Story;
|
|
@@ -53,12 +53,22 @@ export const LoadingWithSkeleton = {
|
|
|
53
53
|
export const ClientPagination = {
|
|
54
54
|
render: () => _jsx(Table, { id: "example-table", data: data, columns: columns, paginationMode: "client", rowsPerPage: 5 }),
|
|
55
55
|
};
|
|
56
|
+
const handleSelectionChange = (selectedRowIds) => {
|
|
57
|
+
console.log('Selected Row IDs:', selectedRowIds);
|
|
58
|
+
// Update your state or perform actions based on selected rows
|
|
59
|
+
};
|
|
56
60
|
export const WithCheckboxSelection = {
|
|
57
|
-
render: () => (_jsx(Table, { id: "example-table", data: data, columns: columns, checkboxSelection: true, checkboxPosition: "start", paginationMode: "client" })),
|
|
61
|
+
render: () => (_jsx(Table, { id: "example-table", data: data, columns: columns, checkboxSelection: true, checkboxPosition: "start", paginationMode: "client", onSelectionChange: handleSelectionChange })),
|
|
58
62
|
};
|
|
59
63
|
export const WithDisabledSelection = {
|
|
60
64
|
render: () => (_jsx(Table, { id: "example-table", data: data, columns: columns, checkboxSelection: true, checkboxPosition: "start", enableRowSelection: (row) => row.original.role !== 'Admin' })),
|
|
61
65
|
};
|
|
66
|
+
export const WithSingleRowSelection = {
|
|
67
|
+
render: () => (_jsx(Table, { id: "example-table", data: data, columns: columns, checkboxSelection: true, isMultiRowSelection: false })),
|
|
68
|
+
};
|
|
69
|
+
export const WithDefaultRowsSelected = {
|
|
70
|
+
render: () => (_jsx(Table, { id: "example-table", data: data, columns: columns, checkboxSelection: true, selectedRowIds: ['user-1', 'user-3'] })),
|
|
71
|
+
};
|
|
62
72
|
export const FlexColumnWidths = {
|
|
63
73
|
render: () => {
|
|
64
74
|
const testColumns = [
|
|
@@ -2,12 +2,11 @@ import type { ColumnDef, Row, SortingState } from '@tanstack/react-table';
|
|
|
2
2
|
export type FlexColumnDef<T> = ColumnDef<T> & {
|
|
3
3
|
flex?: number;
|
|
4
4
|
};
|
|
5
|
-
export interface TableProps<T
|
|
6
|
-
id: string;
|
|
7
|
-
}> {
|
|
5
|
+
export interface TableProps<T> {
|
|
8
6
|
id: string;
|
|
9
7
|
data: T[];
|
|
10
8
|
columns: FlexColumnDef<T>[];
|
|
9
|
+
rowIdKey?: keyof T | 'id';
|
|
11
10
|
className?: string;
|
|
12
11
|
isLoading?: boolean;
|
|
13
12
|
showSkeletonRows?: boolean;
|
|
@@ -15,10 +14,12 @@ export interface TableProps<T extends {
|
|
|
15
14
|
checkboxPosition?: 'start' | 'end';
|
|
16
15
|
sorting?: SortingState;
|
|
17
16
|
onSortingChange?: (s: SortingState) => void;
|
|
18
|
-
onSelectionChange?: (
|
|
17
|
+
onSelectionChange?: (selectedIds: string[]) => void;
|
|
19
18
|
enableRowSelection?: (row: Row<T>) => boolean;
|
|
19
|
+
isMultiRowSelection?: boolean;
|
|
20
20
|
pageCount?: number;
|
|
21
21
|
paginationMode?: 'client' | 'server';
|
|
22
22
|
noRowsMessage?: string;
|
|
23
23
|
rowsPerPage?: number;
|
|
24
|
+
selectedRowIds?: string[];
|
|
24
25
|
}
|
|
@@ -2,7 +2,7 @@ import { type SortingState } from '@tanstack/react-table';
|
|
|
2
2
|
import type { TableProps } from '../Table.types';
|
|
3
3
|
export declare function useTableController<T extends {
|
|
4
4
|
id: string;
|
|
5
|
-
}>({ data, columns, checkboxSelection, checkboxPosition, paginationMode, pageCount, sorting, onSortingChange, onSelectionChange, enableRowSelection, rowsPerPage, }: TableProps<T>): {
|
|
5
|
+
}>({ data, columns, checkboxSelection, checkboxPosition, paginationMode, pageCount, sorting, onSortingChange, onSelectionChange, enableRowSelection, rowsPerPage, isMultiRowSelection, selectedRowIds, rowIdKey, }: TableProps<T>): {
|
|
6
6
|
table: import("@tanstack/react-table").Table<T>;
|
|
7
7
|
pageSize: number;
|
|
8
8
|
pageIndex: number;
|
|
@@ -1,19 +1,29 @@
|
|
|
1
|
-
import { useEffect,
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
2
|
import { useReactTable, getCoreRowModel, getSortedRowModel, getPaginationRowModel, } from '@tanstack/react-table';
|
|
3
3
|
import { applyFlexSizes, getCheckboxSelectionColumn } from '../table.helpers';
|
|
4
|
-
export function useTableController({ data, columns, checkboxSelection, checkboxPosition = 'start', paginationMode = 'client', pageCount, sorting, onSortingChange, onSelectionChange, enableRowSelection, rowsPerPage = 10, }) {
|
|
4
|
+
export function useTableController({ data, columns, checkboxSelection, checkboxPosition = 'start', paginationMode = 'client', pageCount, sorting, onSortingChange, onSelectionChange, enableRowSelection, rowsPerPage = 10, isMultiRowSelection = true, selectedRowIds, rowIdKey = 'id', }) {
|
|
5
5
|
const safeData = Array.isArray(data) ? data : [];
|
|
6
6
|
const [tableColumns, setTableColumns] = useState(() => applyFlexSizes(columns !== null && columns !== void 0 ? columns : []));
|
|
7
7
|
const [internalSorting, setInternalSorting] = useState(sorting !== null && sorting !== void 0 ? sorting : []);
|
|
8
|
+
const [internalRowSelection, setInternalRowSelection] = useState(() => {
|
|
9
|
+
if (selectedRowIds) {
|
|
10
|
+
return selectedRowIds.reduce((acc, id) => {
|
|
11
|
+
acc[id] = true;
|
|
12
|
+
return acc;
|
|
13
|
+
}, {});
|
|
14
|
+
}
|
|
15
|
+
return {};
|
|
16
|
+
});
|
|
8
17
|
const [pageIndex, setPageIndex] = useState(0);
|
|
9
18
|
const [pageSize, setPageSize] = useState(rowsPerPage !== null && rowsPerPage !== void 0 ? rowsPerPage : 10);
|
|
10
19
|
const table = useReactTable({
|
|
11
20
|
data: safeData,
|
|
12
21
|
columns: tableColumns,
|
|
13
|
-
getRowId: (row) => row
|
|
22
|
+
getRowId: (row) => String(row[rowIdKey]),
|
|
14
23
|
state: {
|
|
15
24
|
pagination: { pageSize, pageIndex },
|
|
16
25
|
sorting: sorting !== null && sorting !== void 0 ? sorting : internalSorting,
|
|
26
|
+
rowSelection: internalRowSelection,
|
|
17
27
|
},
|
|
18
28
|
getSortedRowModel: getSortedRowModel(),
|
|
19
29
|
getCoreRowModel: getCoreRowModel(),
|
|
@@ -31,6 +41,15 @@ export function useTableController({ data, columns, checkboxSelection, checkboxP
|
|
|
31
41
|
onSortingChange === null || onSortingChange === void 0 ? void 0 : onSortingChange(nextSorting);
|
|
32
42
|
},
|
|
33
43
|
enableRowSelection: enableRowSelection !== null && enableRowSelection !== void 0 ? enableRowSelection : true,
|
|
44
|
+
onRowSelectionChange: (updater) => {
|
|
45
|
+
const newSelection = typeof updater === 'function' ? updater(internalRowSelection) : updater;
|
|
46
|
+
setInternalRowSelection(newSelection);
|
|
47
|
+
if (onSelectionChange) {
|
|
48
|
+
const selectedIdsArray = Object.keys(newSelection).filter((id) => newSelection[id]);
|
|
49
|
+
onSelectionChange(selectedIdsArray);
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
enableMultiRowSelection: isMultiRowSelection,
|
|
34
53
|
});
|
|
35
54
|
useEffect(() => {
|
|
36
55
|
const normalized = applyFlexSizes(columns !== null && columns !== void 0 ? columns : []);
|
|
@@ -52,12 +71,6 @@ export function useTableController({ data, columns, checkboxSelection, checkboxP
|
|
|
52
71
|
}
|
|
53
72
|
setTableColumns(finalColumns);
|
|
54
73
|
}, [columns, checkboxSelection, checkboxPosition, table]);
|
|
55
|
-
const selectedRows = useMemo(() => {
|
|
56
|
-
return table.getSelectedRowModel().rows.map((row) => row.original);
|
|
57
|
-
}, [table.getSelectedRowModel().rows]);
|
|
58
|
-
useEffect(() => {
|
|
59
|
-
onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange(selectedRows);
|
|
60
|
-
}, [selectedRows, onSelectionChange]);
|
|
61
74
|
return {
|
|
62
75
|
table,
|
|
63
76
|
pageSize,
|
|
@@ -5,8 +5,13 @@ export function getCheckboxSelectionColumn(table) {
|
|
|
5
5
|
id: 'select',
|
|
6
6
|
size: 24,
|
|
7
7
|
header: () => {
|
|
8
|
+
if (!table.options.enableMultiRowSelection) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
8
11
|
const hasSelectableRows = table.getRowModel().rows.some((row) => row.getCanSelect());
|
|
9
|
-
return (_jsx("div", { className: "flex justify-center items-center h-full", children: _jsx(Checkbox, { checked: table.getIsAllPageRowsSelected(),
|
|
12
|
+
return (_jsx("div", { className: "flex justify-center items-center h-full", children: _jsx(Checkbox, { checked: table.getIsAllPageRowsSelected(),
|
|
13
|
+
// indeterminate={table.getIsSomeRowsSelected()} TODO: Implement indeterminate state
|
|
14
|
+
disabled: !hasSelectableRows, onCheckedChange: (checked) => {
|
|
10
15
|
table.toggleAllPageRowsSelected(Boolean(checked));
|
|
11
16
|
} }) }));
|
|
12
17
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eml-payments/ui-kit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.18",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "ARLO UIKit",
|
|
6
6
|
"homepage": "https://github.com/EML-Payments/arlo.npm.uikit#readme",
|
|
@@ -50,6 +50,7 @@
|
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
52
|
"@radix-ui/react-checkbox": "^1.3.2",
|
|
53
|
+
"@radix-ui/react-dropdown-menu": "^2.1.15",
|
|
53
54
|
"@radix-ui/react-label": "^2.1.7",
|
|
54
55
|
"@radix-ui/react-radio-group": "^1.3.7",
|
|
55
56
|
"@radix-ui/react-select": "^2.2.5",
|