@popmenu/table 0.22.0 → 0.25.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.
@@ -0,0 +1,3 @@
1
+ /// <reference types="react" />
2
+ import { GlobalFilterProps } from './GlobalFilterProps';
3
+ export declare const GlobalFilter: (props: GlobalFilterProps) => JSX.Element;
@@ -0,0 +1,7 @@
1
+ import { OutlinedTextFieldProps } from '@material-ui/core/TextField';
2
+ export interface GlobalFilterProps extends Omit<OutlinedTextFieldProps, 'variant'> {
3
+ /** the value of global filter from table instance */
4
+ value: string;
5
+ /** the setter for value of global filter from table instance */
6
+ setValue: (value: string) => void;
7
+ }
@@ -0,0 +1,2 @@
1
+ export { GlobalFilter } from './GlobalFilter';
2
+ export { GlobalFilterProps } from './GlobalFilterProps';
@@ -1,5 +1,6 @@
1
1
  import React from 'react';
2
2
  import { Cell, Column, TableInstance, UsePaginationOptions } from 'react-table';
3
+ import { GlobalFilterProps } from '../GlobalFilter';
3
4
  export interface TableProps {
4
5
  /** Configurations for each individual column. */
5
6
  columns: Array<Column & {
@@ -17,6 +18,8 @@ export interface TableProps {
17
18
  fullHeight?: boolean;
18
19
  /** Styles the table to take up its containers full width. */
19
20
  fullWidth?: boolean;
21
+ /** Enables global filter as search. */
22
+ searchable?: boolean;
20
23
  /** Enables sortability for columns. */
21
24
  sortable?: boolean;
22
25
  /** Sets loading state. When true, Will render a CircularProgress when true and not rows. */
@@ -25,6 +28,8 @@ export interface TableProps {
25
28
  emptyMessage?: string;
26
29
  /** An image to be displayed with the empty message. */
27
30
  EmptyImage?: () => React.ReactNode;
31
+ /** Props applied to the global search input component. Extends [TextField props](https://v4.mui.com/api/text-field/). */
32
+ GlobalFilterProps?: Omit<GlobalFilterProps, 'value' | 'setValue'>;
28
33
  /** Callback for managing pagination events. */
29
34
  onPaginate?: (pageIndex: number, instance: TableInstance) => void;
30
35
  /** Callback for managing pagination events. */
package/build/index.es.js CHANGED
@@ -3,12 +3,12 @@ import React__default, { forwardRef, createElement, useMemo, useState } from 're
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';
6
+ import { useGlobalFilter } from 'react-table/src/plugin-hooks/useGlobalFilter.js';
6
7
  import { Pagination } from '@material-ui/lab';
7
8
  import '@material-ui/lab/Alert';
8
- import { makeStyles, Paper as Paper$1, Link as Link$1, Box, useTheme as useTheme$1 } from '@material-ui/core';
9
+ import { makeStyles, Typography as Typography$1, Paper as Paper$1, Link as Link$1, Box, useTheme as useTheme$1, TextField, InputAdornment } from '@material-ui/core';
9
10
  import '@material-ui/core/AppBar';
10
11
  import MuiIconButton from '@material-ui/core/IconButton';
11
- import MuiTypography from '@material-ui/core/Typography';
12
12
  import { makeStyles as makeStyles$1, useTheme } from '@material-ui/core/styles';
13
13
  import MuiAvatar from '@material-ui/core/Avatar';
14
14
  import '@material-ui/core/Badge';
@@ -137,12 +137,12 @@ function __rest(s, e) {
137
137
  return t;
138
138
  }
139
139
 
140
- var _path$3k;
140
+ var _path$3l;
141
141
 
142
- function _extends$3B() { _extends$3B = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends$3B.apply(this, arguments); }
142
+ function _extends$3C() { _extends$3C = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends$3C.apply(this, arguments); }
143
143
 
144
144
  function SvgUser(props) {
145
- return /*#__PURE__*/React.createElement("svg", _extends$3B({
145
+ return /*#__PURE__*/React.createElement("svg", _extends$3C({
146
146
  viewBox: "0 0 16 16",
147
147
  strokeLinecap: "round",
148
148
  strokeLinejoin: "round",
@@ -150,7 +150,7 @@ function SvgUser(props) {
150
150
  width: "1em",
151
151
  height: "1em",
152
152
  fill: "none"
153
- }, props), _path$3k || (_path$3k = /*#__PURE__*/React.createElement("path", {
153
+ }, props), _path$3l || (_path$3l = /*#__PURE__*/React.createElement("path", {
154
154
  d: "M13.333 14v-1.333A2.667 2.667 0 0010.667 10H5.333a2.667 2.667 0 00-2.666 2.667V14M8 7.333A2.667 2.667 0 108 2a2.667 2.667 0 000 5.333z"
155
155
  })));
156
156
  }
@@ -277,29 +277,29 @@ IconButton.defaultProps = {
277
277
  color: 'secondary',
278
278
  };
279
279
 
280
- var useTypographyStyles = makeStyles$1(function () {
281
- var _a;
282
- return ({
283
- root: (_a = {},
284
- _a["& ." + iconStaticClassName] = {
285
- position: 'relative',
286
- top: '0.125em',
287
- padding: '0 0.125em',
288
- boxSizing: 'content-box',
289
- },
290
- _a),
291
- caption: {
292
- display: 'inline',
293
- },
294
- overline: {
295
- display: 'inline',
296
- },
297
- });
298
- });
280
+ var useTypographyStyles = makeStyles$1(function () { return ({
281
+ root: function (_a) {
282
+ var _b;
283
+ var weight = _a.weight;
284
+ return (__assign((_b = {}, _b["& ." + iconStaticClassName] = {
285
+ position: 'relative',
286
+ top: '0.125em',
287
+ padding: '0 0.125em',
288
+ boxSizing: 'content-box',
289
+ }, _b), (weight && { fontWeight: (weight === 'semi-bold' && 600) || (weight === 'bold' && 700) || 400 })));
290
+ },
291
+ caption: {
292
+ display: 'inline',
293
+ },
294
+ overline: {
295
+ display: 'inline',
296
+ },
297
+ }); });
299
298
 
300
299
  var Typography = forwardRef(function (props, ref) {
300
+ props.weight; var restProps = __rest(props, ["weight"]);
301
301
  var classes = useTypographyStyles(props);
302
- return React__default.createElement(MuiTypography, __assign({ ref: ref, classes: classes }, props));
302
+ return React__default.createElement(Typography$1, __assign({ ref: ref, classes: classes }, restProps));
303
303
  });
304
304
  Typography.displayName = 'Typography';
305
305
 
@@ -715,12 +715,12 @@ var TableHeader = function (props) {
715
715
  return React__default.createElement("thead", __assign$1({}, restProps), children);
716
716
  };
717
717
 
718
- var _path$3K;
718
+ var _path$3L;
719
719
 
720
- function _extends$3$() { _extends$3$ = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends$3$.apply(this, arguments); }
720
+ function _extends$40() { _extends$40 = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends$40.apply(this, arguments); }
721
721
 
722
722
  function SvgMore(props) {
723
- return /*#__PURE__*/React.createElement("svg", _extends$3$({
723
+ return /*#__PURE__*/React.createElement("svg", _extends$40({
724
724
  viewBox: "0 0 16 16",
725
725
  strokeLinecap: "round",
726
726
  strokeLinejoin: "round",
@@ -728,17 +728,35 @@ function SvgMore(props) {
728
728
  width: "1em",
729
729
  height: "1em",
730
730
  fill: "none"
731
- }, props), _path$3K || (_path$3K = /*#__PURE__*/React.createElement("path", {
731
+ }, props), _path$3L || (_path$3L = /*#__PURE__*/React.createElement("path", {
732
732
  d: "M8 8.667a.667.667 0 100-1.334.667.667 0 000 1.334zM8 4a.667.667 0 100-1.333A.667.667 0 008 4zm0 9.333A.667.667 0 108 12a.667.667 0 000 1.333z"
733
733
  })));
734
734
  }
735
735
 
736
- var _path$2X;
736
+ var _path$3B;
737
737
 
738
- function _extends$3b() { _extends$3b = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends$3b.apply(this, arguments); }
738
+ function _extends$3S() { _extends$3S = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends$3S.apply(this, arguments); }
739
+
740
+ function SvgSearch(props) {
741
+ return /*#__PURE__*/React.createElement("svg", _extends$3S({
742
+ viewBox: "0 0 16 16",
743
+ strokeLinecap: "round",
744
+ strokeLinejoin: "round",
745
+ stroke: "currentColor",
746
+ width: "1em",
747
+ height: "1em",
748
+ fill: "none"
749
+ }, props), _path$3B || (_path$3B = /*#__PURE__*/React.createElement("path", {
750
+ d: "M7.333 12.667A5.333 5.333 0 107.333 2a5.333 5.333 0 000 10.667zM14 14l-2.9-2.9"
751
+ })));
752
+ }
753
+
754
+ var _path$2Y;
755
+
756
+ function _extends$3c() { _extends$3c = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends$3c.apply(this, arguments); }
739
757
 
740
758
  function SvgChevronDown(props) {
741
- return /*#__PURE__*/React.createElement("svg", _extends$3b({
759
+ return /*#__PURE__*/React.createElement("svg", _extends$3c({
742
760
  viewBox: "0 0 16 16",
743
761
  strokeLinecap: "round",
744
762
  strokeLinejoin: "round",
@@ -746,17 +764,17 @@ function SvgChevronDown(props) {
746
764
  width: "1em",
747
765
  height: "1em",
748
766
  fill: "none"
749
- }, props), _path$2X || (_path$2X = /*#__PURE__*/React.createElement("path", {
767
+ }, props), _path$2Y || (_path$2Y = /*#__PURE__*/React.createElement("path", {
750
768
  d: "M4 6l4 4 4-4"
751
769
  })));
752
770
  }
753
771
 
754
- var _path$2W;
772
+ var _path$2X;
755
773
 
756
- function _extends$3a() { _extends$3a = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends$3a.apply(this, arguments); }
774
+ function _extends$3b() { _extends$3b = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends$3b.apply(this, arguments); }
757
775
 
758
776
  function SvgChevronUp(props) {
759
- return /*#__PURE__*/React.createElement("svg", _extends$3a({
777
+ return /*#__PURE__*/React.createElement("svg", _extends$3b({
760
778
  viewBox: "0 0 16 16",
761
779
  strokeLinecap: "round",
762
780
  strokeLinejoin: "round",
@@ -764,7 +782,7 @@ function SvgChevronUp(props) {
764
782
  width: "1em",
765
783
  height: "1em",
766
784
  fill: "none"
767
- }, props), _path$2W || (_path$2W = /*#__PURE__*/React.createElement("path", {
785
+ }, props), _path$2X || (_path$2X = /*#__PURE__*/React.createElement("path", {
768
786
  d: "M12 10L8 6l-4 4"
769
787
  })));
770
788
  }
@@ -856,28 +874,6 @@ var TableCell = function (props) {
856
874
  return (React__default.createElement("td", __assign$1({ className: root }, restProps), children));
857
875
  };
858
876
 
859
- var useTableStyles = makeStyles(function (_a) {
860
- var typography = _a.typography;
861
- return ({
862
- root: function (_a) {
863
- var fullWidth = _a.fullWidth;
864
- return ({
865
- display: fullWidth ? 'flex' : 'inline-flex',
866
- flexDirection: 'column',
867
- });
868
- },
869
- table: function (_a) {
870
- var fullWidth = _a.fullWidth;
871
- return (__assign$1(__assign$1({ borderCollapse: 'collapse' }, (fullWidth && { width: '100%' })), typography[tableConstants.tableTypography]));
872
- },
873
- tableWrap: function () { return ({
874
- maxWidth: '100%',
875
- overflowX: 'auto',
876
- overflowY: 'hidden',
877
- }); },
878
- });
879
- });
880
-
881
877
  var filterProps = function (props) {
882
878
  props.icon; props.action; var filteredProps = __rest$1(props, ["icon", "action"]);
883
879
  return filteredProps;
@@ -931,17 +927,54 @@ var makeColumns = function (config) {
931
927
  return memoizedColumns;
932
928
  };
933
929
 
930
+ var GlobalFilter = function (props) {
931
+ var value = props.value, setValue = props.setValue, restProps = __rest$1(props, ["value", "setValue"]);
932
+ var onChange = restProps.onChange, InputProps = restProps.InputProps, cleanRestProps = __rest$1(restProps, ["onChange", "InputProps"]);
933
+ var handleChange = function (e) {
934
+ setValue(e.target.value);
935
+ if (onChange) {
936
+ onChange(e);
937
+ }
938
+ };
939
+ return (React__default.createElement(TextField, __assign$1({ variant: "outlined", value: value, onChange: handleChange, InputProps: __assign$1({ startAdornment: (React__default.createElement(InputAdornment, { position: "start" },
940
+ React__default.createElement(Icon, { icon: SvgSearch }))) }, InputProps) }, cleanRestProps)));
941
+ };
942
+
943
+ var useTableStyles = makeStyles(function (_a) {
944
+ var typography = _a.typography;
945
+ return ({
946
+ root: function (_a) {
947
+ var fullWidth = _a.fullWidth;
948
+ return ({
949
+ display: fullWidth ? 'flex' : 'inline-flex',
950
+ flexDirection: 'column',
951
+ });
952
+ },
953
+ table: function (_a) {
954
+ var fullWidth = _a.fullWidth;
955
+ return (__assign$1(__assign$1({ borderCollapse: 'collapse' }, (fullWidth && { width: '100%' })), typography[tableConstants.tableTypography]));
956
+ },
957
+ tableWrap: function () { return ({
958
+ maxWidth: '100%',
959
+ overflowX: 'auto',
960
+ overflowY: 'hidden',
961
+ }); },
962
+ });
963
+ });
964
+
934
965
  /* eslint-disable react/jsx-key */
935
966
  var Table = function (props) {
967
+ // Setup
936
968
  var actionColumnId = 'table-actions-column';
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;
969
+ 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, GlobalFilterProps = props.GlobalFilterProps, _a = props.paginationOptions, paginationOptions = _a === void 0 ? {} : _a, _b = props.tableOptions, tableOptions = _b === void 0 ? {} : _b, searchable = props.searchable, onPaginate = props.onPaginate;
938
970
  var theme = useTheme$1();
939
971
  var classes = useTableStyles(props);
940
972
  var columns = useMemo(function () { return makeColumns({ columns: rawColumns, actions: actions, actionColumnId: actionColumnId }); }, [rawColumns, actions]);
941
973
  var initialPaginationState = paginationOptions.initialState, restPaginationOptions = __rest$1(paginationOptions, ["initialState"]);
942
974
  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;
975
+ var instance = useTable(__assign$1(__assign$1({ columns: columns, data: data, disableSortBy: !sortable, initialState: initialState }, restPaginationOptions), tableOptions), useGlobalFilter, useSortBy, usePagination);
976
+ 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, state = instance.state, setGlobalFilter = instance.setGlobalFilter;
977
+ // Aliases
945
978
  var isPaginated = pageCount > 1;
946
979
  var isEmpty = rows.length === 0;
947
980
  var lineHeightRem = parseFloat(theme.typography[tableConstants.tableTypography].lineHeight);
@@ -952,6 +985,7 @@ var Table = function (props) {
952
985
  var bodyHeight = instance.state.pageSize * cellHeight;
953
986
  var footerHeight = 88;
954
987
  var minHeight = headerHeight + bodyHeight + footerHeight;
988
+ // Handlers
955
989
  var handlePagination = function (_e, pageNumber) {
956
990
  var pageIndex = pageNumber - 1;
957
991
  if (onPaginate) {
@@ -962,6 +996,8 @@ var Table = function (props) {
962
996
  }
963
997
  };
964
998
  return (React__default.createElement(Box, { className: classes.root, minHeight: fullHeight ? minHeight : 'unset' },
999
+ searchable && (React__default.createElement(Box, null,
1000
+ React__default.createElement(GlobalFilter, __assign$1({ value: state.globalFilter, setValue: setGlobalFilter }, GlobalFilterProps)))),
965
1001
  React__default.createElement(Box, { className: classes.tableWrap },
966
1002
  React__default.createElement("table", __assign$1({ className: classes.table }, getTableProps()),
967
1003
  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'))); }))); })),