@eml-payments/ui-kit 1.1.2 → 1.1.4

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.
@@ -1,14 +1,6 @@
1
1
  import { Table } from './Table';
2
- import type { TableProps } from './Table.types';
3
2
  import type { Meta, StoryObj } from '@storybook/react';
4
- interface ExtraArgs {
5
- initialPageNo?: number;
6
- initialPageSize?: number;
7
- }
8
- type TableStoryArgs = TableProps<{
9
- id: string;
10
- }> & ExtraArgs;
11
- declare const meta: Meta<TableStoryArgs>;
3
+ declare const meta: Meta<typeof Table>;
12
4
  export default meta;
13
5
  type Story = StoryObj<typeof Table>;
14
6
  export declare const Basic: Story;
@@ -30,4 +22,6 @@ export declare const WithHiddenHeader: Story;
30
22
  export declare const GroupedByDate: Story;
31
23
  export declare const WithSearch: Story;
32
24
  export declare const ServerSideSearch: Story;
33
- export declare const WithQueryParams: StoryObj<TableStoryArgs>;
25
+ export declare const ClientPaginationWithQueryParams: StoryObj;
26
+ export declare const ServerPaginationWithQueryParams: StoryObj;
27
+ export declare const TwoTablesWithQueryParams: StoryObj;
@@ -44,18 +44,6 @@ const columns = [
44
44
  const meta = {
45
45
  title: 'UIKit/Table',
46
46
  component: Table,
47
- args: {
48
- initialPageNo: 1,
49
- initialPageSize: 10,
50
- },
51
- decorators: [
52
- (Story, context) => {
53
- const { initialPageNo, initialPageSize } = context.args;
54
- const query = `?pageNo=${initialPageNo}&pageSize=${initialPageSize}`;
55
- window.history.replaceState({}, '', query);
56
- return _jsx(Story, {});
57
- },
58
- ],
59
47
  tags: ['autodocs'],
60
48
  render: () => _jsx(Table, { id: "example-table", data: data, columns: columns }),
61
49
  };
@@ -279,12 +267,73 @@ export const ServerSideSearch = {
279
267
  return (_jsxs("div", { className: "space-y-4", children: [_jsx("div", { style: { width: '300px' }, children: _jsx(SearchInput, { onSearch: (q) => setQuery(q), onClear: () => setQuery(''), placeholder: "Search users..." }) }), _jsx(Table, { id: "server-search-table", data: data, columns: columns, paginationMode: "server", totalServerRows: 100, onRefetch: fetchData, isLoading: isLoading, isSearchActive: query.trim().length >= 3 })] }));
280
268
  },
281
269
  };
