@popmenu/table 0.19.5 → 0.20.0

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,5 +1,5 @@
1
1
  import React from 'react';
2
- import { Cell, Column } from 'react-table';
2
+ import { Cell, Column, TableInstance, UsePaginationOptions } from 'react-table';
3
3
  export interface TableProps {
4
4
  /** Configurations for each individual column. */
5
5
  columns: Array<Column & {
@@ -13,8 +13,8 @@ export interface TableProps {
13
13
  label: string;
14
14
  action: (cell: Cell) => void;
15
15
  }>;
16
- /** Styles the table to not shrink below the height of a full page. */
17
- enableMinHeight?: boolean;
16
+ /** Styles the table to maintain the height of a fully populated page (entries in a table, not the window). */
17
+ fullHeight?: boolean;
18
18
  /** Styles the table to take up its containers full width. */
19
19
  fullWidth?: boolean;
20
20
  /** Enables sortability for columns. */
@@ -25,4 +25,15 @@ export interface TableProps {
25
25
  emptyMessage?: string;
26
26
  /** An image to be displayed with the empty message. */
27
27
  EmptyImage?: () => React.ReactNode;
28
+ /** Callback for managing pagination events. */
29
+ onPaginate?: (pageIndex: number, instance: TableInstance) => void;
30
+ /** Callback for managing pagination events. */
31
+ paginationOptions?: UsePaginationOptions<Record<string, unknown>> & Partial<{
32
+ initialState: Partial<{
33
+ pageSize: number;
34
+ pageIndex: number;
35
+ }>;
36
+ }>;
37
+ /** Added to the main parameter of the useTable hook. */
38
+ tableOptions?: Record<string, unknown>;
28
39
  }
@@ -0,0 +1 @@
1
+ export { useManualPagination } from './useManualPagination';
@@ -0,0 +1 @@
1
+ export { useManualPagination } from './useManualPagination';
@@ -0,0 +1,5 @@
1
+ import { UseManualPaginationConfig, UseManualPaginationResult } from './useManualPaginationTypes';
2
+ /**
3
+ * Makes props for the Table component that will enable manual pagination and also provides page state.
4
+ */
5
+ export declare const useManualPagination: (config: UseManualPaginationConfig) => UseManualPaginationResult;
@@ -0,0 +1,52 @@
1
+ import { TableInstance } from 'react-table';
2
+ interface PaginationEvent {
3
+ /** Index of the page a user has selected. */
4
+ index: number;
5
+ /** The entire table instance by react-table. */
6
+ instance: TableInstance;
7
+ }
8
+ export interface UseManualPaginationConfig {
9
+ /** The total number of records available. */
10
+ total: number;
11
+ /** Sets the default page to show when the table is first presented. */
12
+ initialPageIndex?: number;
13
+ /** Sets the default page size to show when the table is first presented. */
14
+ initialPageSize?: number;
15
+ /** Callback triggered when a pagination even occurs. setPageIndex, which is the second arg of the callback,
16
+ * is required to be called manually. */
17
+ onPaginate?: (paginationEvent: PaginationEvent, setPageIndex: (index: number) => void) => void;
18
+ }
19
+ interface ManualPaginationProps {
20
+ /** A callback for pagination events. Should map to the Table component's onPaginate prop */
21
+ onPaginate: (index: number, instance: TableInstance) => void;
22
+ /** Additional configuration for the Table component. Should map to the Table component's tableOptions prop */
23
+ tableOptions: {
24
+ useControlledState: (state: Record<string, unknown>) => Record<string, unknown>;
25
+ };
26
+ /** Additional configuration for the Table component. Should map to the Table component's paginationOptions prop */
27
+ paginationOptions: {
28
+ initialState: {
29
+ pageIndex: number;
30
+ pageSize: number;
31
+ };
32
+ manualPagination: boolean;
33
+ pageCount: number;
34
+ };
35
+ }
36
+ interface PageState {
37
+ /** The number of rows a Table shows per page */
38
+ pageSize: number;
39
+ /** The page index a Table will show */
40
+ pageIndex: number;
41
+ /** Updates pageSize */
42
+ setPageSize: (size: number) => void;
43
+ /** Updates pageIndex */
44
+ setPageIndex: (index: number) => void;
45
+ }
46
+ export interface UseManualPaginationResult {
47
+ /** A collection of props for the Table component that will enable manual pagination. Apply with spread operator. */
48
+ manualPaginationProps: ManualPaginationProps;
49
+ /** Stateful values for controlling pages of a Table */
50
+ pageState: PageState;
51
+ }
52
+ export {};
package/build/index.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from './components';
2
+ export * from './hooks';
package/build/index.es.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as React from 'react';
2
- import React__default, { forwardRef, createElement, useMemo } from 'react';
2
+ import React__default, { forwardRef, createElement, useMemo, useState } from 'react';
3
3
  import { useTable } from 'react-table/src/hooks/useTable.js';
4
4
  import { usePagination } from 'react-table/src/plugin-hooks/usePagination.js';
5
5
  import { useSortBy } from 'react-table/src/plugin-hooks/useSortBy.js';
@@ -810,9 +810,15 @@ var TableBody = function (props) {
810
810
  return React__default.createElement("tbody", __assign$1({}, restProps), children);
811
811
  };
812
812
 
813
+ var tableConstants = {
814
+ tableTypography: 'body1',
815
+ cellPaddingFactor: 2,
816
+ headerBorder: 2,
817
+ };
818
+
813
819
  var useTableCellStyles = makeStyles(function (theme) { return ({
814
820
  root: function () { return ({
815
- padding: theme.spacing(2),
821
+ padding: theme.spacing(tableConstants.cellPaddingFactor),
816
822
  }); },
817
823
  }); });
818
824
 
@@ -834,7 +840,7 @@ var useTableStyles = makeStyles(function (_a) {
834
840
  },
835
841
  table: function (_a) {
836
842
  var fullWidth = _a.fullWidth;
837
- return (__assign$1(__assign$1({ borderCollapse: 'collapse' }, (fullWidth && { width: '100%' })), typography.body1));
843
+ return (__assign$1(__assign$1({ borderCollapse: 'collapse' }, (fullWidth && { width: '100%' })), typography[tableConstants.tableTypography]));
838
844
  },
839
845
  tableWrap: function () { return ({
840
846
  maxWidth: '100%',
@@ -900,36 +906,92 @@ var makeColumns = function (config) {
900
906
  /* eslint-disable react/jsx-key */
901
907
  var Table = function (props) {
902
908
  var actionColumnId = 'table-actions-column';
903
- var rawColumns = props.columns, data = props.data, actions = props.actions, sortable = props.sortable, emptyMessage = props.emptyMessage, EmptyImage = props.EmptyImage, isLoading = props.loading, enableMinHeight = props.enableMinHeight;
909
+ var rawColumns = props.columns, data = props.data, actions = props.actions, sortable = props.sortable, emptyMessage = props.emptyMessage, EmptyImage = props.EmptyImage, isLoading = props.loading, fullHeight = props.fullHeight, _a = props.paginationOptions, paginationOptions = _a === void 0 ? {} : _a, _b = props.tableOptions, tableOptions = _b === void 0 ? {} : _b, onPaginate = props.onPaginate;
910
+ var theme = useTheme$1();
904
911
  var classes = useTableStyles(props);
905
912
  var columns = useMemo(function () { return makeColumns({ columns: rawColumns, actions: actions, actionColumnId: actionColumnId }); }, [rawColumns, actions]);
906
- var instance = useTable({ columns: columns, data: data, disableSortBy: !sortable }, useSortBy, usePagination);
907
- var getTableProps = instance.getTableProps, getTableBodyProps = instance.getTableBodyProps, headerGroups = instance.headerGroups, page = instance.page, prepareRow = instance.prepareRow, pageCount = instance.pageCount, gotoPage = instance.gotoPage, state = instance.state, rows = instance.rows;
913
+ var initialPaginationState = paginationOptions.initialState, restPaginationOptions = __rest$1(paginationOptions, ["initialState"]);
914
+ var initialState = __assign$1({}, initialPaginationState);
915
+ var instance = useTable(__assign$1(__assign$1({ columns: columns, data: data, disableSortBy: !sortable, initialState: initialState }, restPaginationOptions), tableOptions), useSortBy, usePagination);
916
+ var getTableProps = instance.getTableProps, getTableBodyProps = instance.getTableBodyProps, headerGroups = instance.headerGroups, page = instance.page, prepareRow = instance.prepareRow, pageCount = instance.pageCount, gotoPage = instance.gotoPage, rows = instance.rows;
908
917
  var isPaginated = pageCount > 1;
909
918
  var isEmpty = rows.length === 0;
919
+ var lineHeightRem = parseFloat(theme.typography[tableConstants.tableTypography].lineHeight);
920
+ var documentFontSize = parseFloat(getComputedStyle(document.documentElement).fontSize);
921
+ var typographyHeight = lineHeightRem * documentFontSize;
922
+ var cellHeight = typographyHeight + theme.spacing(tableConstants.cellPaddingFactor * 2);
923
+ var headerHeight = cellHeight + tableConstants.headerBorder;
924
+ var bodyHeight = instance.state.pageSize * cellHeight;
925
+ var footerHeight = 88;
926
+ var minHeight = headerHeight + bodyHeight + footerHeight;
910
927
  var handlePagination = function (_e, pageNumber) {
911
- gotoPage(pageNumber - 1);
928
+ var pageIndex = pageNumber - 1;
929
+ if (onPaginate) {
930
+ onPaginate(pageIndex, instance);
931
+ }
932
+ else {
933
+ gotoPage(pageIndex);
934
+ }
912
935
  };
913
- return (React__default.createElement(Box, { className: classes.root, minHeight: (isLoading || isEmpty) && enableMinHeight ? 618 : 'unset' },
914
- React__default.createElement(Box, { className: classes.tableWrap, minHeight: page.length > 0 && !isLoading && enableMinHeight ? 618 : 'unset' },
936
+ return (React__default.createElement(Box, { className: classes.root, minHeight: fullHeight ? minHeight : 'unset' },
937
+ React__default.createElement(Box, { className: classes.tableWrap },
915
938
  React__default.createElement("table", __assign$1({ className: classes.table }, getTableProps()),
916
939
  React__default.createElement(TableHeader, null, headerGroups.map(function (headerGroup) { return (React__default.createElement(TableHeaderRow, __assign$1({}, headerGroup.getHeaderGroupProps()), headerGroup.headers.map(function (column) { return (React__default.createElement(TableHeaderCell, __assign$1({}, column.getHeaderProps(column.getSortByToggleProps()), { column: __assign$1(__assign$1({}, column), { isActionColumn: column.id === actionColumnId }), tableProps: props }), column.render('Header'))); }))); })),
917
940
  !isEmpty && !isLoading && (React__default.createElement(TableBody, __assign$1({}, getTableBodyProps()), page.map(function (row) {
918
941
  prepareRow(row);
919
942
  return (React__default.createElement(TableRow, __assign$1({}, row.getRowProps()), row.cells.map(function (cell) { return (React__default.createElement(TableCell, __assign$1({}, cell.getCellProps()), cell.render('Cell'))); })));
920
943
  }))))),
921
- isEmpty && (React__default.createElement(Box, { display: "flex", flexDirection: "column", justifyContent: "center", alignItems: "center", p: 2, flexGrow: 1 },
944
+ isEmpty && !isLoading && (React__default.createElement(Box, { display: "flex", flexDirection: "column", justifyContent: "center", alignItems: "center", p: 2, flexGrow: 1 },
922
945
  EmptyImage && EmptyImage(),
923
946
  React__default.createElement(Typography, null, emptyMessage))),
924
947
  isLoading && (React__default.createElement(Box, { display: "flex", flexDirection: "column", justifyContent: "center", alignItems: "center", p: 2, flexGrow: 1 },
925
948
  React__default.createElement(CircularProgress, { size: 80, color: "inherit" }))),
926
- isPaginated && !isLoading && (React__default.createElement(TableFooter, null,
927
- React__default.createElement(Pagination, { count: pageCount, page: state.pageIndex + 1, onChange: handlePagination })))));
949
+ isPaginated && (React__default.createElement(TableFooter, null,
950
+ React__default.createElement(Pagination, { count: pageCount, page: instance.state.pageIndex + 1, onChange: handlePagination })))));
928
951
  };
929
952
  Table.defaultProps = {
930
953
  emptyMessage: 'No results.',
931
- enableMinHeight: true,
932
954
  };
933
955
 
934
- export { Table };
956
+ /**
957
+ * Makes props for the Table component that will enable manual pagination and also provides page state.
958
+ */
959
+ var useManualPagination = function (config) {
960
+ var _a = config.initialPageIndex, initialPageIndex = _a === void 0 ? 0 : _a, _b = config.initialPageSize, initialPageSize = _b === void 0 ? 10 : _b, total = config.total, onPaginate = config.onPaginate;
961
+ var _c = useState(0), pageIndex = _c[0], setPageIndex = _c[1];
962
+ var _d = useState(initialPageSize), pageSize = _d[0], setPageSize = _d[1];
963
+ var handlePagination = function (index, instance) {
964
+ if (onPaginate) {
965
+ onPaginate({ index: index, instance: instance }, setPageIndex);
966
+ }
967
+ else {
968
+ setPageIndex(index);
969
+ }
970
+ };
971
+ var manualPaginationProps = {
972
+ onPaginate: handlePagination,
973
+ tableOptions: {
974
+ useControlledState: function (state) {
975
+ return useMemo(function () { return (__assign$1(__assign$1({}, state), { pageIndex: pageIndex, pageSize: pageSize })); }, [pageIndex, pageSize]);
976
+ },
977
+ },
978
+ paginationOptions: {
979
+ initialState: { pageIndex: initialPageIndex, pageSize: pageSize },
980
+ manualPagination: true,
981
+ pageCount: Math.ceil(total / pageSize),
982
+ },
983
+ };
984
+ var pageState = {
985
+ pageIndex: pageIndex,
986
+ pageSize: pageSize,
987
+ setPageIndex: setPageIndex,
988
+ setPageSize: setPageSize,
989
+ };
990
+ return {
991
+ manualPaginationProps: manualPaginationProps,
992
+ pageState: pageState,
993
+ };
994
+ };
995
+
996
+ export { Table, useManualPagination };
935
997
  //# sourceMappingURL=index.es.js.map