@bluemarble/bm-components 0.0.40 → 0.0.41

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
@@ -15002,32 +15002,33 @@ const GridPagination = ({ currentPage, totalNumberOfPages, onPageChange, rowsPer
15002
15002
  React__default.createElement(MdArrowForwardIos, { size: 8 * 2 })))));
15003
15003
  };
15004
15004
 
15005
+ /* eslint no-undef: "off" */
15006
+ function useEvent(event, handler, passive = false) {
15007
+ useEffect(() => {
15008
+ window.addEventListener(event, handler, passive);
15009
+ return function cleanup() {
15010
+ window.removeEventListener(event, handler);
15011
+ };
15012
+ });
15013
+ }
15014
+
15005
15015
  function Grid({ columns: columnsData, children, fixedColumns, paperProps, tableBodyProps, tableHeadProps, tableProps, sortedDirection, sortedBy, onSortBy, dense = true, striped, bordered, currentPage, totalNumberOfPages, onPageChange, rowsPerPage, setRowsPerPage, rowsPerPageOptions, }) {
15006
15016
  const [columns, setColumns] = useState([]);
15007
15017
  const [startResize, setStartResize] = useState(0);
15008
15018
  const columnsRef = useRef(null);
15019
+ const [draggingColumn, setDraggingColumn] = useState(-1);
15009
15020
  const defaultColumn = useRef(columnsData);
15010
- const columnsResizing = useRef(undefined);
15021
+ const startColumnWidth = useRef(undefined);
15011
15022
  function handleResizeColumn(event) {
15012
- if (columnsResizing.current === undefined || event.clientX === 0)
15023
+ if (draggingColumn < 0 || event.clientX === 0)
15013
15024
  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;
15025
+ const initialWidth = startColumnWidth.current;
15026
+ const offset = event.clientX - startResize;
15027
+ const newWidth = (initialWidth || 100) + offset;
15028
+ setColumns((prevColuns) => {
15029
+ prevColuns[draggingColumn].width = newWidth;
15030
+ return prevColuns.slice(0);
15020
15031
  });
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
  }
15032
15033
  function getTableWidth() {
15033
15034
  if (fixedColumns)
@@ -15036,11 +15037,35 @@ function Grid({ columns: columnsData, children, fixedColumns, paperProps, tableB
15036
15037
  };
15037
15038
  return {};
15038
15039
  }
15040
+ function getColWidth(width) {
15041
+ if (fixedColumns)
15042
+ return width;
15043
+ return "auto";
15044
+ }
15039
15045
  useEffect(() => {
15040
15046
  const newColumns = columnsData.map((column) => (Object.assign(Object.assign({}, column), { width: column.width || 100 })));
15041
15047
  defaultColumn.current = newColumns;
15042
15048
  setColumns(newColumns);
15043
15049
  }, [columnsData]);
15050
+ useEvent("mousemove", (event) => {
15051
+ if (draggingColumn >= 0) {
15052
+ handleResizeColumn(event);
15053
+ }
15054
+ });
15055
+ useEvent("mousedown", (event) => {
15056
+ if (event.target.id) {
15057
+ if (String(event.target.id).startsWith("resize-col")) {
15058
+ event.preventDefault();
15059
+ const dragginCol = parseInt(event.target.id.split("-").pop());
15060
+ setDraggingColumn(dragginCol);
15061
+ setStartResize(event.clientX);
15062
+ startColumnWidth.current = columns[dragginCol].width;
15063
+ }
15064
+ }
15065
+ });
15066
+ useEvent("mouseup", () => {
15067
+ setDraggingColumn(-1);
15068
+ });
15044
15069
  return (React__default.createElement(Paper, Object.assign({}, paperProps, { sx: Object.assign({ "tr td": {
15045
15070
  py: dense ? 0.23 : 0.7,
15046
15071
  border: bordered ? "1px solid" : "",
@@ -15048,19 +15073,21 @@ function Grid({ columns: columnsData, children, fixedColumns, paperProps, tableB
15048
15073
  }, "tr:nth-of-type(even)": {
15049
15074
  transition: "background-color ease 200ms",
15050
15075
  bgcolor: striped ? "divider" : "initial",
15051
- }, width: fixedColumns ? "fit-content" : "initial" }, paperProps === null || paperProps === void 0 ? void 0 : paperProps.sx) }),
15076
+ }, width: "fit-content" }, paperProps === null || paperProps === void 0 ? void 0 : paperProps.sx) }),
15052
15077
  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
15078
  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) },
15079
+ 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, minWidth: getColWidth((column === null || column === void 0 ? void 0 : column.width) || 100), maxWidth: getColWidth((column === null || column === void 0 ? void 0 : column.width) || 100), pl: 2, zIndex: columns.length - index }, column === null || column === void 0 ? void 0 : column.sx) },
15055
15080
  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: {
15081
+ index < columns.length && (React__default.createElement(Box$2, { id: `resize-col-${index}`, sx: {
15057
15082
  width: "4px",
15058
15083
  height: "100%",
15059
15084
  position: "absolute",
15060
15085
  top: 0,
15061
15086
  zIndex: index,
15062
15087
  right: "-3px",
15063
- bgcolor: "transparent",
15088
+ bgcolor: draggingColumn === index
15089
+ ? "primary.main"
15090
+ : "transparent",
15064
15091
  transition: "all ease 200ms",
15065
15092
  ":hover": {
15066
15093
  cursor: "w-resize",
@@ -15222,7 +15249,7 @@ function useGrid({ columns, filters = [], rowsPerPageOptions = [30, 60, 100], })
15222
15249
  sortedDirection,
15223
15250
  columns,
15224
15251
  currentPage,
15225
- totalNumberOfPages: totalNumberOfPages === 0 ? 1 : totalNumberOfPages,
15252
+ totalNumberOfPages: totalNumberOfPages < 0 ? 0 : totalNumberOfPages,
15226
15253
  onPageChange,
15227
15254
  setRowsPerPage: onChangeRowsPerPage,
15228
15255
  rowsPerPageOptions,
@@ -15230,15 +15257,5 @@ function useGrid({ columns, filters = [], rowsPerPageOptions = [30, 60, 100], })
15230
15257
  };
15231
15258
  }
15232
15259
 
15233
- /* eslint no-undef: "off" */
15234
- function useEvent(event, handler, passive = false) {
15235
- useEffect(() => {
15236
- window.addEventListener(event, handler, passive);
15237
- return function cleanup() {
15238
- window.removeEventListener(event, handler);
15239
- };
15240
- });
15241
- }
15242
-
15243
15260
  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 };
15244
15261
  //# sourceMappingURL=index.js.map