@popmenu/table 0.19.5 → 0.22.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';
@@ -678,6 +678,34 @@ var Tooltip = forwardRef(function (props, ref) {
678
678
  });
679
679
  Tooltip.displayName = 'Tooltip';
680
680
 
681
+ makeStyles(function () { return ({
682
+ root: {
683
+ height: 56,
684
+ },
685
+ }); });
686
+ makeStyles(function (theme) { return ({
687
+ root: {
688
+ height: '100%',
689
+ width: 'min-content',
690
+ borderRadius: theme.spacing(1),
691
+ },
692
+ input: {
693
+ '&::-webkit-clear-button, &::-webkit-outer-spin-button, &::-webkit-inner-spin-button': {
694
+ display: 'none',
695
+ },
696
+ '-moz-appearance': 'textfield',
697
+ minWidth: 40,
698
+ padding: 0,
699
+ textAlign: 'center',
700
+ },
701
+ adornedStart: {
702
+ padding: 0,
703
+ },
704
+ adornedEnd: {
705
+ padding: 0,
706
+ },
707
+ }); });
708
+
681
709
  var TableFooter = function (props) {
682
710
  return (React__default.createElement(Box, { display: "flex", justifyContent: "center", alignItems: "end", paddingTop: 3, paddingBottom: 4, flexGrow: 1 }, props.children));
683
711
  };
@@ -810,9 +838,15 @@ var TableBody = function (props) {
810
838
  return React__default.createElement("tbody", __assign$1({}, restProps), children);
811
839
  };
812
840
 
841
+ var tableConstants = {
842
+ tableTypography: 'body1',
843
+ cellPaddingFactor: 2,
844
+ headerBorder: 2,
845
+ };
846
+
813
847
  var useTableCellStyles = makeStyles(function (theme) { return ({
814
848
  root: function () { return ({
815
- padding: theme.spacing(2),
849
+ padding: theme.spacing(tableConstants.cellPaddingFactor),
816
850
  }); },
817
851
  }); });
818
852
 
