@eml-payments/ui-kit 1.7.5 → 1.7.6

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.
Files changed (32) hide show
  1. package/dist/index.css +1 -1
  2. package/dist/index.d.cts +488 -0
  3. package/dist/index.d.ts +488 -0
  4. package/dist/src/components/Table/BaseTable/index.d.ts +1 -0
  5. package/dist/src/components/Table/BaseTable/index.js +1 -0
  6. package/dist/src/components/Table/Pagination/PaginationControls.d.ts +3 -0
  7. package/dist/src/components/Table/Pagination/PaginationControls.js +22 -0
  8. package/dist/src/components/Table/Pagination/PaginationControls.types.d.ts +24 -0
  9. package/dist/src/components/Table/Pagination/PaginationControls.types.js +1 -0
  10. package/dist/src/components/Table/StandardTable/StandardTable.d.ts +1 -1
  11. package/dist/src/components/Table/StandardTable/StandardTable.stories.d.ts +2 -1
  12. package/dist/src/components/Table/StandardTable/StandardTable.stories.js +58 -10
  13. package/dist/src/components/Table/StandardTable/StandardTable.types.d.ts +2 -2
  14. package/dist/src/components/Table/StandardTable/useStandardTableController.d.ts +1 -1
  15. package/dist/src/components/Table/StandardTable/useStandardTableController.js +13 -2
  16. package/dist/src/components/Table/Table.d.ts +4 -0
  17. package/dist/src/components/Table/Table.js +93 -0
  18. package/dist/src/components/Table/Table.stories.d.ts +31 -0
  19. package/dist/src/components/Table/Table.stories.js +479 -0
  20. package/dist/src/components/Table/Table.types.d.ts +15 -4
  21. package/dist/src/components/Table/hooks/useInfiniteScrolling.d.ts +29 -0
  22. package/dist/src/components/Table/hooks/useInfiniteScrolling.js +96 -0
  23. package/dist/src/components/Table/hooks/usePaginationController.d.ts +16 -0
  24. package/dist/src/components/Table/hooks/usePaginationController.js +30 -0
  25. package/dist/src/components/Table/hooks/useTableController.d.ts +26 -0
  26. package/dist/src/components/Table/hooks/useTableController.js +146 -0
  27. package/dist/src/components/Table/hooks/useUrlPaginationSync.d.ts +7 -1
  28. package/dist/src/components/Table/hooks/useUrlPaginationSync.js +52 -10
  29. package/dist/src/components/Table/types/scopedTableId.types.d.ts +4 -0
  30. package/dist/src/components/Table/types/scopedTableId.types.js +1 -0
  31. package/dist/src/components/Tooltip/Tooltip.stories.js +1 -1
  32. package/package.json +1 -1