282
- export const WithQueryParams = {
270
+ // Note: Does not update state search params
271
+ export const ClientPaginationWithQueryParams = {
283
272
  args: {
284
273
  initialPageNo: 2,
285
274
  initialPageSize: 5,
286
275
  },
276
+ decorators: [
277
+ (Story, context) => {
278
+ const { initialPageNo, initialPageSize } = context.args;
279
+ const query = `?clients_pageNo=${initialPageNo}&clients_pageSize=${initialPageSize}`;
280
+ window.history.replaceState({}, '', query);
281
+ return _jsx(Story, {});
282
+ },
283
+ ],
284
+ render: () => (_jsxs(_Fragment, { children: [_jsx(CurrentUrl, {}), _jsx(Table, { id: "clients", data: data, columns: columns, paginationMode: "client", showHeader: true })] })),
285
+ };
286
+ // Note: Does not update state search params
287
+ export const ServerPaginationWithQueryParams = {
288
+ args: {
289
+ initialPageNo: 2,
290
+ initialPageSize: 5,
291
+ },
292
+ decorators: [
293
+ (Story, context) => {
294
+ const { initialPageNo, initialPageSize } = context.args;
295
+ const query = `?clients_pageNo=${initialPageNo}&clients_pageSize=${initialPageSize}`;
296
+ window.history.replaceState({}, '', query);
297
+ return _jsx(Story, {});
298
+ },
299
+ ],
300
+ render: () => {
301
+ const [data, setData] = useState([]);
302
+ const [isLoading, setIsLoading] = useState(false);
303
+ const fetchData = useCallback(async (pageIndex, pageSize) => {
304
+ setIsLoading(true);
305
+ const results = await fetchServerData(pageIndex, pageSize);
306
+ setData(results);
307
+ setIsLoading(false);
308
+ }, []);
309
+ return (_jsxs(_Fragment, { children: [_jsx(CurrentUrl, {}), _jsx(Table, { id: "clients", data: data, columns: columns, paginationMode: "server", totalServerRows: 100, onRefetch: fetchData, isLoading: isLoading, showHeader: true })] }));
310
+ },
311
+ };
312
+ // Note: Does not update state search params
313
+ export const TwoTablesWithQueryParams = {
314
+ args: {
315
+ clientsInitialPageNo: 2,
316
+ clientsInitialPageSize: 5,
317
+ usersInitialPageNo: 3,
318
+ usersInitialPageSize: 10,
319
+ },
320
+ decorators: [
321
+ (Story, context) => {
322
+ const { clientsInitialPageNo, clientsInitialPageSize, usersInitialPageNo, usersInitialPageSize } = context.args;
323
+ const query = `?clients_pageNo=${clientsInitialPageNo}&clients_pageSize=${clientsInitialPageSize}&users_pageNo=${usersInitialPageNo}&users_pageSize=${usersInitialPageSize}`;
324
+ window.history.replaceState({}, '', query);
325
+ return _jsx(Story, {});
326
+ },
327
+ ],
287
328
  render: () => {
288
- return (_jsxs(_Fragment, { children: [_jsx(CurrentUrl, {}), _jsx(Table, { id: "url-sync-table", data: data, columns: columns, paginationMode: "client", showHeader: true })] }));
329
+ const [usersData, setUsersData] = useState([]);
330
+ const [isLoadingUsers, setIsLoadingUsers] = useState(false);
331
+ const fetchUsersData = useCallback(async (pageIndex, pageSize) => {
332
+ setIsLoadingUsers(true);
333
+ const results = await fetchServerData(pageIndex, pageSize);
334
+ setUsersData(results);
335
+ setIsLoadingUsers(false);
336
+ }, []);
337
+ return (_jsxs(_Fragment, { children: [_jsx(CurrentUrl, {}), _jsxs("div", { style: { display: 'flex', gap: '2rem' }, children: [_jsx(Table, { id: "clients", data: data, columns: columns, paginationMode: "client", showHeader: true }), _jsx(Table, { id: "users", data: usersData, columns: columns, paginationMode: "server", totalServerRows: 100, onRefetch: fetchUsersData, isLoading: isLoadingUsers, showHeader: true })] })] }));
289
338
  },
290
339
  };
@@ -37,8 +37,4 @@ export interface TableProps<T> {
37
37
  grouping?: string[];
38
38
  isSearchActive?: boolean;
39
39
  noSearchResultsMessage?: string;
40
- queryParamKeys?: {
41
- pageSize?: string;
42
- pageNo?: string;
43
- };
44
40
  }
@@ -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, sorting, onSortingChange, onSelectionChange, enableRowSelection, rowsPerPage, isMultiRowSelection, selectedRowIds, rowIdKey, totalServerRows, onRefetch, showHeader, grouping, queryParamKeys, }: TableProps<T>): {
5
+ }>({ id, data, columns, checkboxSelection, checkboxPosition, paginationMode, sorting, onSortingChange, onSelectionChange, enableRowSelection, rowsPerPage, isMultiRowSelection, selectedRowIds, rowIdKey, totalServerRows, onRefetch, showHeader, grouping, }: TableProps<T>): {
6
6
  pageIndex: number;
7
7
  pageSize: number;
8
8
  canNextPage: boolean;
@@ -3,8 +3,7 @@ import { useReactTable, getCoreRowModel, getSortedRowModel, getPaginationRowMode
3
3
  import { applyFlexSizes, getCheckboxSelectionColumn } from '../table.helpers';
4
4
  import { usePaginationController } from './usePaginationController';
5
5
  import { useUrlPaginationSync } from './useUrlPaginationSync';
6
- export function useTableController({ data, columns, checkboxSelection, checkboxPosition = 'start', paginationMode = 'client', sorting, onSortingChange, onSelectionChange, enableRowSelection, rowsPerPage = 10, isMultiRowSelection = true, selectedRowIds, rowIdKey = 'id', totalServerRows, onRefetch, showHeader = true, grouping, queryParamKeys, }) {
7
- var _a;
6
+ export function useTableController({ id, data, columns, checkboxSelection, checkboxPosition = 'start', paginationMode = 'client', sorting, onSortingChange, onSelectionChange, enableRowSelection, rowsPerPage = 10, isMultiRowSelection = true, selectedRowIds, rowIdKey = 'id', totalServerRows, onRefetch, showHeader = true, grouping, }) {
8
7
  const safeData = Array.isArray(data) ? data : [];
9
8
  const stableGrouping = useMemo(() => grouping !== null && grouping !== void 0 ? grouping : [], [grouping]);
10
9
  const columnVisibility = useMemo(() => {
@@ -21,17 +20,9 @@ export function useTableController({ data, columns, checkboxSelection, checkboxP
21
20
  }
22
21
  return {};
23
22
  });
24
- const { pageIndex, setPageIndex, pageSize, setPageSize } = useUrlPaginationSync(rowsPerPage, queryParamKeys);
25
- const paginatedData = useMemo(() => {
26
- if (paginationMode === 'server') {
27
- return safeData;
28
- }
29
- const start = pageIndex * pageSize;
30
- const end = start + pageSize;
31
- return safeData.slice(start, end);
32
- }, [safeData, pageIndex, pageSize, paginationMode]);
23
+ const { pageIndex, setPageIndex, pageSize, setPageSize } = useUrlPaginationSync(rowsPerPage, id);
33
24
  const table = useReactTable({
34
- data: paginatedData,
25
+ data: safeData,
35
26
  columns: tableColumns,
36
27
  getRowId: (row) => String(row[rowIdKey]),
37
28
  state: {
@@ -43,12 +34,10 @@ export function useTableController({ data, columns, checkboxSelection, checkboxP
43
34
  },
44
35
  getSortedRowModel: getSortedRowModel(),
45
36
  getCoreRowModel: getCoreRowModel(),
46
- getPaginationRowModel: paginationMode !== 'server' ? getPaginationRowModel() : undefined,
47
- manualPagination: true,
37
+ getPaginationRowModel: paginationMode === 'client' ? getPaginationRowModel() : undefined,
38
+ manualPagination: paginationMode === 'server',
48
39
  autoResetPageIndex: false,
49
- pageCount: paginationMode === 'server'
50
- ? Math.ceil((totalServerRows !== null && totalServerRows !== void 0 ? totalServerRows : 0) / pageSize)
51
- : Math.ceil(((_a = safeData === null || safeData === void 0 ? void 0 : safeData.length) !== null && _a !== void 0 ? _a : 0) / pageSize),
40
+ pageCount: paginationMode === 'server' ? Math.ceil((totalServerRows !== null && totalServerRows !== void 0 ? totalServerRows : 0) / pageSize) : undefined,
52
41
  onPaginationChange: (updater) => {
53
42
  const next = typeof updater === 'function' ? updater({ pageIndex, pageSize }) : updater;
54
43
  setPageIndex(next.pageIndex);
@@ -1,11 +1,6 @@
1
- interface QueryParamKeys {
2
- pageSize?: string;
3
- pageNo?: string;
4
- }
5
- export declare function useUrlPaginationSync(rowsPerPage: number, queryParamKeys?: QueryParamKeys): {
1
+ export declare function useUrlPaginationSync(rowsPerPage: number, tableId: string): {
6
2
  pageIndex: number;
7
3
  setPageIndex: import("react").Dispatch<import("react").SetStateAction<number>>;
8
4
  pageSize: number;
9
5
  setPageSize: import("react").Dispatch<import("react").SetStateAction<number>>;
10
6
  };
11
- export {};
@@ -1,18 +1,12 @@
1
- import { useEffect, useState } from 'react';
1
+ import { useState } from 'react';
2
2
  import { useSearchParams } from 'react-router-dom';
3
- export function useUrlPaginationSync(rowsPerPage, queryParamKeys = {}) {
4
- const [searchParams, setSearchParams] = useSearchParams();
5
- const pageSizeKey = queryParamKeys.pageSize || 'pageSize';
6
- const pageNoKey = queryParamKeys.pageNo || 'pageNo';
3
+ export function useUrlPaginationSync(rowsPerPage, tableId) {
4
+ const [searchParams] = useSearchParams();
5
+ const pageSizeKey = `${tableId}_pageSize`;
6
+ const pageNoKey = `${tableId}_pageNo`;
7
7
  const initialPageIndex = Math.max(parseInt(searchParams.get(pageNoKey) || '1') - 1, 0);
8
8
  const initialPageSize = Math.max(parseInt(searchParams.get(pageSizeKey) || String(rowsPerPage !== null && rowsPerPage !== void 0 ? rowsPerPage : 10)), 1);
9
9
  const [pageIndex, setPageIndex] = useState(initialPageIndex);
10
10
  const [pageSize, setPageSize] = useState(initialPageSize);
11
- useEffect(() => {
12
- const newParams = new URLSearchParams(searchParams);
13
- newParams.set(pageNoKey, String(pageIndex + 1));
14
- newParams.set(pageSizeKey, String(pageSize));
15
- setSearchParams(newParams, { replace: true });
16
- }, [pageIndex, pageSize]);
17
11
  return { pageIndex, setPageIndex, pageSize, setPageSize };
18
12
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eml-payments/ui-kit",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "private": false,
5
5
  "description": "ARLO UIKit",
6
6
  "homepage": "https://github.com/EML-Payments/arlo.npm.uikit#readme",