@bluemarble/bm-components 0.0.32 → 0.0.34

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.
package/dist/esm/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import React__default, { memo, useState, useRef, useMemo, useEffect, useCallback } from 'react';
3
- import { TableHead, TableRow, TableCell, TableSortLabel, Chip, Tooltip, IconButton, Paper, LinearProgress, TableContainer, Table, TableBody, TablePagination, Popover, Box as Box$2, Typography, Autocomplete as Autocomplete$1, TextField, Button, FormControl, Select as Select$1, InputLabel, FormControlLabel, Checkbox as Checkbox$1, Switch as Switch$1, FormLabel, RadioGroup, Radio as Radio$1, CircularProgress } from '@mui/material';
3
+ import { TableHead, TableRow, TableCell, TableSortLabel, Chip, Tooltip, IconButton, Paper, LinearProgress, TableContainer, Table, TableBody, TablePagination, Popover, Box as Box$2, Typography, Autocomplete as Autocomplete$1, TextField, Button, FormControl, Select as Select$1, InputLabel, FormControlLabel, Checkbox as Checkbox$1, Switch as Switch$1, FormLabel, RadioGroup, Radio as Radio$1, CircularProgress, Stack, MenuItem } from '@mui/material';
4
4
  import emStyled from '@emotion/styled';
5
5
  import '@emotion/react';
6
6
  import { Field, useField } from 'formik';
@@ -31,7 +31,7 @@ const GridHeader = ({ setOrder, setOrderBy, order, orderBy, titles = [], }) => {
31
31
  return (React__default.createElement(TableCell, { key: title.name, sortDirection: orderBy === title.name ? order : false, sx: Object.assign({ fontWeight: "bold" }, title.sx) },
32
32
  React__default.createElement(TableSortLabel, { onClick: () => onRequestSort(title.name), active: orderBy === title.name, direction: orderBy === title.name ? order : "asc", sx: {
33
33
  position: "relative",
34
- svg: { position: "absolute", right: "-70%" },
34
+ svg: { position: "absolute", right: "27px" },
35
35
  } }, title.label)));
36
36
  }))));
37
37
  };
@@ -3765,6 +3765,10 @@ function MdSearch (props) {
3765
3765
  return GenIcon({"tag":"svg","attr":{"viewBox":"0 0 24 24"},"child":[{"tag":"path","attr":{"fill":"none","d":"M0 0h24v24H0z"}},{"tag":"path","attr":{"d":"M15.5 14h-.79l-.28-.27A6.471 6.471 0 0016 9.5 6.5 6.5 0 109.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"}}]})(props);
3766
3766
  }function MdFilterList (props) {
3767
3767
  return GenIcon({"tag":"svg","attr":{"viewBox":"0 0 24 24"},"child":[{"tag":"path","attr":{"fill":"none","d":"M0 0h24v24H0z"}},{"tag":"path","attr":{"d":"M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z"}}]})(props);
3768
+ }function MdArrowBackIosNew (props) {
3769
+ return GenIcon({"tag":"svg","attr":{"viewBox":"0 0 24 24"},"child":[{"tag":"path","attr":{"fill":"none","d":"M0 0h24v24H0z"}},{"tag":"path","attr":{"d":"M17.77 3.77L16 2 6 12l10 10 1.77-1.77L9.54 12z"}}]})(props);
3770
+ }function MdArrowForwardIos (props) {
3771
+ return GenIcon({"tag":"svg","attr":{"viewBox":"0 0 24 24"},"child":[{"tag":"path","attr":{"fill":"none","d":"M0 0h24v24H0V0z"}},{"tag":"path","attr":{"d":"M6.23 20.23L8 22l10-10L8 2 6.23 3.77 14.46 12z"}}]})(props);
3768
3772
  }function MdClose (props) {
3769
3773
  return GenIcon({"tag":"svg","attr":{"viewBox":"0 0 24 24"},"child":[{"tag":"path","attr":{"fill":"none","d":"M0 0h24v24H0z"}},{"tag":"path","attr":{"d":"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"}}]})(props);
3770
3774
  }
@@ -3862,7 +3866,7 @@ const AutoCreatedRows = ({ tableData, columns, primaryKey, }) => {
3862
3866
  }))))));
3863
3867
  };
3864
3868
 
