mctable-react 1.0.8 → 1.0.10

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/README.md CHANGED
@@ -54,6 +54,17 @@ You can also pass `settings`, `license`, or `onReady` if you need deeper access
54
54
 
55
55
  ## Changelog
56
56
 
57
+ ```
58
+ v1.0.10
59
+ - Upgrade to `mctable` core library `v1.4.2`
60
+ ```
61
+
62
+ ```
63
+ v1.0.9
64
+ - CommonJS module format support added
65
+ ```
66
+
67
+
57
68
  ```
58
69
  v1.0.8
59
70
  - Type exports added from `mctable`
package/dist/index.cjs ADDED
@@ -0,0 +1,345 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ McTable: () => McTableReact,
34
+ McTableReact: () => McTableReact
35
+ });
36
+ module.exports = __toCommonJS(index_exports);
37
+
38
+ // src/McTable.tsx
39
+ var React = __toESM(require("react"), 1);
40
+ var import_mctable = require("mctable");
41
+ var import_jsx_runtime = require("react/jsx-runtime");
42
+ var __sigIds = /* @__PURE__ */ new WeakMap();
43
+ var __nextSigId = 1;
44
+ function __getSigId(obj) {
45
+ const existing = __sigIds.get(obj);
46
+ if (existing !== void 0) return existing;
47
+ const next = __nextSigId++;
48
+ __sigIds.set(obj, next);
49
+ return next;
50
+ }
51
+ function __isPlainObject(value) {
52
+ if (!value || typeof value !== "object") return false;
53
+ const proto = Object.getPrototypeOf(value);
54
+ return proto === Object.prototype || proto === null;
55
+ }
56
+ function __signatureValue(value) {
57
+ if (value === null) return null;
58
+ const valueType = typeof value;
59
+ if (valueType === "string" || valueType === "number" || valueType === "boolean") return value;
60
+ if (valueType === "undefined") return ["u"];
61
+ if (valueType === "bigint") return ["bi", String(value)];
62
+ if (valueType === "symbol") return ["sy", String(value)];
63
+ if (valueType === "function") return ["fn", __getSigId(value)];
64
+ if (Array.isArray(value)) return value.map(__signatureValue);
65
+ if (value instanceof Date) return ["d", value.toISOString()];
66
+ if (!__isPlainObject(value)) {
67
+ return ["o", __getSigId(value), Object.prototype.toString.call(value)];
68
+ }
69
+ const obj = value;
70
+ const keys = Object.keys(obj).sort();
71
+ const out = {};
72
+ for (const key of keys) out[key] = __signatureValue(obj[key]);
73
+ return out;
74
+ }
75
+ function __stableSignature(value) {
76
+ try {
77
+ return JSON.stringify(__signatureValue(value));
78
+ } catch {
79
+ return "[unserializable]";
80
+ }
81
+ }
82
+ function __areRecordsEqual(prevRecords, nextRecords) {
83
+ if (prevRecords === nextRecords) return true;
84
+ if (prevRecords.length !== nextRecords.length) return false;
85
+ for (let index = 0; index < prevRecords.length; index++) {
86
+ if (__stableSignature(prevRecords[index]) !== __stableSignature(nextRecords[index])) {
87
+ return false;
88
+ }
89
+ }
90
+ return true;
91
+ }
92
+ function __areColumnsEqual(prevColumns, nextColumns) {
93
+ if (prevColumns === nextColumns) return true;
94
+ if (prevColumns.length !== nextColumns.length) return false;
95
+ for (let index = 0; index < prevColumns.length; index++) {
96
+ if (__stableSignature(prevColumns[index]) !== __stableSignature(nextColumns[index])) {
97
+ return false;
98
+ }
99
+ }
100
+ return true;
101
+ }
102
+ function __getValidationOnlyChanges({
103
+ prevRecords,
104
+ prevColumns,
105
+ nextRecords,
106
+ nextColumns
107
+ }) {
108
+ if (__stableSignature(prevRecords) !== __stableSignature(nextRecords)) return null;
109
+ if (prevColumns.length !== nextColumns.length) return null;
110
+ const validationChanges = [];
111
+ for (let index = 0; index < prevColumns.length; index++) {
112
+ const prevColumn = prevColumns[index];
113
+ const nextColumn = nextColumns[index];
114
+ const { validation: prevValidation, ...prevColumnWithoutValidation } = prevColumn;
115
+ const { validation: nextValidation, ...nextColumnWithoutValidation } = nextColumn;
116
+ if (__stableSignature(prevColumnWithoutValidation) !== __stableSignature(nextColumnWithoutValidation)) {
117
+ return null;
118
+ }
119
+ const prevRequired = prevValidation?.required;
120
+ const nextRequired = nextValidation?.required;
121
+ const prevTypeMatch = prevValidation?.typeMatch;
122
+ const nextTypeMatch = nextValidation?.typeMatch;
123
+ if (prevRequired === nextRequired && prevTypeMatch === nextTypeMatch) {
124
+ continue;
125
+ }
126
+ const settings = {
127
+ name: nextColumn.name
128
+ };
129
+ if (prevRequired !== nextRequired) {
130
+ settings.required = nextRequired ?? null;
131
+ }
132
+ if (prevTypeMatch !== nextTypeMatch) {
133
+ settings.typeMatch = nextTypeMatch ?? null;
134
+ }
135
+ validationChanges.push({
136
+ position: index + 1,
137
+ settings
138
+ });
139
+ }
140
+ if (validationChanges.length === 0) return null;
141
+ return validationChanges;
142
+ }
143
+ function __getWidthOnlyChanges({
144
+ prevRecords,
145
+ prevColumns,
146
+ nextRecords,
147
+ nextColumns
148
+ }) {
149
+ if (__stableSignature(prevRecords) !== __stableSignature(nextRecords)) return null;
150
+ if (prevColumns.length !== nextColumns.length) return null;
151
+ const widthChanges = [];
152
+ for (let index = 0; index < prevColumns.length; index++) {
153
+ const prevColumn = prevColumns[index];
154
+ const nextColumn = nextColumns[index];
155
+ const { width: _prevWidth, ...prevColumnWithoutWidth } = prevColumn;
156
+ const { width: _nextWidth, ...nextColumnWithoutWidth } = nextColumn;
157
+ if (__stableSignature(prevColumnWithoutWidth) !== __stableSignature(nextColumnWithoutWidth)) {
158
+ return null;
159
+ }
160
+ if (prevColumn.width !== nextColumn.width) {
161
+ widthChanges.push({
162
+ position: index + 1,
163
+ width: nextColumn.width ?? "reset"
164
+ });
165
+ }
166
+ }
167
+ if (widthChanges.length === 0) return null;
168
+ return widthChanges;
169
+ }
170
+ var __nextInstanceId = 1;
171
+ var useIsomorphicLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
172
+ var McTableReact = React.forwardRef(
173
+ ({
174
+ records,
175
+ columns,
176
+ settings,
177
+ license,
178
+ onReady,
179
+ onColumnResizeStart,
180
+ onColumnResizing,
181
+ onColumnResizeEnd,
182
+ onRowResizeStart,
183
+ onRowResizing,
184
+ onRowResizeEnd,
185
+ className,
186
+ style
187
+ }, ref) => {
188
+ const containerRef = React.useRef(null);
189
+ const tableRef = React.useRef(null);
190
+ const instanceIdRef = React.useRef(null);
191
+ const lastDataRef = React.useRef({
192
+ records: records ?? [],
193
+ columns: columns ?? []
194
+ });
195
+ const [readyTick, setReadyTick] = React.useState(0);
196
+ const effectiveSettings = React.useMemo(() => {
197
+ const hasAnyResizeCallback = onColumnResizeStart !== void 0 || onColumnResizing !== void 0 || onColumnResizeEnd !== void 0 || onRowResizeStart !== void 0 || onRowResizing !== void 0 || onRowResizeEnd !== void 0;
198
+ if (!settings && !hasAnyResizeCallback) return void 0;
199
+ if (!hasAnyResizeCallback) return settings;
200
+ const nextEvents = {
201
+ ...settings?.events ?? {}
202
+ };
203
+ if (onColumnResizeStart !== void 0) nextEvents.onColumnResizeStart = onColumnResizeStart;
204
+ if (onColumnResizing !== void 0) nextEvents.onColumnResizing = onColumnResizing;
205
+ if (onColumnResizeEnd !== void 0) nextEvents.onColumnResizeEnd = onColumnResizeEnd;
206
+ if (onRowResizeStart !== void 0) nextEvents.onRowResizeStart = onRowResizeStart;
207
+ if (onRowResizing !== void 0) nextEvents.onRowResizing = onRowResizing;
208
+ if (onRowResizeEnd !== void 0) nextEvents.onRowResizeEnd = onRowResizeEnd;
209
+ return {
210
+ ...settings ?? {},
211
+ events: nextEvents
212
+ };
213
+ }, [
214
+ settings,
215
+ onColumnResizeStart,
216
+ onColumnResizing,
217
+ onColumnResizeEnd,
218
+ onRowResizeStart,
219
+ onRowResizing,
220
+ onRowResizeEnd
221
+ ]);
222
+ React.useImperativeHandle(
223
+ ref,
224
+ () => ({
225
+ table: tableRef.current,
226
+ api: tableRef.current?.api ?? null
227
+ }),
228
+ [readyTick]
229
+ );
230
+ useIsomorphicLayoutEffect(() => {
231
+ if (typeof window === "undefined") return;
232
+ if (tableRef.current) {
233
+ return;
234
+ }
235
+ const container = containerRef.current;
236
+ if (!container) {
237
+ return;
238
+ }
239
+ const instanceId = __nextInstanceId++;
240
+ instanceIdRef.current = instanceId;
241
+ const initialRecords = records ?? lastDataRef.current.records;
242
+ const initialColumns = columns ?? lastDataRef.current.columns;
243
+ const instance = new import_mctable.McTable({
244
+ container,
245
+ records: initialRecords,
246
+ columns: initialColumns,
247
+ settings: effectiveSettings,
248
+ license
249
+ });
250
+ tableRef.current = instance;
251
+ setReadyTick((x) => x + 1);
252
+ onReady?.(instance);
253
+ return () => {
254
+ const current = tableRef.current;
255
+ const currentInstanceId = instanceIdRef.current;
256
+ tableRef.current = null;
257
+ instanceIdRef.current = null;
258
+ setReadyTick((x) => x + 1);
259
+ try {
260
+ const anyTable = current;
261
+ if (typeof anyTable?.destroy === "function") anyTable.destroy();
262
+ else if (typeof anyTable?.dispose === "function") anyTable.dispose();
263
+ } catch {
264
+ }
265
+ if (containerRef.current) {
266
+ containerRef.current.innerHTML = "";
267
+ }
268
+ };
269
+ }, []);
270
+ React.useEffect(() => {
271
+ const table = tableRef.current;
272
+ if (!table) {
273
+ return;
274
+ }
275
+ const prevRecords = lastDataRef.current.records;
276
+ const prevColumns = lastDataRef.current.columns;
277
+ const nextRecords = records !== void 0 ? records : prevRecords;
278
+ const nextColumns = columns !== void 0 ? columns : prevColumns;
279
+ const recordsChanged = records !== void 0 && (nextRecords.length !== prevRecords.length || !__areRecordsEqual(prevRecords, nextRecords));
280
+ const columnsChanged = columns !== void 0 && (nextColumns.length !== prevColumns.length || !__areColumnsEqual(prevColumns, nextColumns));
281
+ const didChange = recordsChanged || columnsChanged;
282
+ if (!didChange) {
283
+ return;
284
+ }
285
+ if (records !== void 0) {
286
+ lastDataRef.current.records = nextRecords;
287
+ }
288
+ if (columns !== void 0) {
289
+ lastDataRef.current.columns = nextColumns;
290
+ }
291
+ const widthOnlyChanges = __getWidthOnlyChanges({
292
+ prevRecords,
293
+ prevColumns,
294
+ nextRecords,
295
+ nextColumns
296
+ });
297
+ const validationOnlyChanges = __getValidationOnlyChanges({
298
+ prevRecords,
299
+ prevColumns,
300
+ nextRecords,
301
+ nextColumns
302
+ });
303
+ if (widthOnlyChanges) {
304
+ for (const change of widthOnlyChanges) {
305
+ table.api.setContentColumnWidth(change);
306
+ }
307
+ } else if (validationOnlyChanges) {
308
+ for (const change of validationOnlyChanges) {
309
+ table.api.updateContentColumnSettings(change.settings);
310
+ }
311
+ } else {
312
+ table.api.setData({
313
+ data: {
314
+ records: lastDataRef.current.records,
315
+ columns: lastDataRef.current.columns
316
+ }
317
+ });
318
+ }
319
+ }, [records, columns]);
320
+ React.useEffect(() => {
321
+ const table = tableRef.current;
322
+ if (!table) return;
323
+ if (!effectiveSettings) return;
324
+ table.api.updateSettings(effectiveSettings);
325
+ }, [effectiveSettings]);
326
+ const combinedClassName = ["mctable-react", className].filter(Boolean).join(" ");
327
+ const combinedStyle = {
328
+ width: "100%",
329
+ height: "100%",
330
+ display: "flex",
331
+ flex: "1 1 auto",
332
+ minWidth: 0,
333
+ minHeight: 0,
334
+ alignItems: "stretch",
335
+ ...style
336
+ };
337
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { ref: containerRef, className: combinedClassName, style: combinedStyle });
338
+ }
339
+ );
340
+ McTableReact.displayName = "McTable";
341
+ // Annotate the CommonJS export names for ESM import in node:
342
+ 0 && (module.exports = {
343
+ McTable,
344
+ McTableReact
345
+ });
@@ -0,0 +1,26 @@
1
+ import * as React from 'react';
2
+ import { MctColumn, MctInitSettings, MctLicense, McTable, MctRow, MctApi } from 'mctable';
3
+ export { McTableCtor, MctApi, MctColumn, MctColumnData, MctInitSettings, MctLicense, MctRow, MctScrollPosition, MctSettingDeleteMethod, MctSettingResizeHandleDimension, MctSettingScrollOverflow, MctSettingScrollOverflowOption, MctSettingScrollStop, MctSettingScrollStopOption, MctSettingScrollTransition } from 'mctable';
4
+
5
+ type McTableReactHandle = {
6
+ table: McTable | null;
7
+ api: MctApi | null;
8
+ };
9
+ type McTableReactProps = {
10
+ records?: any[];
11
+ columns?: MctColumn[];
12
+ settings?: MctInitSettings;
13
+ license?: MctLicense;
14
+ onReady?: (table: McTable) => void;
15
+ onColumnResizeStart?: (currentColumnWidth: number, column: MctColumn) => void;
16
+ onColumnResizing?: (currentColumnWidth: number, column: MctColumn) => void;
17
+ onColumnResizeEnd?: (newColumnWidth: number, column: MctColumn) => void;
18
+ onRowResizeStart?: (currentRowHeight: number, row: MctRow) => void;
19
+ onRowResizing?: (currentRowHeight: number, row: MctRow) => void;
20
+ onRowResizeEnd?: (newRowHeight: number, row: MctRow) => void;
21
+ className?: string;
22
+ style?: React.CSSProperties;
23
+ };
24
+ declare const McTableReact: React.ForwardRefExoticComponent<McTableReactProps & React.RefAttributes<McTableReactHandle>>;
25
+
26
+ export { McTableReact as McTable, McTableReact, type McTableReactHandle, type McTableReactProps };
package/package.json CHANGED
@@ -1,14 +1,17 @@
1
1
  {
2
2
  "name": "mctable-react",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "",
5
5
  "type": "module",
6
- "main": "./dist/index.js",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
7
8
  "types": "./dist/index.d.ts",
8
9
  "exports": {
9
10
  ".": {
10
11
  "types": "./dist/index.d.ts",
11
- "import": "./dist/index.js"
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs",
14
+ "default": "./dist/index.js"
12
15
  }
13
16
  },
14
17
  "files": [
@@ -16,7 +19,7 @@
16
19
  ],
17
20
  "scripts": {
18
21
  "test": "echo \"Error: no test specified\" && exit 1",
19
- "build": "tsup src/index.ts --format esm --dts --clean --out-dir dist --external react --external react-dom --external react/jsx-runtime --external react/jsx-dev-runtime",
22
+ "build": "tsup src/index.ts --format esm,cjs --dts --clean --out-dir dist --external react --external react-dom --external react/jsx-runtime --external react/jsx-dev-runtime",
20
23
  "start": "vite --open",
21
24
  "release:npm": "npm pack --pack-destination .",
22
25
  "prepack": "npm run build"
@@ -64,6 +67,6 @@
64
67
  "vite": "^5.4.12"
65
68
  },
66
69
  "dependencies": {
67
- "mctable": "^1.4.0"
70
+ "mctable": "^1.4.2"
68
71
  }
69
72
  }