@@ -834,7 +868,7 @@ var useTableStyles = makeStyles(function (_a) {
834
868
  },
835
869
  table: function (_a) {
836
870
  var fullWidth = _a.fullWidth;
837
- return (__assign$1(__assign$1({ borderCollapse: 'collapse' }, (fullWidth && { width: '100%' })), typography.body1));
871
+ return (__assign$1(__assign$1({ borderCollapse: 'collapse' }, (fullWidth && { width: '100%' })), typography[tableConstants.tableTypography]));
838
872
  },
839
873
  tableWrap: function () { return ({
840
874
  maxWidth: '100%',
@@ -900,36 +934,92 @@ var makeColumns = function (config) {
900
934
  /* eslint-disable react/jsx-key */
901
935
  var Table = function (props) {
902
936
  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;
937
+ 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;
938
+ var theme = useTheme$1();
904
939
  var classes = useTableStyles(props);
905
940
  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;
941
+ var initialPaginationState = paginationOptions.initialState, restPaginationOptions = __rest$1(paginationOptions, ["initialState"]);
942
+ var initialState = __assign$1({}, initialPaginationState);
943
+ var instance = useTable(__assign$1(__assign$1({ columns: columns, data: data, disableSortBy: !sortable, initialState: initialState }, restPaginationOptions), tableOptions), useSortBy, usePagination);
944
+ 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
945
  var isPaginated = pageCount > 1;
909
946
  var isEmpty = rows.length === 0;
947
+ var lineHeightRem = parseFloat(theme.typography[tableConstants.tableTypography].lineHeight);
948
+ var documentFontSize = parseFloat(getComputedStyle(document.documentElement).fontSize);
949
+ var typographyHeight = lineHeightRem * documentFontSize;
950
+ var cellHeight = typographyHeight + theme.spacing(tableConstants.cellPaddingFactor * 2);
951
+ var headerHeight = cellHeight + tableConstants.headerBorder;
952
+ var bodyHeight = instance.state.pageSize * cellHeight;
953
+ var footerHeight = 88;
954
+ var minHeight = headerHeight + bodyHeight + footerHeight;
910
955
  var handlePagination = function (_e, pageNumber) {
911
- gotoPage(pageNumber - 1);
956
+ var pageIndex = pageNumber - 1;
957
+ if (onPaginate) {
958
+ onPaginate(pageIndex, instance);
959
+ }
960
+ else {
961
+ gotoPage(pageIndex);
962
+ }
912
963
  };
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' },
964
+ return (React__default.createElement(Box, { className: classes.root, minHeight: fullHeight ? minHeight : 'unset' },
965
+ React__default.createElement(Box, { className: classes.tableWrap },
915
966
  React__default.createElement("table", __assign$1({ className: classes.table }, getTableProps()),
916
967
  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
968
  !isEmpty && !isLoading && (React__default.createElement(TableBody, __assign$1({}, getTableBodyProps()), page.map(function (row) {
918
969
  prepareRow(row);
919
970
  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
971
  }))))),
921
- isEmpty && (React__default.createElement(Box, { display: "flex", flexDirection: "column", justifyContent: "center", alignItems: "center", p: 2, flexGrow: 1 },
972
+ isEmpty && !isLoading && (React__default.createElement(Box, { display: "flex", flexDirection: "column", justifyContent: "center", alignItems: "center", p: 2, flexGrow: 1 },
922
973
  EmptyImage && EmptyImage(),
923
974
  React__default.createElement(Typography, null, emptyMessage))),
924
975
  isLoading && (React__default.createElement(Box, { display: "flex", flexDirection: "column", justifyContent: "center", alignItems: "center", p: 2, flexGrow: 1 },
925
976
  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 })))));
977
+ isPaginated && (React__default.createElement(TableFooter, null,
978
+ React__default.createElement(Pagination, { count: pageCount, page: instance.state.pageIndex + 1, onChange: handlePagination })))));
928
979
  };
929
980
  Table.defaultProps = {
930
981
  emptyMessage: 'No results.',
931
- enableMinHeight: true,
932
982
  };
933
983
 
934
- export { Table };
984
+ /**
985
+ * Makes props for the Table component that will enable manual pagination and also provides page state.
986
+ */
987
+ var useManualPagination = function (config) {
988
+ 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;
989
+ var _c = useState(0), pageIndex = _c[0], setPageIndex = _c[1];
990
+ var _d = useState(initialPageSize), pageSize = _d[0], setPageSize = _d[1];
991
+ var handlePagination = function (index, instance) {
992
+ if (onPaginate) {
993
+ onPaginate({ index: index, instance: instance }, setPageIndex);
994
+ }
995
+ else {
996
+ setPageIndex(index);
997
+ }
998
+ };
999
+ var manualPaginationProps = {
1000
+ onPaginate: handlePagination,
1001
+ tableOptions: {
1002
+ useControlledState: function (state) {
1003
+ return useMemo(function () { return (__assign$1(__assign$1({}, state), { pageIndex: pageIndex, pageSize: pageSize })); }, [pageIndex, pageSize]);
1004
+ },
1005
+ },
1006
+ paginationOptions: {
1007
+ initialState: { pageIndex: initialPageIndex, pageSize: pageSize },
1008
+ manualPagination: true,
1009
+ pageCount: Math.ceil(total / pageSize),
1010
+ },
1011
+ };
1012
+ var pageState = {
1013
+ pageIndex: pageIndex,
1014
+ pageSize: pageSize,
1015
+ setPageIndex: setPageIndex,
1016
+ setPageSize: setPageSize,
1017
+ };
1018
+ return {
1019
+ manualPaginationProps: manualPaginationProps,
1020
+ pageState: pageState,
1021
+ };
1022
+ };
1023
+
1024
+ export { Table, useManualPagination };
935
1025
  //# sourceMappingURL=index.es.js.map