3865
- const Grid = (props) => {
3869
+ const Grid$1 = (props) => {
3866
3870
  const { columnTitles, isLoading, footer, noFilterButtons = false, selectedFilters, customButtons, setSelectedFilters, defaultData, setTableData, updateFilters, tableData, rowOptions = [60, 100, 150], noPagination, primaryKey, noFilters, } = props;
3867
3871
  const [order, setOrder] = useState(undefined);
3868
3872
  const [orderBy, setOrderBy] = useState("");
@@ -14983,5 +14987,260 @@ const TabPanel = (props) => {
14983
14987
  return (React__default.createElement("div", Object.assign({ role: "tabpanel", hidden: value !== index, id: `tabpanel-${index}`, "aria-labelledby": `tab-${index}` }, other), value === index && React__default.createElement(React__default.Fragment, null, children)));
14984
14988
  };
14985
14989
 
14986
- export { Autocomplete, Checkbox, EditableTableCell, Grid, Input, InputMask, LargeButton, Radio, Select, Switch, TabPanel, Td, Tr, filterData, getTabProps };
14990
+ const GridPagination = ({ currentPage, totalNumberOfPages, onPageChange, rowsPerPageOptions, setRowsPerPage, rowsPerPage, dense, }) => {
14991
+ return (React__default.createElement(Stack, { direction: "row", justifyContent: "space-between", alignItems: "center", width: "100%", py: dense ? 0.5 : 1 },
14992
+ React__default.createElement(Box$2, null),
14993
+ React__default.createElement(Stack, { direction: "row", alignItems: "center" },
14994
+ React__default.createElement(Select$1, { variant: "standard", size: "small", margin: "dense", sx: { width: 60, mr: 2, textAlign: "center", fontSize: 13 }, value: rowsPerPage, onChange: ({ target }) => setRowsPerPage(parseInt(target.value)) }, rowsPerPageOptions.map((option) => (React__default.createElement(MenuItem, { key: option, value: option }, option)))),
14995
+ React__default.createElement(Typography, { variant: "body2" },
14996
+ currentPage + 1,
14997
+ " de ",
14998
+ totalNumberOfPages + 1),
14999
+ React__default.createElement(IconButton, { size: "small", disabled: currentPage === 0, onClick: () => onPageChange(currentPage - 1) },
15000
+ React__default.createElement(MdArrowBackIosNew, { size: 8 * 2 })),
15001
+ React__default.createElement(IconButton, { size: "small", disabled: currentPage === totalNumberOfPages, onClick: () => onPageChange(currentPage + 1) },
15002
+ React__default.createElement(MdArrowForwardIos, { size: 8 * 2 })))));
15003
+ };
15004
+
15005
+ function Grid({ columns: columnsData, children, fixedColumns, paperProps, tableBodyProps, tableHeadProps, tableProps, sortedDirection, sortedBy, onSortBy, dense = true, striped, bordered, currentPage, totalNumberOfPages, onPageChange, rowsPerPage, setRowsPerPage, rowsPerPageOptions, }) {
15006
+ const [columns, setColumns] = useState([]);
15007
+ const [startResize, setStartResize] = useState(0);
15008
+ const columnsRef = useRef(null);
15009
+ const defaultColumn = useRef(columnsData);
15010
+ const columnsResizing = useRef(undefined);
15011
+ function handleResizeColumn(event) {
15012
+ if (columnsResizing.current === undefined || event.clientX === 0)
15013
+ return;
15014
+ const newColumns = defaultColumn.current.map((column, columnIndex) => {
15015
+ if (columnsResizing.current === columnIndex) {
15016
+ const width = ((column === null || column === void 0 ? void 0 : column.width) || 0) + event.pageX - startResize;
15017
+ return Object.assign(Object.assign({}, column), { width: width <= 0 ? 0 : width });
15018
+ }
15019
+ return column;
15020
+ });
15021
+ setColumns(newColumns);
15022
+ }
15023
+ function onDragStart(ev, index) {
15024
+ setStartResize(ev.pageX);
15025
+ columnsResizing.current = index;
15026
+ }
15027
+ function ondragend() {
15028
+ setStartResize(0);
15029
+ columnsResizing.current = undefined;
15030
+ defaultColumn.current = columns;
15031
+ }
15032
+ function getTableWidth() {
15033
+ if (fixedColumns)
15034
+ return {
15035
+ width: columns.reduce((acc, col) => (acc += (col === null || col === void 0 ? void 0 : col.width) || 100), 0),
15036
+ };
15037
+ return {};
15038
+ }
15039
+ useEffect(() => {
15040
+ const newColumns = columnsData.map((column) => (Object.assign(Object.assign({}, column), { width: column.width || 100 })));
15041
+ defaultColumn.current = newColumns;
15042
+ setColumns(newColumns);
15043
+ }, [columnsData]);
15044
+ return (React__default.createElement(Paper, Object.assign({}, paperProps, { sx: Object.assign(Object.assign({ "tr td": {
15045
+ py: dense ? 0.23 : 0.7,
15046
+ border: bordered ? "1px solid" : "",
15047
+ borderColor: "divider",
15048
+ }, "tr:nth-of-type(even)": {
15049
+ transition: "background-color ease 200ms",
15050
+ bgcolor: striped ? "divider" : "initial",
15051
+ } }, getTableWidth()), paperProps === null || paperProps === void 0 ? void 0 : paperProps.sx) }),
15052
+ React__default.createElement(Table, Object.assign({ size: "small", stickyHeader: true }, tableProps, { sx: Object.assign(Object.assign({}, getTableWidth()), tableProps === null || tableProps === void 0 ? void 0 : tableProps.sx) }),
15053
+ React__default.createElement(TableHead, Object.assign({}, tableHeadProps),
15054
+ React__default.createElement(TableRow, { ref: columnsRef }, columns.map((column, index) => (React__default.createElement(TableCell, { key: column.name, padding: dense ? "none" : "normal", sx: Object.assign({ border: "1px solid", borderColor: "divider", width: column.width || 100, pl: 2, zIndex: columns.length - index }, column === null || column === void 0 ? void 0 : column.sx) },
15055
+ React__default.createElement(TableSortLabel, { active: sortedBy === column.name, direction: sortedDirection, onClick: () => onSortBy(column.name), disabled: column.canSort === false, sx: { position: "relative", right: "-9px" } }, column.label),
15056
+ index < columns.length && (React__default.createElement(Box$2, { draggable: true, onDragStart: (ev) => onDragStart(ev, index), onDrag: handleResizeColumn, onDragEnd: ondragend, sx: {
15057
+ width: "4px",
15058
+ height: "100%",
15059
+ position: "absolute",
15060
+ top: 0,
15061
+ zIndex: index,
15062
+ right: "-3px",
15063
+ bgcolor: "transparent",
15064
+ transition: "all ease 200ms",
15065
+ ":hover": {
15066
+ cursor: "w-resize",
15067
+ bgcolor: "primary.main",
15068
+ },
15069
+ } }))))))),
15070
+ React__default.createElement(TableBody, Object.assign({}, tableBodyProps), children)),
15071
+ React__default.createElement(GridPagination, { rowsPerPageOptions: rowsPerPageOptions, currentPage: currentPage, totalNumberOfPages: totalNumberOfPages, onPageChange: onPageChange, setRowsPerPage: setRowsPerPage, rowsPerPage: rowsPerPage, dense: dense })));
15072
+ }
15073
+
15074
+ function useFilter() {
15075
+ const [selectedFilters, setSelectedFilters] = useState([]);
15076
+ const filterBy = (newFilter) => {
15077
+ const propToCompare = newFilter.id ? "id" : "prop";
15078
+ function removeRepeatedFilters(filter) {
15079
+ return filter[propToCompare] !== newFilter[propToCompare];
15080
+ }
15081
+ setSelectedFilters((filters) => [
15082
+ ...filters.filter(removeRepeatedFilters),
15083
+ newFilter,
15084
+ ]);
15085
+ };
15086
+ const removeFilter = (prop) => {
15087
+ setSelectedFilters(selectedFilters.filter((filter) => filter.prop !== prop));
15088
+ };
15089
+ function clearAllFilters() {
15090
+ setSelectedFilters([]);
15091
+ }
15092
+ return {
15093
+ filters: selectedFilters,
15094
+ filterBy,
15095
+ removeFilter,
15096
+ createFilter,
15097
+ clearAllFilters,
15098
+ };
15099
+ }
15100
+ function getObjectValue(obj) {
15101
+ return (prop) => prop.split(".").reduce((o, k) => o[k], obj);
15102
+ }
15103
+ function isDate(date) {
15104
+ if (date instanceof Date)
15105
+ return true;
15106
+ else if (String(date).endsWith("Z"))
15107
+ return true;
15108
+ return false;
15109
+ }
15110
+ function compareFilter(item, filter) {
15111
+ const itemValue = getObjectValue(item)(filter.prop);
15112
+ switch (filter.compareType) {
15113
+ case "equal":
15114
+ return itemValue === filter.value;
15115
+ case "gte":
15116
+ return isDate(itemValue)
15117
+ ? moment(String(itemValue)).isSameOrAfter(filter.value)
15118
+ : itemValue >= filter.value;
15119
+ case "gt":
15120
+ return isDate(itemValue)
15121
+ ? moment(String(itemValue)).isAfter(filter.value)
15122
+ : itemValue > filter.value;
15123
+ case "lte":
15124
+ return isDate(itemValue)
15125
+ ? moment(String(itemValue)).isSameOrBefore(filter.value)
15126
+ : itemValue <= filter.value;
15127
+ case "lt":
15128
+ return isDate(itemValue)
15129
+ ? moment(String(itemValue)).isBefore(filter.value)
15130
+ : itemValue < filter.value;
15131
+ }
15132
+ return false;
15133
+ }
15134
+ function createFilter(filters) {
15135
+ function apply(item) {
15136
+ const satisfiedFilters = filters.reduce((acc, filter) => {
15137
+ if (compareFilter(item, filter))
15138
+ acc += 1;
15139
+ return acc;
15140
+ }, 0);
15141
+ return satisfiedFilters === filters.length;
15142
+ }
15143
+ return {
15144
+ apply,
15145
+ };
15146
+ }
15147
+
15148
+ function useGrid({ columns, filters = [], rowsPerPageOptions = [30, 60, 100], }) {
15149
+ const [data, setData] = useState([]);
15150
+ const [sortedBy, setSortedBy] = useState("");
15151
+ const [sortedDirection, setSortedDirection] = useState("desc");
15152
+ const [currentPage, setCurrentPage] = useState(0);
15153
+ const [rowsPerPage, setRowsPerPage] = useState(rowsPerPageOptions[0]);
15154
+ const defaultData = useRef([]);
15155
+ const toggleSortedDirection = () => {
15156
+ if (sortedDirection === "desc")
15157
+ setSortedDirection("asc");
15158
+ if (sortedDirection === "asc")
15159
+ setSortedDirection("desc");
15160
+ };
15161
+ const onSortBy = (prop) => {
15162
+ if (!prop)
15163
+ return;
15164
+ if (prop === sortedBy) {
15165
+ if (sortedDirection === "asc") {
15166
+ toggleSortedDirection();
15167
+ setSortedBy("");
15168
+ }
15169
+ else
15170
+ toggleSortedDirection();
15171
+ }
15172
+ else {
15173
+ setSortedDirection("desc");
15174
+ setSortedBy(prop);
15175
+ }
15176
+ };
15177
+ const set = (data) => {
15178
+ setData(data);
15179
+ defaultData.current = data;
15180
+ };
15181
+ const sortData = (data) => {
15182
+ if (sortedBy) {
15183
+ return data.sort((a, b) => {
15184
+ const compare = b[sortedBy] > a[sortedBy];
15185
+ if (sortedDirection === "asc")
15186
+ return compare ? 1 : -1;
15187
+ return compare ? -1 : 1;
15188
+ });
15189
+ }
15190
+ else
15191
+ return data;
15192
+ };
15193
+ const onPageChange = (pageNumber) => {
15194
+ if (pageNumber < 0)
15195
+ return;
15196
+ if (pageNumber > totalNumberOfPages)
15197
+ return;
15198
+ setCurrentPage(pageNumber);
15199
+ };
15200
+ const onChangeRowsPerPage = (rows) => {
15201
+ const totalNumberOfPages = Math.round(filteredData.length / rows) - 1;
15202
+ if (currentPage > totalNumberOfPages)
15203
+ setCurrentPage(totalNumberOfPages);
15204
+ setRowsPerPage(rows);
15205
+ };
15206
+ const filteredData = useMemo(() => {
15207
+ const newData = defaultData.current.slice(0);
15208
+ const newFilter = createFilter(filters);
15209
+ return newData.filter(newFilter.apply);
15210
+ }, [data, filters]);
15211
+ const displayData = useMemo(() => {
15212
+ const sortedData = sortData(filteredData);
15213
+ const startPage = currentPage * rowsPerPage;
15214
+ const endPage = startPage + rowsPerPage;
15215
+ return sortedData.slice(startPage, endPage);
15216
+ }, [sortedBy, sortedDirection, filteredData, currentPage, rowsPerPage]);
15217
+ const totalNumberOfPages = Math.round(filteredData.length / rowsPerPage) - 1;
15218
+ return {
15219
+ data: displayData,
15220
+ defaultData: defaultData.current,
15221
+ set,
15222
+ onSortBy,
15223
+ sortedBy,
15224
+ sortedDirection,
15225
+ columns,
15226
+ currentPage,
15227
+ totalNumberOfPages: totalNumberOfPages,
15228
+ onPageChange,
15229
+ setRowsPerPage: onChangeRowsPerPage,
15230
+ rowsPerPageOptions,
15231
+ rowsPerPage,
15232
+ };
15233
+ }
15234
+
15235
+ /* eslint no-undef: "off" */
15236
+ function useEvent(event, handler, passive = false) {
15237
+ useEffect(() => {
15238
+ window.addEventListener(event, handler, passive);
15239
+ return function cleanup() {
15240
+ window.removeEventListener(event, handler);
15241
+ };
15242
+ });
15243
+ }
15244
+
15245
+ export { Autocomplete, Grid as BaseGrid, Checkbox, EditableTableCell, Grid$1 as Grid, Input, InputMask, LargeButton, Radio, Select, Switch, TabPanel, Td, Tr, createFilter, filterData, getTabProps, useEvent, useFilter, useGrid };
14987
15246
  //# sourceMappingURL=index.js.map