@@ -0,0 +1,30 @@
1
+ import { useCallback, useEffect, useMemo } from 'react';
2
+ export function usePaginationController({ table, paginationMode, onRefetch }) {
3
+ const { pageIndex, pageSize } = table.getState().pagination;
4
+ const canNextPage = table.getCanNextPage();
5
+ const canPrevPage = table.getCanPreviousPage();
6
+ const onNextPage = useCallback(() => {
7
+ if (canNextPage) {
8
+ table.nextPage();
9
+ }
10
+ }, [canNextPage, table]);
11
+ const onPrevPage = useCallback(() => {
12
+ if (canPrevPage) {
13
+ table.previousPage();
14
+ }
15
+ }, [canPrevPage, table]);
16
+ // Only refetch in server mode
17
+ useEffect(() => {
18
+ if (paginationMode === 'server' && onRefetch) {
19
+ onRefetch(pageIndex, pageSize);
20
+ }
21
+ }, [paginationMode, pageIndex, pageSize, onRefetch]);
22
+ return useMemo(() => ({
23
+ pageIndex,
24
+ pageSize,
25
+ canNextPage,
26
+ canPrevPage,
27
+ onNextPage,
28
+ onPrevPage,
29
+ }), [pageIndex, pageSize, canNextPage, canPrevPage, onNextPage, onPrevPage]);
30
+ }
@@ -0,0 +1,26 @@
1
+ import { type SortingState } from '@tanstack/react-table';
2
+ import type { TableProps } from '../Table.types';
3
+ export declare function useTableController<T extends {
4
+ id: string;
5
+ }>({ id, height, data, columns, checkboxSelection, checkboxPosition, paginationMode, sorting, onSortingChange, onSelectionChange, enableRowSelection, rowsPerPage, isMultiRowSelection, selectedRowIds, rowIdKey, totalServerRows, onRefetch, showHeader, grouping, infiniteScroll, virtualization, }: TableProps<T>): {
6
+ pageIndex: number;
7
+ pageSize: number;
8
+ canNextPage: boolean;
9
+ canPrevPage: boolean;
10
+ onNextPage: () => void;
11
+ onPrevPage: () => void;
12
+ table: import("@tanstack/table-core").Table<T>;
13
+ height: string | number | undefined;
14
+ showHeader: boolean;
15
+ grouping: string[] | undefined;
16
+ setPageSize: import("react").Dispatch<import("react").SetStateAction<number>>;
17
+ setPageIndex: import("react").Dispatch<import("react").SetStateAction<number>>;
18
+ sorting: SortingState;
19
+ virtualizationEnabled: boolean;
20
+ rowVirtualizer: import("@tanstack/virtual-core").Virtualizer<HTMLDivElement, Element> | undefined;
21
+ hasNextPage: boolean;
22
+ parentScrollRef: import("react").MutableRefObject<HTMLDivElement | null>;
23
+ loaderRef: import("react").MutableRefObject<HTMLTableRowElement | null>;
24
+ infiniteScroll: import("..").InfiniteScrollOptions | undefined;
25
+ virtualization: import("..").VirtualizationOptions | undefined;
26
+ };
@@ -0,0 +1,146 @@
1
+ import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
2
+ import { useReactTable, getCoreRowModel, getSortedRowModel, getPaginationRowModel, getGroupedRowModel, getExpandedRowModel, } from '@tanstack/react-table';
3
+ import { applyFlexSizes, getCheckboxSelectionColumn } from '../table.helpers';
4
+ import { usePaginationController } from './usePaginationController';
5
+ import { useUrlPaginationSync } from './useUrlPaginationSync';
6
+ import { useInfiniteScrolling } from './useInfiniteScrolling';
7
+ export function useTableController({ id, height, data, columns, checkboxSelection, checkboxPosition = 'start', paginationMode = 'client', sorting, onSortingChange, onSelectionChange, enableRowSelection, rowsPerPage = 10, isMultiRowSelection = true, selectedRowIds, rowIdKey = 'id', totalServerRows, onRefetch, showHeader = true, grouping, infiniteScroll, virtualization, }) {
8
+ const safeData = Array.isArray(data) ? data : [];
9
+ const stableGrouping = useMemo(() => grouping !== null && grouping !== void 0 ? grouping : [], [grouping]);
10
+ const parentScrollRef = useRef(null);
11
+ const loaderRef = useRef(null);
12
+ const columnVisibility = useMemo(() => {
13
+ return Object.fromEntries(stableGrouping.map((columnId) => [columnId, false]));
14
+ }, [stableGrouping]);
15
+ const [containerWidth, setContainerWidth] = useState(0);
16
+ useLayoutEffect(() => {
17
+ if (!parentScrollRef.current)
18
+ return;
19
+ const ro = new ResizeObserver(([entry]) => {
20
+ setContainerWidth(entry.contentRect.width);
21
+ });
22
+ ro.observe(parentScrollRef.current);
23
+ return () => ro.disconnect();
24
+ }, []);
25
+ const [tableColumns, setTableColumns] = useState(() => applyFlexSizes(columns !== null && columns !== void 0 ? columns : [], containerWidth, !!(virtualization === null || virtualization === void 0 ? void 0 : virtualization.enabled)));
26
+ const [internalSorting, setInternalSorting] = useState(sorting !== null && sorting !== void 0 ? sorting : []);
27
+ const [internalRowSelection, setInternalRowSelection] = useState(() => {
28
+ if (selectedRowIds) {
29
+ return selectedRowIds.reduce((acc, id) => {
30
+ acc[id] = true;
31
+ return acc;
32
+ }, {});
33
+ }
34
+ return {};
35
+ });
36
+ const { pageIndex, setPageIndex, pageSize, setPageSize } = useUrlPaginationSync(rowsPerPage, id);
37
+ const isInfinite = !!(infiniteScroll === null || infiniteScroll === void 0 ? void 0 : infiniteScroll.enabled);
38
+ const dataPages = useMemo(() => {
39
+ if (!isInfinite) {
40
+ return undefined;
41
+ }
42
+ return [{ rows: safeData }];
43
+ }, [isInfinite, safeData.length]);
44
+ const infiniteOpts = useMemo(() => infiniteScroll, [infiniteScroll]);
45
+ const virtualizationOpts = useMemo(() => virtualization, [virtualization]);
46
+ const { allRows, virtualizationEnabled, rowVirtualizer, hasNextPage } = useInfiniteScrolling({
47
+ dataPages,
48
+ flatData: data,
49
+ infiniteScroll: infiniteOpts,
50
+ virtualization: virtualizationOpts,
51
+ parentScrollRef,
52
+ loaderRef,
53
+ });
54
+ const dataForTable = useMemo(() => (virtualizationEnabled || isInfinite ? allRows : safeData), [virtualizationEnabled, isInfinite, allRows, safeData]);
55
+ const table = useReactTable({
56
+ data: dataForTable,
57
+ columns: tableColumns,
58
+ getRowId: (row) => String(row[rowIdKey]),
59
+ state: {
60
+ ...(isInfinite ? {} : { pagination: { pageIndex, pageSize } }),
61
+ sorting: sorting !== null && sorting !== void 0 ? sorting : internalSorting,
62
+ rowSelection: internalRowSelection,
63
+ grouping: stableGrouping,
64
+ columnVisibility,
65
+ },
66
+ getSortedRowModel: getSortedRowModel(),
67
+ getCoreRowModel: getCoreRowModel(),
68
+ getPaginationRowModel: !isInfinite && paginationMode === 'client' ? getPaginationRowModel() : undefined,
69
+ manualPagination: !isInfinite && paginationMode === 'server',
70
+ autoResetPageIndex: false,
71
+ pageCount: !isInfinite && paginationMode === 'server' ? Math.ceil((totalServerRows !== null && totalServerRows !== void 0 ? totalServerRows : 0) / pageSize) : undefined,
72
+ onPaginationChange: (updater) => {
73
+ if (isInfinite)
74
+ return;
75
+ const next = typeof updater === 'function' ? updater({ pageIndex, pageSize }) : updater;
76
+ setPageIndex(next.pageIndex);
77
+ setPageSize(next.pageSize);
78
+ },
79
+ onSortingChange: (updater) => {
80
+ const nextSorting = typeof updater === 'function' ? updater(sorting !== null && sorting !== void 0 ? sorting : internalSorting) : updater;
81
+ setInternalSorting(nextSorting);
82
+ onSortingChange === null || onSortingChange === void 0 ? void 0 : onSortingChange(nextSorting);
83
+ },
84
+ enableRowSelection: enableRowSelection !== null && enableRowSelection !== void 0 ? enableRowSelection : true,
85
+ onRowSelectionChange: (updater) => {
86
+ const newSelection = typeof updater === 'function' ? updater(internalRowSelection) : updater;
87
+ setInternalRowSelection(newSelection);
88
+ if (onSelectionChange) {
89
+ const selectedIdsArray = Object.keys(newSelection).filter((id) => newSelection[id]);
90
+ onSelectionChange(selectedIdsArray);
91
+ }
92
+ },
93
+ enableMultiRowSelection: isMultiRowSelection,
94
+ getGroupedRowModel: getGroupedRowModel(),
95
+ getExpandedRowModel: getExpandedRowModel(),
96
+ });
97
+ useEffect(() => {
98
+ if (paginationMode === 'none') {
99
+ setPageIndex(0);
100
+ }
101
+ }, [paginationMode, setPageIndex]);
102
+ const pagination = usePaginationController({
103
+ table,
104
+ paginationMode,
105
+ onRefetch,
106
+ });
107
+ const selectionColumn = useMemo(() => {
108
+ return checkboxSelection ? getCheckboxSelectionColumn(table) : null;
109
+ }, [checkboxSelection, table]);
110
+ useEffect(() => {
111
+ const normalized = applyFlexSizes(columns !== null && columns !== void 0 ? columns : [], containerWidth, !!(virtualization === null || virtualization === void 0 ? void 0 : virtualization.enabled));
112
+ let finalColumns;
113
+ if (checkboxSelection && selectionColumn) {
114
+ if (checkboxPosition === 'start') {
115
+ finalColumns = [selectionColumn, ...normalized];
116
+ }
117
+ else if (checkboxPosition === 'end') {
118
+ finalColumns = [...normalized, selectionColumn];
119
+ }
120
+ else {
121
+ finalColumns = normalized;
122
+ }
123
+ }
124
+ else {
125
+ finalColumns = normalized;
126
+ }
127
+ setTableColumns(finalColumns);
128
+ }, [columns, checkboxSelection, checkboxPosition, containerWidth, virtualization === null || virtualization === void 0 ? void 0 : virtualization.enabled]);
129
+ return {
130
+ table,
131
+ height,
132
+ showHeader,
133
+ grouping,
134
+ setPageSize,
135
+ setPageIndex,
136
+ sorting: sorting !== null && sorting !== void 0 ? sorting : internalSorting,
137
+ virtualizationEnabled,
138
+ rowVirtualizer,
139
+ hasNextPage,
140
+ parentScrollRef,
141
+ loaderRef,
142
+ infiniteScroll,
143
+ virtualization,
144
+ ...pagination,
145
+ };
146
+ }
@@ -1,4 +1,10 @@
1
- export declare function useUrlPaginationSync(tableId: string, defaultPageSize: number): {
1
+ export interface UseUrlPaginationSyncOptions {
2
+ defaultPageSize: number;
3
+ tableId?: string;
4
+ scoped?: boolean;
5
+ enabled?: boolean;
6
+ }
7
+ export declare function useUrlPaginationSync({ defaultPageSize, tableId, scoped, enabled, }: UseUrlPaginationSyncOptions): {
2
8
  initialPageIndex: number;
3
9
  initialPageSize: number;
4
10
  syncToUrl: (pageIndex: number, pageSize: number) => void;
@@ -1,23 +1,65 @@
1
- import { useCallback, useState } from 'react';
1
+ import { useCallback, useEffect, useMemo } from 'react';
2
2
  import { useSearchParams } from 'react-router-dom';
3
- export function useUrlPaginationSync(tableId, defaultPageSize) {
3
+ function getPaginationParamKeys(tableId) {
4
+ if (tableId) {
5
+ return {
6
+ pageNoKey: `${tableId}Page`,
7
+ pageSizeKey: `${tableId}Size`,
8
+ };
9
+ }
10
+ return {
11
+ pageNoKey: 'page',
12
+ pageSizeKey: 'size',
13
+ };
14
+ }
15
+ export function useUrlPaginationSync({ defaultPageSize, tableId, scoped = false, enabled = true, }) {
4
16
  const [searchParams, setSearchParams] = useSearchParams();
5
- const pageNoKey = `${tableId}_pageNo`;
6
- const pageSizeKey = `${tableId}_pageSize`;
7
- const [initialPageIndex] = useState(() => {
17
+ const { pageNoKey, pageSizeKey } = useMemo(() => getPaginationParamKeys(scoped ? tableId : undefined), [tableId, scoped]);
18
+ const initialPageIndex = useMemo(() => {
19
+ if (!enabled) {
20
+ return 0;
21
+ }
8
22
  const raw = Number(searchParams.get(pageNoKey));
9
- return Number.isFinite(raw) && raw > 0 ? raw - 1 : 0;
10
- });
11
- const [initialPageSize] = useState(() => {
23
+ const oneBased = Number.isFinite(raw) && raw > 0 ? raw : 1;
24
+ return oneBased - 1;
25
+ }, [enabled, searchParams, pageNoKey]);
26
+ const initialPageSize = useMemo(() => {
27
+ if (!enabled) {
28
+ return defaultPageSize;
29
+ }
12
30
  const raw = Number(searchParams.get(pageSizeKey));
13
31
  return Number.isFinite(raw) && raw > 0 ? raw : defaultPageSize;
14
- });
32
+ }, [enabled, searchParams, pageSizeKey, defaultPageSize]);
15
33
  const syncToUrl = useCallback((pageIndex, pageSize) => {
34
+ if (!enabled) {
35
+ return;
36
+ }
16
37
  const next = new URLSearchParams(searchParams);
17
38
  next.set(pageNoKey, String(pageIndex + 1)); // 1-based
18
39
  next.set(pageSizeKey, String(pageSize));
19
40
  setSearchParams(next, { replace: true });
20
- }, [pageNoKey, pageSizeKey, searchParams, setSearchParams]);
41
+ }, [enabled, searchParams, setSearchParams, pageNoKey, pageSizeKey]);
42
+ useEffect(() => {
43
+ if (enabled) {
44
+ return;
45
+ }
46
+ if (!searchParams.has(pageNoKey) && !searchParams.has(pageSizeKey)) {
47
+ return;
48
+ }
49
+ const next = new URLSearchParams(searchParams);
50
+ next.delete(pageNoKey);
51
+ next.delete(pageSizeKey);
52
+ setSearchParams(next, { replace: true });
53
+ }, [enabled, searchParams, setSearchParams, pageNoKey, pageSizeKey]);
54
+ // Ensure active table pagination params exist (also when param keys change).
55
+ useEffect(() => {
56
+ if (!enabled) {
57
+ return;
58
+ }
59
+ if (!searchParams.has(pageNoKey) || !searchParams.has(pageSizeKey)) {
60
+ syncToUrl(initialPageIndex, initialPageSize);
61
+ }
62
+ }, [enabled, searchParams, pageNoKey, pageSizeKey, initialPageIndex, initialPageSize, syncToUrl]);
21
63
  return {
22
64
  initialPageIndex,
23
65
  initialPageSize,
@@ -0,0 +1,4 @@
1
+ type IsAsciiLetter<C extends string> = C extends Lowercase<C> ? (C extends Uppercase<C> ? false : true) : false;
2
+ type IsLowerAlphaNum<S extends string> = S extends '' ? true : S extends `${infer First}${infer Rest}` ? IsAsciiLetter<First> extends true ? IsLowerAlphaNum<Rest> : First extends `${number}` ? IsLowerAlphaNum<Rest> : false : false;
3
+ export type ScopedTableId<T extends string> = string extends T ? string : T extends `${infer First}${infer Rest}` ? IsAsciiLetter<First> extends true ? IsLowerAlphaNum<Rest> extends true ? T : never : never : never;
4
+ export {};
@@ -12,7 +12,7 @@ export const Default = {
12
12
  render: () => (_jsx(TooltipStoryWrapper, { content: "Simple tooltip", children: _jsx(Button, { children: "Hover me" }) })),
13
13
  };
14
14
  export const LongTextResponsive = {
15
- render: () => (_jsx(TooltipStoryWrapper, { content: "This tooltip contains a longer message that wraps gracefully across multiple lines. On smaller\r\n\t\tscreens, it narrows and stacks vertically so it's easier to read\u2014especially useful for\r\n\t\taccessibility, mobile devices, or multi-language support.", side: "top", children: _jsx(Button, { children: "Hover me" }) })),
15
+ render: () => (_jsx(TooltipStoryWrapper, { content: "This tooltip contains a longer message that wraps gracefully across multiple lines. On smaller\n\t\tscreens, it narrows and stacks vertically so it's easier to read\u2014especially useful for\n\t\taccessibility, mobile devices, or multi-language support.", side: "top", children: _jsx(Button, { children: "Hover me" }) })),
16
16
  };
17
17
  export const OnDifferentSides = {
18
18
  render: () => (_jsxs("div", { className: "grid grid-cols-2 gap-4", children: [_jsx(TooltipStoryWrapper, { content: "Top tooltip", side: "top", children: _jsx(Button, { children: "Top" }) }), _jsx(TooltipStoryWrapper, { content: "Bottom tooltip", side: "bottom", children: _jsx(Button, { children: "Bottom" }) }), _jsx(TooltipStoryWrapper, { content: "Left tooltip", side: "left", children: _jsx(Button, { children: "Left" }) }), _jsx(TooltipStoryWrapper, { content: "Right tooltip", side: "right", children: _jsx(Button, { children: "Right" }) })] })),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eml-payments/ui-kit",
3
- "version": "1.7.5",
3
+ "version": "1.7.6",
4
4
  "private": false,
5
5
  "description": "ARLO UIKit",
6
6
  "homepage": "https://github.com/EML-Payments/arlo.npm.uikit#readme",