@popmenu/table 0.22.0 → 0.23.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,9 +3,10 @@ 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, 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
12
  import MuiTypography from '@material-ui/core/Typography';
@@ -137,20 +138,20 @@ function __rest(s, e) {
137
138
  return t;
138
139
  }
139
140
 
140
- var _path$3k;
141
+ var _path$2O;
141
142
 
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); }
143
+ function _extends$32() { _extends$32 = 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$32.apply(this, arguments); }
143
144
 
144
145
  function SvgUser(props) {
145
- return /*#__PURE__*/React.createElement("svg", _extends$3B({
146
+ return /*#__PURE__*/React.createElement("svg", _extends$32({
146
147
  viewBox: "0 0 16 16",
147
148
  strokeLinecap: "round",
148
149
  strokeLinejoin: "round",
149
150
  stroke: "currentColor",
151
+ fill: "none",
150
152
  width: "1em",
151
- height: "1em",
152
- fill: "none"
153
- }, props), _path$3k || (_path$3k = /*#__PURE__*/React.createElement("path", {
153
+ height: "1em"
154
+ }, props), _path$2O || (_path$2O = /*#__PURE__*/React.createElement("path", {
154
155
  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
156
  })));
156
157
  }
@@ -715,57 +716,75 @@ var TableHeader = function (props) {
715
716
  return React__default.createElement("thead", __assign$1({}, restProps), children);
716
717
  };
717
718
 
718
- var _path$3K;
719
+ var _path$4w;
719
720
 
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); }
721
+ function _extends$4M() { _extends$4M = 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$4M.apply(this, arguments); }
721
722
 
722
- function SvgMore(props) {
723
- return /*#__PURE__*/React.createElement("svg", _extends$3$({
723
+ function SvgChevronDown(props) {
724
+ return /*#__PURE__*/React.createElement("svg", _extends$4M({
724
725
  viewBox: "0 0 16 16",
725
726
  strokeLinecap: "round",
726
727
  strokeLinejoin: "round",
727
728
  stroke: "currentColor",
729
+ fill: "none",
728
730
  width: "1em",
729
- height: "1em",
730
- fill: "none"
731
- }, props), _path$3K || (_path$3K = /*#__PURE__*/React.createElement("path", {
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"
731
+ height: "1em"
732
+ }, props), _path$4w || (_path$4w = /*#__PURE__*/React.createElement("path", {
733
+ d: "M4 6l4 4 4-4"
733
734
  })));
734
735
  }
735
736
 
736
- var _path$2X;
737
+ var _path$4v;
737
738
 
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); }
739
+ function _extends$4L() { _extends$4L = 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$4L.apply(this, arguments); }
739
740
 
740
- function SvgChevronDown(props) {
741
- return /*#__PURE__*/React.createElement("svg", _extends$3b({
741
+ function SvgChevronUp(props) {
742
+ return /*#__PURE__*/React.createElement("svg", _extends$4L({
742
743
  viewBox: "0 0 16 16",
743
744
  strokeLinecap: "round",
744
745
  strokeLinejoin: "round",
745
746
  stroke: "currentColor",
747
+ fill: "none",
746
748
  width: "1em",
747
- height: "1em",
748
- fill: "none"
749
- }, props), _path$2X || (_path$2X = /*#__PURE__*/React.createElement("path", {
750
- d: "M4 6l4 4 4-4"
749
+ height: "1em"
750
+ }, props), _path$4v || (_path$4v = /*#__PURE__*/React.createElement("path", {
751
+ d: "M12 10L8 6l-4 4"
751
752
  })));
752
753
  }
753
754
 
754
- var _path$2W;
755
+ var _path$3c;
755
756
 
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); }
757
+ 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); }
757
758
 
758
- function SvgChevronUp(props) {
759
- return /*#__PURE__*/React.createElement("svg", _extends$3a({
759
+ function SvgMore(props) {
760
+ return /*#__PURE__*/React.createElement("svg", _extends$3s({
760
761
  viewBox: "0 0 16 16",
761
762
  strokeLinecap: "round",
762
763
  strokeLinejoin: "round",
763
764
  stroke: "currentColor",
765
+ fill: "none",
764
766
  width: "1em",
765
- height: "1em",
766
- fill: "none"
767
- }, props), _path$2W || (_path$2W = /*#__PURE__*/React.createElement("path", {
768
- d: "M12 10L8 6l-4 4"
767
+ height: "1em"
768
+ }, props), _path$3c || (_path$3c = /*#__PURE__*/React.createElement("path", {
769
+ d: "M8 8.667a.667.667 0 100-1.334.667.667 0 000 1.334zM8 4a.667.667 0 100-1.334A.667.667 0 008 4zm0 9.333A.667.667 0 108 12a.667.667 0 000 1.333z"
770
+ })));
771
+ }
772
+
773
+ var _path$32;
774
+
775
+ function _extends$3i() { _extends$3i = 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$3i.apply(this, arguments); }
776
+
777
+ function SvgSearch(props) {
778
+ return /*#__PURE__*/React.createElement("svg", _extends$3i({
779
+ viewBox: "0 0 16 16",
780
+ strokeLinecap: "round",
781
+ strokeLinejoin: "round",
782
+ stroke: "currentColor",
783
+ fill: "none",
784
+ width: "1em",
785
+ height: "1em"
786
+ }, props), _path$32 || (_path$32 = /*#__PURE__*/React.createElement("path", {
787
+ d: "M7.333 12.667A5.333 5.333 0 107.333 2a5.333 5.333 0 000 10.667zM14 14l-2.9-2.9"
769
788
  })));
770
789
  }
771
790
 
@@ -856,28 +875,6 @@ var TableCell = function (props) {
856
875
  return (React__default.createElement("td", __assign$1({ className: root }, restProps), children));
857
876
  };
858
877
 
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
878
  var filterProps = function (props) {
882
879
  props.icon; props.action; var filteredProps = __rest$1(props, ["icon", "action"]);
883
880
  return filteredProps;
@@ -931,17 +928,54 @@ var makeColumns = function (config) {
931
928
  return memoizedColumns;
932
929
  };
933
930
 
931
+ var GlobalFilter = function (props) {
932
+ var value = props.value, setValue = props.setValue, restProps = __rest$1(props, ["value", "setValue"]);
933
+ var onChange = restProps.onChange, InputProps = restProps.InputProps, cleanRestProps = __rest$1(restProps, ["onChange", "InputProps"]);
934
+ var handleChange = function (e) {
935
+ setValue(e.target.value);
936
+ if (onChange) {
937
+ onChange(e);
938
+ }
939
+ };
940
+ return (React__default.createElement(TextField, __assign$1({ variant: "outlined", value: value, onChange: handleChange, InputProps: __assign$1({ startAdornment: (React__default.createElement(InputAdornment, { position: "start" },
941
+ React__default.createElement(Icon, { icon: SvgSearch }))) }, InputProps) }, cleanRestProps)));
942
+ };
943
+
944
+ var useTableStyles = makeStyles(function (_a) {
945
+ var typography = _a.typography;
946
+ return ({
947
+ root: function (_a) {
948
+ var fullWidth = _a.fullWidth;
949
+ return ({
950
+ display: fullWidth ? 'flex' : 'inline-flex',
951
+ flexDirection: 'column',
952
+ });
953
+ },
954
+ table: function (_a) {
955
+ var fullWidth = _a.fullWidth;
956
+ return (__assign$1(__assign$1({ borderCollapse: 'collapse' }, (fullWidth && { width: '100%' })), typography[tableConstants.tableTypography]));
957
+ },
958
+ tableWrap: function () { return ({
959
+ maxWidth: '100%',
960
+ overflowX: 'auto',
961
+ overflowY: 'hidden',
962
+ }); },
963
+ });
964
+ });
965
+
934
966
  /* eslint-disable react/jsx-key */
935
967
  var Table = function (props) {
968
+ // Setup
936
969
  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;
970
+ 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
971
  var theme = useTheme$1();
939
972
  var classes = useTableStyles(props);
940
973
  var columns = useMemo(function () { return makeColumns({ columns: rawColumns, actions: actions, actionColumnId: actionColumnId }); }, [rawColumns, actions]);
941
974
  var initialPaginationState = paginationOptions.initialState, restPaginationOptions = __rest$1(paginationOptions, ["initialState"]);
942
975
  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;
976
+ var instance = useTable(__assign$1(__assign$1({ columns: columns, data: data, disableSortBy: !sortable, initialState: initialState }, restPaginationOptions), tableOptions), useGlobalFilter, useSortBy, usePagination);
977
+ 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;
978
+ // Aliases
945
979
  var isPaginated = pageCount > 1;
946
980
  var isEmpty = rows.length === 0;
947
981
  var lineHeightRem = parseFloat(theme.typography[tableConstants.tableTypography].lineHeight);
@@ -952,6 +986,7 @@ var Table = function (props) {
952
986
  var bodyHeight = instance.state.pageSize * cellHeight;
953
987
  var footerHeight = 88;
954
988
  var minHeight = headerHeight + bodyHeight + footerHeight;
989
+ // Handlers
955
990
  var handlePagination = function (_e, pageNumber) {
956
991
  var pageIndex = pageNumber - 1;
957
992
  if (onPaginate) {
@@ -962,6 +997,8 @@ var Table = function (props) {
962
997
  }
963
998
  };
964
999
  return (React__default.createElement(Box, { className: classes.root, minHeight: fullHeight ? minHeight : 'unset' },
1000
+ searchable && (React__default.createElement(Box, null,
1001
+ React__default.createElement(GlobalFilter, __assign$1({ value: state.globalFilter, setValue: setGlobalFilter }, GlobalFilterProps)))),
965
1002
  React__default.createElement(Box, { className: classes.tableWrap },
966
1003
  React__default.createElement("table", __assign$1({ className: classes.table }, getTableProps()),
967
1004
  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'))); }))); })),