mig-schema-table 3.0.96 → 3.0.97

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,9 @@
1
+ import { Dispatch, SetStateAction } from "react";
2
+ import "./index.scss";
3
+ interface IColumnResizersProps {
4
+ columnWidths: number[];
5
+ setColumnWidths: Dispatch<SetStateAction<number[]>>;
6
+ tableBodyHeight: number;
7
+ }
8
+ declare const ColumnResizers: ({ columnWidths, setColumnWidths, tableBodyHeight, }: IColumnResizersProps) => import("react/jsx-runtime").JSX.Element;
9
+ export default ColumnResizers;
@@ -0,0 +1,42 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import React from "react";
3
+ import "./index.scss";
4
+ const MINIMUM_COLUMN_WIDTH = 25;
5
+ const ColumnResizers = ({ columnWidths, setColumnWidths, tableBodyHeight, }) => {
6
+ const [dragColumnIndex, setDragColumnIndex] = React.useState(-1);
7
+ const [dragStartX, setDragStartX] = React.useState(0);
8
+ const setColumnDelta = React.useCallback((columnDelta) => {
9
+ if (!columnWidths) {
10
+ throw new Error("Missing column widths?");
11
+ }
12
+ const newColumnWidths = columnWidths.map((columnWidth, columnWidthIndex) => {
13
+ if (columnWidthIndex === dragColumnIndex) {
14
+ return Math.max(columnWidth + columnDelta, MINIMUM_COLUMN_WIDTH);
15
+ }
16
+ if (columnWidthIndex - 1 === dragColumnIndex) {
17
+ return Math.max(columnWidth - columnDelta, MINIMUM_COLUMN_WIDTH);
18
+ }
19
+ return columnWidth;
20
+ });
21
+ setColumnWidths(newColumnWidths);
22
+ }, [columnWidths, dragColumnIndex, setColumnWidths]);
23
+ let pointer = 0;
24
+ const onDragStart = React.useCallback((e) => {
25
+ setDragColumnIndex(parseInt(e.currentTarget.dataset.columnIndex));
26
+ setDragStartX(e.clientX);
27
+ }, []);
28
+ const onDragEnd = React.useCallback((e) => {
29
+ setColumnDelta(e.clientX - dragStartX);
30
+ setDragStartX(0);
31
+ setDragColumnIndex(-1);
32
+ }, [dragStartX, setColumnDelta]);
33
+ return (_jsx("div", { children: columnWidths.map((columnWidth, columnIndex) => {
34
+ pointer += columnWidth;
35
+ const classNames = ["schema-table__column_resizer"];
36
+ if (columnIndex === dragColumnIndex) {
37
+ classNames.push("schema-table__column_resizer--dragged");
38
+ }
39
+ return columnIndex === columnWidths.length - 1 ? null : (_jsx("div", { style: { left: pointer, bottom: tableBodyHeight }, className: classNames.join(" "), draggable: "true", "data-column-index": columnIndex, onDragStart: onDragStart, onDragEnd: onDragEnd }, columnIndex));
40
+ }) }));
41
+ };
42
+ export default ColumnResizers;
@@ -31,6 +31,7 @@ export interface ISchemaTableProps<T> {
31
31
  getRowSelected?: (rowData: T, dataIndex: number) => boolean;
32
32
  isColumnFilterable?: boolean;
33
33
  isExportable?: boolean;
34
+ isResizable?: boolean;
34
35
  isSearchable?: boolean;
35
36
  isSortable?: boolean;
36
37
  maxHeight?: number;
@@ -58,6 +59,6 @@ export interface INumberColumnFilterValue {
58
59
  filterEmpty?: true;
59
60
  }
60
61
  export type TColumnFilterValue = string | boolean | number | IDateColumnFilterValue | INumberColumnFilterValue;
61
- declare function SchemaTable<T>({ Heading, checkedIndexes, config, customElement, data, defaultColumnFilters, defaultSortAsc, defaultSortColumn, disabledCheckedIndexes, enableAutoFocus, enableRowCounter, getRowClassName, getRowSelected, isColumnFilterable, isExportable, isSearchable, isSortable, maxHeight, onCheckedIndexesChange, onRowClick, onRowDoubleClick, onSearchEnter, rowHeight, schema, searchPlaceholder, style, translate, useFilterStateHash, width, }: ISchemaTableProps<T>): import("react/jsx-runtime").JSX.Element;
62
+ declare function SchemaTable<T>({ Heading, checkedIndexes, config, customElement, data, defaultColumnFilters, defaultSortAsc, defaultSortColumn, disabledCheckedIndexes, enableAutoFocus, enableRowCounter, getRowClassName, getRowSelected, isColumnFilterable, isExportable, isResizable, isSearchable, isSortable, maxHeight, onCheckedIndexesChange, onRowClick, onRowDoubleClick, onSearchEnter, rowHeight, schema, searchPlaceholder, style, translate, useFilterStateHash, width, }: ISchemaTableProps<T>): import("react/jsx-runtime").JSX.Element;
62
63
  declare const _default: typeof SchemaTable;
63
64
  export default _default;
@@ -22,6 +22,7 @@ import Th, { EColumnFilterStatus } from "./Th";
22
22
  import ThMenu from "./ThMenu";
23
23
  import { saveAs } from "file-saver";
24
24
  import { unparse } from "papaparse";
25
+ import ColumnResizers from "./ColumnResizers";
25
26
  const startOfTheWorldDate = new Date("1000-01-01 00:00:00Z");
26
27
  const numberFormatter = new Intl.NumberFormat("nl-NL");
27
28
  const currencyFormatter = new Intl.NumberFormat("nl-NL", {
@@ -46,7 +47,7 @@ function getIsColumnSortable(isTableSortable, propSchema, propConfig) {
46
47
  return !!(isTableSortable &&
47
48
  (propSchema || (propConfig === null || propConfig === void 0 ? void 0 : propConfig.renderData) || (propConfig === null || propConfig === void 0 ? void 0 : propConfig.sort)));
48
49
  }
49
- function SchemaTable({ Heading = VariableSizeList, checkedIndexes, config, customElement, data, defaultColumnFilters = {}, defaultSortAsc = false, defaultSortColumn, disabledCheckedIndexes, enableAutoFocus = true, enableRowCounter = true, getRowClassName, getRowSelected, isColumnFilterable = true, isExportable = true, isSearchable = true, isSortable = true, maxHeight, onCheckedIndexesChange, onRowClick, onRowDoubleClick, onSearchEnter, rowHeight = 36, schema, searchPlaceholder, style, translate = defaultTranslate, useFilterStateHash, width, }) {
50
+ function SchemaTable({ Heading = VariableSizeList, checkedIndexes, config, customElement, data, defaultColumnFilters = {}, defaultSortAsc = false, defaultSortColumn, disabledCheckedIndexes, enableAutoFocus = true, enableRowCounter = true, getRowClassName, getRowSelected, isColumnFilterable = true, isExportable = true, isResizable = true, isSearchable = true, isSortable = true, maxHeight, onCheckedIndexesChange, onRowClick, onRowDoubleClick, onSearchEnter, rowHeight = 36, schema, searchPlaceholder, style, translate = defaultTranslate, useFilterStateHash, width, }) {
50
51
  const [sortColumn, setSortColumn] = React.useState(defaultSortColumn);
51
52
  const [sortAsc, setSortAsc] = React.useState(defaultSortAsc);
52
53
  const [thMenuConfig, setThMenuConfig] = React.useState();
@@ -203,30 +204,22 @@ function SchemaTable({ Heading = VariableSizeList, checkedIndexes, config, custo
203
204
  }, 0);
204
205
  return { dynamicWidthColumnCount, fixedWidthColumnsWidth };
205
206
  }, [columnNames, config]);
206
- const columnWidths = React.useMemo(() => {
207
- const dynamicColumnWidth = Math.floor((width - fixedWidthColumnsWidth) / dynamicWidthColumnCount);
208
- let roundingDifference = (width - fixedWidthColumnsWidth) % dynamicWidthColumnCount;
209
- return columnNames.map((propName) => {
210
- if (propName === SELECT_ALL_COLUMN_NAME) {
211
- return SELECT_ALL_COLUMN_WIDTH;
212
- }
213
- const propConfig = config ? config[propName] : undefined;
214
- if (propConfig === null || propConfig === void 0 ? void 0 : propConfig.width) {
215
- return propConfig === null || propConfig === void 0 ? void 0 : propConfig.width;
216
- }
217
- if (roundingDifference) {
218
- roundingDifference -= 1;
219
- return dynamicColumnWidth + 1;
220
- }
221
- return dynamicColumnWidth;
222
- });
223
- }, [
224
- columnNames,
225
- config,
226
- dynamicWidthColumnCount,
227
- fixedWidthColumnsWidth,
228
- width,
229
- ]);
207
+ const dynamicColumnWidth = Math.floor((width - fixedWidthColumnsWidth) / dynamicWidthColumnCount);
208
+ let roundingDifference = (width - fixedWidthColumnsWidth) % dynamicWidthColumnCount;
209
+ const [columnWidths, setColumnWidths] = React.useState(columnNames.map((propName) => {
210
+ if (propName === SELECT_ALL_COLUMN_NAME) {
211
+ return SELECT_ALL_COLUMN_WIDTH;
212
+ }
213
+ const propConfig = config ? config[propName] : undefined;
214
+ if (propConfig === null || propConfig === void 0 ? void 0 : propConfig.width) {
215
+ return propConfig === null || propConfig === void 0 ? void 0 : propConfig.width;
216
+ }
217
+ if (roundingDifference) {
218
+ roundingDifference -= 1;
219
+ return dynamicColumnWidth + 1;
220
+ }
221
+ return dynamicColumnWidth;
222
+ }));
230
223
  const getColumnWidth = React.useCallback((columnIndex) => (columnWidths ? columnWidths[columnIndex] : 1), [columnWidths]);
231
224
  const filteredRenderData = React.useMemo(() => {
232
225
  if (!renderData ||
@@ -593,15 +586,15 @@ function SchemaTable({ Heading = VariableSizeList, checkedIndexes, config, custo
593
586
  type: "text/csv;charset=utf-8",
594
587
  }), "export.csv");
595
588
  }, [sortedRenderData]);
596
- return (_jsxs("div", Object.assign({ className: `schema-table${onRowClick ? " schema-table--clickable-rows" : ""}`, style: Object.assign(Object.assign({}, style), { width: rowWidth }), role: "table" }, { children: [_jsxs("div", Object.assign({ className: "schema-table__action-container" }, { children: [_jsx("div", { children: isSearchable ? (_jsx("input", { className: "schema-table__search", type: "text", placeholder: searchPlaceholder || "Search...", value: searchQuery, onChange: onSearchChange, onKeyDown: onInputKeyDown, autoFocus: enableAutoFocus, onBlur: onSearchBlur })) : null }), customElement || (_jsx("div", { className: "schema-table__custom_element_placeholder" })), enableRowCounter ? (_jsx("span", Object.assign({ className: "schema-table__row_counter" }, { children: translate("showingFilteredCountOfTotalCount", (sortedRenderData === null || sortedRenderData === void 0 ? void 0 : sortedRenderData.length) || 0, data.length) }))) : null, isExportable ? (_jsx("button", Object.assign({ onClick: onExportDataClick, style: { marginLeft: 8 }, disabled: !(sortedRenderData === null || sortedRenderData === void 0 ? void 0 : sortedRenderData.length) }, { children: translate("exportData") }))) : null, isSearchable || isColumnFilterable ? (_jsx("button", Object.assign({ onClick: onClearFiltersButtonClick, style: { marginLeft: 8 }, disabled: Object.keys(columnFilterMap).length + searchQuery.length === 0 }, { children: translate("clearFilters") }))) : null] })), _jsx(Heading, Object.assign({ height: 50, itemCount: columnCount, itemSize: getColumnWidth, layout: "horizontal", width: rowWidth, sortAsc: sortAsc, setSortAsc: onSetSortAsc, setSortColumn: onSetSortColumn, sortColumn: sortColumn, sortedRenderData: sortedRenderData, className: "schema-table__th-row" }, { children: ConfiguredTh }), `thead_${rowWidth}_${sortColumn}_${sortAsc}_${searchQuery}`), sourceData && !isDirty ? (_jsx(VariableSizeGrid, Object.assign({ className: "schema-table__tbody", height: tableBodyHeight, width: rowWidth, columnWidth: getColumnWidth, rowHeight: getRowHeight, columnCount: columnCount, rowCount: rowCount }, { children: SchemaTableTd }), `tbody_${tableBodyHeight}_${rowWidth}_${sortColumn}_${sortAsc}_${searchQuery}_${columnCount}`)) : (_jsx("div", Object.assign({ style: {
597
- width: rowWidth,
598
- height: Math.max(50, tableBodyHeight),
599
- border: "1px solid #BBB",
600
- textAlign: "center",
601
- display: "flex",
602
- backgroundColor: "#CCC",
603
- alignItems: "center",
604
- justifyContent: "center",
605
- } }, { children: isDirty ? (_jsx("button", Object.assign({ onClick: refreshData, className: "btn border" }, { children: "Refresh data" }))) : (_jsx("div", { children: "\u231B Loading..." })) }))), thMenuConfig ? (_jsx(ThMenu, { isFilterable: !!isColumnFilterable, isSortable: getIsColumnSortable(!!isSortable, schema.properties[thMenuConfig.propName], thMenuConfig.propConfig), onChange: onSchemaColumnFilterChange, onClose: onPopoverClose, onInputKeyDown: onInputKeyDown, propConfig: thMenuConfig.propConfig, propIsRequired: thMenuConfig.propIsRequired, propName: thMenuConfig.propName, propSchema: schema.properties[thMenuConfig.propName], referenceElement: thMenuConfig.referenceElement, setSortAsc: setSortAsc, setSortColumn: setSortColumn, translate: translate, value: columnFilterMap[thMenuConfig.propName] })) : null] })));
589
+ return (_jsxs("div", Object.assign({ className: `schema-table${onRowClick ? " schema-table--clickable-rows" : ""}`, style: Object.assign(Object.assign({}, style), { width: rowWidth }), role: "table" }, { children: [_jsxs("div", Object.assign({ className: "schema-table__action-container" }, { children: [_jsx("div", { children: isSearchable ? (_jsx("input", { className: "schema-table__search", type: "text", placeholder: searchPlaceholder || "Search...", value: searchQuery, onChange: onSearchChange, onKeyDown: onInputKeyDown, autoFocus: enableAutoFocus, onBlur: onSearchBlur })) : null }), customElement || (_jsx("div", { className: "schema-table__custom_element_placeholder" })), enableRowCounter ? (_jsx("span", Object.assign({ className: "schema-table__row_counter" }, { children: translate("showingFilteredCountOfTotalCount", (sortedRenderData === null || sortedRenderData === void 0 ? void 0 : sortedRenderData.length) || 0, data.length) }))) : null, isExportable ? (_jsx("button", Object.assign({ onClick: onExportDataClick, style: { marginLeft: 8 }, disabled: !(sortedRenderData === null || sortedRenderData === void 0 ? void 0 : sortedRenderData.length) }, { children: translate("exportData") }))) : null, isSearchable || isColumnFilterable ? (_jsx("button", Object.assign({ onClick: onClearFiltersButtonClick, style: { marginLeft: 8 }, disabled: Object.keys(columnFilterMap).length + searchQuery.length === 0 }, { children: translate("clearFilters") }))) : null] })), _jsxs("div", Object.assign({ className: "schema-table__column_resize_container" }, { children: [_jsx(Heading, Object.assign({ height: 50, itemCount: columnCount, itemSize: getColumnWidth, layout: "horizontal", width: rowWidth, sortAsc: sortAsc, setSortAsc: onSetSortAsc, setSortColumn: onSetSortColumn, sortColumn: sortColumn, sortedRenderData: sortedRenderData, className: "schema-table__th-row" }, { children: ConfiguredTh }), `thead_${rowWidth}_${sortColumn}_${sortAsc}_${searchQuery}_${columnWidths.join(" ")}`), sourceData && !isDirty ? (_jsx(VariableSizeGrid, Object.assign({ className: "schema-table__tbody", height: tableBodyHeight, width: rowWidth, columnWidth: getColumnWidth, rowHeight: getRowHeight, columnCount: columnCount, rowCount: rowCount }, { children: SchemaTableTd }), `tbody_${tableBodyHeight}_${rowWidth}_${sortColumn}_${sortAsc}_${searchQuery}_${columnCount}_${columnWidths.join(" ")}`)) : (_jsx("div", Object.assign({ style: {
590
+ width: rowWidth,
591
+ height: Math.max(50, tableBodyHeight),
592
+ border: "1px solid #BBB",
593
+ textAlign: "center",
594
+ display: "flex",
595
+ backgroundColor: "#CCC",
596
+ alignItems: "center",
597
+ justifyContent: "center",
598
+ } }, { children: isDirty ? (_jsx("button", Object.assign({ onClick: refreshData, className: "btn border" }, { children: "Refresh data" }))) : (_jsx("div", { children: "\u231B Loading..." })) }))), isResizable ? (_jsx(ColumnResizers, { columnWidths: columnWidths, setColumnWidths: setColumnWidths, tableBodyHeight: tableBodyHeight })) : null] })), thMenuConfig ? (_jsx(ThMenu, { isFilterable: !!isColumnFilterable, isSortable: getIsColumnSortable(!!isSortable, schema.properties[thMenuConfig.propName], thMenuConfig.propConfig), onChange: onSchemaColumnFilterChange, onClose: onPopoverClose, onInputKeyDown: onInputKeyDown, propConfig: thMenuConfig.propConfig, propIsRequired: thMenuConfig.propIsRequired, propName: thMenuConfig.propName, propSchema: schema.properties[thMenuConfig.propName], referenceElement: thMenuConfig.referenceElement, setSortAsc: setSortAsc, setSortColumn: setSortColumn, translate: translate, value: columnFilterMap[thMenuConfig.propName] })) : null] })));
606
599
  }
607
600
  export default React.memo(SchemaTable);
package/dist/index.css CHANGED
@@ -47,6 +47,9 @@
47
47
  .schema-table__row_counter {
48
48
  font-size: 0.835rem;
49
49
  }
50
+ .schema-table__column_resize_container {
51
+ position: relative;
52
+ }
50
53
 
51
54
  .schema-table__th {
52
55
  background-color: #fcfcfc;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mig-schema-table",
3
- "version": "3.0.96",
3
+ "version": "3.0.97",
4
4
  "main": "dist/index.js",
5
5
  "files": [
6
6
  "dist/"