beautiful-grid 1.0.12 → 1.0.13

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/cjs/BGrid.js CHANGED
@@ -759,7 +759,8 @@ function BGrid(_a) {
759
759
  ]);
760
760
  return (React.createElement(store_1.AppStoreProvider, { initialState: initialStoreState },
761
761
  React.createElement(TreeContext_1.TreeContext.Provider, { value: treeContextValue },
762
- React.createElement(Table_1.default, { ref: ref, columns: computedColumns,
762
+ React.createElement(Table_1.default, { ref: ref, sourceColumns: resolvedColumns,
763
+ columns: computedColumns,
763
764
  columnsGroup: visibilityProjection.columnsGroup,
764
765
  columnGroups: visibilityProjection.columnGroups,
765
766
  onChangeColumns: resolvedOnChangeColumns, width: width, height: height, className: className, style: style, loading: loading, disabled: resolvedDisabled, spinning: spinning, scrollLeft: scrollLeft, scrollTop: scrollTop, headerHeight: headerHeight, footerHeight: footerHeight, bottomBarHeight: resolvedBottomBarHeight,
@@ -166,6 +166,7 @@ function Table(props) {
166
166
  var _a;
167
167
  var _this = this;
168
168
  var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8;
169
+ var store = (0, store_1.useAppStoreApi)();
169
170
  var cellSelectionOptions = props.cellSelectionOptions, onChangeData = props.onChangeData, sourceIndexByVisibleIndex = props.sourceIndexByVisibleIndex;
170
171
  var cellSelectionEnabled = (_b = cellSelectionOptions === null || cellSelectionOptions === void 0 ? void 0 : cellSelectionOptions.enabled) !== null && _b !== void 0 ? _b : true;
171
172
  var rowKeyRegistryState = (0, react_1.useRef)({
@@ -657,12 +658,46 @@ function Table(props) {
657
658
  var scrollPropsRef = (0, react_1.useRef)({ top: props.scrollTop, left: props.scrollLeft });
658
659
  var syncedDataPropRef = (0, react_1.useRef)(props.data);
659
660
  var _23 = __read(React.useState(null), 2), rowScrollRequest = _23[0], setRowScrollRequest = _23[1];
661
+ var sourceColumnsRef = (0, react_1.useRef)(props.sourceColumns);
662
+ sourceColumnsRef.current = props.sourceColumns;
663
+ var getCurrentExportData = (0, react_1.useCallback)(function (options) {
664
+ var state = store.getState();
665
+ return (0, utils_1.createGridExportData)({
666
+ options: options,
667
+ columns: state.columns,
668
+ sourceColumns: sourceColumnsRef.current,
669
+ data: state.data,
670
+ sourceData: state.sourceData,
671
+ sourceIndexByVisibleIndex: state.sourceIndexByVisibleIndex,
672
+ visibleIndexBySourceIndex: state.visibleIndexBySourceIndex,
673
+ checkedIndexesMap: state.checkedIndexesMap,
674
+ rowKey: state.rowKey,
675
+ });
676
+ }, [store]);
660
677
  React.useImperativeHandle(props.ref, function () { return ({
661
678
  scrollToRow: function (rowIndex, options) {
662
679
  var _a;
663
680
  setRowScrollRequest({ rowIndex: rowIndex, align: (_a = options === null || options === void 0 ? void 0 : options.align) !== null && _a !== void 0 ? _a : 'nearest' });
664
681
  },
665
- }); }, []);
682
+ getExportData: function (options) {
683
+ return getCurrentExportData(options);
684
+ },
685
+ exportCsv: function (options) {
686
+ var _a;
687
+ var exportData = getCurrentExportData(options);
688
+ var csv = (0, utils_1.serializeGridExportDataToCsv)(exportData, options);
689
+ (0, utils_1.downloadCsv)(csv, (_a = options === null || options === void 0 ? void 0 : options.fileName) !== null && _a !== void 0 ? _a : 'export.csv');
690
+ },
691
+ exportExcel: function (options) {
692
+ var exportData = getCurrentExportData(options);
693
+ void Promise.resolve().then(function () { return __importStar(require('../utils/exportExcel')); }).then(function (_a) {
694
+ var _b;
695
+ var serializeGridExportDataToExcel = _a.serializeGridExportDataToExcel, downloadExcel = _a.downloadExcel;
696
+ var excelBytes = serializeGridExportDataToExcel(exportData, options);
697
+ downloadExcel(excelBytes, (_b = options === null || options === void 0 ? void 0 : options.fileName) !== null && _b !== void 0 ? _b : 'export.xlsx');
698
+ });
699
+ },
700
+ }); }, [getCurrentExportData]);
666
701
  var scrollRafRef = (0, react_1.useRef)(null);
667
702
  var scrollIdleTimerRef = (0, react_1.useRef)(null);
668
703
  var selectionAutoScrollRafRef = (0, react_1.useRef)(null);
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+ var __values = (this && this.__values) || function(o) {
3
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
4
+ if (m) return m.call(o);
5
+ if (o && typeof o.length === "number") return {
6
+ next: function () {
7
+ if (o && i >= o.length) o = void 0;
8
+ return { value: o && o[i++], done: !o };
9
+ }
10
+ };
11
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.formatCsvCellValue = formatCsvCellValue;
15
+ exports.escapeCsvCell = escapeCsvCell;
16
+ exports.serializeGridExportDataToCsv = serializeGridExportDataToCsv;
17
+ exports.downloadCsv = downloadCsv;
18
+ function formatCsvCellValue(value, preventFormulaInjection) {
19
+ if (preventFormulaInjection === void 0) { preventFormulaInjection = true; }
20
+ if (value === null || value === undefined) {
21
+ return '';
22
+ }
23
+ var formatted;
24
+ if (typeof value === 'string') {
25
+ formatted = value;
26
+ var startsWithFormula = /^[=+\-@]/.test(formatted);
27
+ var startsWithDangerousControl = /^[\t\r\n]/.test(formatted);
28
+ var hasFormulaAfterLeadingWhitespace = /^\s+[=+\-@]/.test(formatted);
29
+ if (preventFormulaInjection &&
30
+ (startsWithFormula || startsWithDangerousControl || hasFormulaAfterLeadingWhitespace)) {
31
+ formatted = "'".concat(formatted);
32
+ }
33
+ return formatted;
34
+ }
35
+ if (typeof value === 'number') {
36
+ return String(value);
37
+ }
38
+ if (typeof value === 'boolean') {
39
+ return value ? 'true' : 'false';
40
+ }
41
+ if (typeof value === 'bigint') {
42
+ return value.toString();
43
+ }
44
+ if (value instanceof Date) {
45
+ return isNaN(value.getTime()) ? 'Invalid Date' : value.toISOString();
46
+ }
47
+ if (typeof value === 'object') {
48
+ try {
49
+ formatted = JSON.stringify(value);
50
+ }
51
+ catch (_a) {
52
+ formatted = String(value);
53
+ }
54
+ return formatted;
55
+ }
56
+ return String(value);
57
+ }
58
+ function escapeCsvCell(cell, delimiter) {
59
+ if (delimiter === void 0) { delimiter = ','; }
60
+ var needsQuotes = cell.includes(delimiter) ||
61
+ cell.includes('"') ||
62
+ cell.includes('\r') ||
63
+ cell.includes('\n');
64
+ if (needsQuotes) {
65
+ return "\"".concat(cell.replace(/"/g, '""'), "\"");
66
+ }
67
+ return cell;
68
+ }
69
+ function serializeGridExportDataToCsv(data, options) {
70
+ var e_1, _a;
71
+ var _b, _c, _d, _e, _f;
72
+ var delimiter = (_b = options === null || options === void 0 ? void 0 : options.delimiter) !== null && _b !== void 0 ? _b : ',';
73
+ var newline = (_c = options === null || options === void 0 ? void 0 : options.newline) !== null && _c !== void 0 ? _c : '\r\n';
74
+ var includeHeader = (_d = options === null || options === void 0 ? void 0 : options.includeHeader) !== null && _d !== void 0 ? _d : true;
75
+ var bom = (_e = options === null || options === void 0 ? void 0 : options.bom) !== null && _e !== void 0 ? _e : true;
76
+ var preventFormulaInjection = (_f = options === null || options === void 0 ? void 0 : options.preventFormulaInjection) !== null && _f !== void 0 ? _f : true;
77
+ var lines = [];
78
+ if (includeHeader) {
79
+ var headerLine = data.columns
80
+ .map(function (col) { return escapeCsvCell(formatCsvCellValue(col.header, preventFormulaInjection), delimiter); })
81
+ .join(delimiter);
82
+ lines.push(headerLine);
83
+ }
84
+ try {
85
+ for (var _g = __values(data.rows), _h = _g.next(); !_h.done; _h = _g.next()) {
86
+ var row = _h.value;
87
+ var rowLine = row.cells
88
+ .map(function (cell) { return escapeCsvCell(formatCsvCellValue(cell, preventFormulaInjection), delimiter); })
89
+ .join(delimiter);
90
+ lines.push(rowLine);
91
+ }
92
+ }
93
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
94
+ finally {
95
+ try {
96
+ if (_h && !_h.done && (_a = _g.return)) _a.call(_g);
97
+ }
98
+ finally { if (e_1) throw e_1.error; }
99
+ }
100
+ var csvText = lines.join(newline);
101
+ return (bom ? '\uFEFF' : '') + csvText;
102
+ }
103
+ function downloadCsv(csv, fileName) {
104
+ if (fileName === void 0) { fileName = 'export.csv'; }
105
+ if (typeof window === 'undefined' || typeof document === 'undefined') {
106
+ return;
107
+ }
108
+ var resolvedName = (fileName || '').trim();
109
+ if (!resolvedName) {
110
+ resolvedName = 'export.csv';
111
+ }
112
+ else if (!resolvedName.toLowerCase().endsWith('.csv')) {
113
+ resolvedName += '.csv';
114
+ }
115
+ var blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
116
+ if (typeof URL !== 'undefined' && typeof URL.createObjectURL === 'function') {
117
+ var url = URL.createObjectURL(blob);
118
+ var link = document.createElement('a');
119
+ link.href = url;
120
+ link.setAttribute('download', resolvedName);
121
+ link.style.display = 'none';
122
+ document.body.appendChild(link);
123
+ link.click();
124
+ document.body.removeChild(link);
125
+ if (typeof URL.revokeObjectURL === 'function') {
126
+ URL.revokeObjectURL(url);
127
+ }
128
+ }
129
+ }
@@ -0,0 +1,230 @@
1
+ "use strict";
2
+ var __values = (this && this.__values) || function(o) {
3
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
4
+ if (m) return m.call(o);
5
+ if (o && typeof o.length === "number") return {
6
+ next: function () {
7
+ if (o && i >= o.length) o = void 0;
8
+ return { value: o && o[i++], done: !o };
9
+ }
10
+ };
11
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
12
+ };
13
+ var __read = (this && this.__read) || function (o, n) {
14
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
15
+ if (!m) return o;
16
+ var i = m.call(o), r, ar = [], e;
17
+ try {
18
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
+ }
20
+ catch (error) { e = { error: error }; }
21
+ finally {
22
+ try {
23
+ if (r && !r.done && (m = i["return"])) m.call(i);
24
+ }
25
+ finally { if (e) throw e.error; }
26
+ }
27
+ return ar;
28
+ };
29
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
30
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
31
+ if (ar || !(i in from)) {
32
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
33
+ ar[i] = from[i];
34
+ }
35
+ }
36
+ return to.concat(ar || Array.prototype.slice.call(from));
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.resolveExportColumns = resolveExportColumns;
40
+ exports.resolveExportRows = resolveExportRows;
41
+ exports.createGridExportData = createGridExportData;
42
+ var getColumnId_1 = require("./getColumnId");
43
+ var getCellValue_1 = require("./getCellValue");
44
+ function resolveExportColumns(columns, sourceColumns, scope) {
45
+ var e_1, _a, e_2, _b;
46
+ if (scope === void 0) { scope = 'visible'; }
47
+ var selectedColumns;
48
+ if (Array.isArray(scope)) {
49
+ var candidatePool = sourceColumns && sourceColumns.length > 0 ? sourceColumns : columns;
50
+ var columnMap = new Map();
51
+ try {
52
+ for (var candidatePool_1 = __values(candidatePool), candidatePool_1_1 = candidatePool_1.next(); !candidatePool_1_1.done; candidatePool_1_1 = candidatePool_1.next()) {
53
+ var col = candidatePool_1_1.value;
54
+ columnMap.set((0, getColumnId_1.getColumnId)(col), col);
55
+ }
56
+ }
57
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
58
+ finally {
59
+ try {
60
+ if (candidatePool_1_1 && !candidatePool_1_1.done && (_a = candidatePool_1.return)) _a.call(candidatePool_1);
61
+ }
62
+ finally { if (e_1) throw e_1.error; }
63
+ }
64
+ selectedColumns = [];
65
+ try {
66
+ for (var scope_1 = __values(scope), scope_1_1 = scope_1.next(); !scope_1_1.done; scope_1_1 = scope_1.next()) {
67
+ var id = scope_1_1.value;
68
+ var col = columnMap.get(id);
69
+ if (col) {
70
+ selectedColumns.push(col);
71
+ }
72
+ }
73
+ }
74
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
75
+ finally {
76
+ try {
77
+ if (scope_1_1 && !scope_1_1.done && (_b = scope_1.return)) _b.call(scope_1);
78
+ }
79
+ finally { if (e_2) throw e_2.error; }
80
+ }
81
+ }
82
+ else if (scope === 'all') {
83
+ selectedColumns = sourceColumns && sourceColumns.length > 0 ? __spreadArray([], __read(sourceColumns), false) : __spreadArray([], __read(columns), false);
84
+ }
85
+ else {
86
+ // 'visible'
87
+ selectedColumns = __spreadArray([], __read(columns), false);
88
+ }
89
+ var exportableColumns = selectedColumns.filter(function (col) { return col.exportable !== false; });
90
+ return exportableColumns.map(function (column, columnIndex) {
91
+ var columnId = (0, getColumnId_1.getColumnId)(column);
92
+ var header;
93
+ if (typeof column.exportHeader === 'function') {
94
+ header = column.exportHeader(column);
95
+ }
96
+ else if (typeof column.exportHeader === 'string') {
97
+ header = column.exportHeader;
98
+ }
99
+ else if (typeof column.label === 'string' || typeof column.label === 'number') {
100
+ header = String(column.label);
101
+ }
102
+ else {
103
+ header = columnId;
104
+ }
105
+ return {
106
+ columnId: columnId,
107
+ header: header,
108
+ column: column,
109
+ columnIndex: columnIndex,
110
+ };
111
+ });
112
+ }
113
+ function resolveExportRows(exportColumns, params) {
114
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
115
+ var rowScope = (_b = (_a = params.options) === null || _a === void 0 ? void 0 : _a.rows) !== null && _b !== void 0 ? _b : 'displayed';
116
+ var rows = [];
117
+ var sourcePool = params.sourceData && params.sourceData.length > 0 ? params.sourceData : params.data;
118
+ var buildCells = function (item, values, sourceIndex, visibleIndex) {
119
+ var e_3, _a;
120
+ var cells = [];
121
+ try {
122
+ for (var exportColumns_1 = __values(exportColumns), exportColumns_1_1 = exportColumns_1.next(); !exportColumns_1_1.done; exportColumns_1_1 = exportColumns_1.next()) {
123
+ var expCol = exportColumns_1_1.value;
124
+ var rawValue = (0, getCellValue_1.getCellValueByRowKey)(expCol.column.key, values);
125
+ if (expCol.column.getExportValue) {
126
+ cells.push(expCol.column.getExportValue({
127
+ column: expCol.column,
128
+ columnId: expCol.columnId,
129
+ visibleIndex: visibleIndex,
130
+ sourceIndex: sourceIndex,
131
+ item: item,
132
+ values: values,
133
+ value: rawValue,
134
+ }));
135
+ }
136
+ else {
137
+ cells.push(rawValue);
138
+ }
139
+ }
140
+ }
141
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
142
+ finally {
143
+ try {
144
+ if (exportColumns_1_1 && !exportColumns_1_1.done && (_a = exportColumns_1.return)) _a.call(exportColumns_1);
145
+ }
146
+ finally { if (e_3) throw e_3.error; }
147
+ }
148
+ return cells;
149
+ };
150
+ var resolveRowKey = function (item, values) {
151
+ var rowKey;
152
+ if (params.rowKey !== undefined && values) {
153
+ var rawKey = (0, getCellValue_1.getCellValueByRowKey)(params.rowKey, values);
154
+ if (rawKey !== undefined && rawKey !== null) {
155
+ rowKey = rawKey;
156
+ }
157
+ }
158
+ if (rowKey === undefined && (item === null || item === void 0 ? void 0 : item.key) !== undefined) {
159
+ rowKey = item.key;
160
+ }
161
+ return rowKey;
162
+ };
163
+ if (rowScope === 'displayed') {
164
+ var displayData = params.data;
165
+ for (var visibleIndex = 0; visibleIndex < displayData.length; visibleIndex++) {
166
+ var item = (_c = displayData[visibleIndex]) !== null && _c !== void 0 ? _c : { values: {} };
167
+ var sourceIndex = (_e = (_d = params.sourceIndexByVisibleIndex) === null || _d === void 0 ? void 0 : _d[visibleIndex]) !== null && _e !== void 0 ? _e : visibleIndex;
168
+ var values = (_f = item.values) !== null && _f !== void 0 ? _f : {};
169
+ var rowKey = resolveRowKey(item, values);
170
+ var cells = buildCells(item, values, sourceIndex, visibleIndex);
171
+ rows.push({
172
+ visibleIndex: visibleIndex,
173
+ sourceIndex: sourceIndex,
174
+ rowKey: rowKey,
175
+ item: item,
176
+ values: values,
177
+ cells: cells,
178
+ });
179
+ }
180
+ }
181
+ else if (rowScope === 'checked') {
182
+ var checkedMap = params.checkedIndexesMap;
183
+ for (var sourceIndex = 0; sourceIndex < sourcePool.length; sourceIndex++) {
184
+ if (!(checkedMap === null || checkedMap === void 0 ? void 0 : checkedMap.has(sourceIndex)))
185
+ continue;
186
+ var item = (_g = sourcePool[sourceIndex]) !== null && _g !== void 0 ? _g : { values: {} };
187
+ var visibleIndex = (_h = params.visibleIndexBySourceIndex) === null || _h === void 0 ? void 0 : _h.get(sourceIndex);
188
+ var values = (_j = item.values) !== null && _j !== void 0 ? _j : {};
189
+ var rowKey = resolveRowKey(item, values);
190
+ var cells = buildCells(item, values, sourceIndex, visibleIndex);
191
+ rows.push({
192
+ visibleIndex: visibleIndex,
193
+ sourceIndex: sourceIndex,
194
+ rowKey: rowKey,
195
+ item: item,
196
+ values: values,
197
+ cells: cells,
198
+ });
199
+ }
200
+ }
201
+ else {
202
+ // 'source'
203
+ for (var sourceIndex = 0; sourceIndex < sourcePool.length; sourceIndex++) {
204
+ var item = (_k = sourcePool[sourceIndex]) !== null && _k !== void 0 ? _k : { values: {} };
205
+ var visibleIndex = (_l = params.visibleIndexBySourceIndex) === null || _l === void 0 ? void 0 : _l.get(sourceIndex);
206
+ var values = (_m = item.values) !== null && _m !== void 0 ? _m : {};
207
+ var rowKey = resolveRowKey(item, values);
208
+ var cells = buildCells(item, values, sourceIndex, visibleIndex);
209
+ rows.push({
210
+ visibleIndex: visibleIndex,
211
+ sourceIndex: sourceIndex,
212
+ rowKey: rowKey,
213
+ item: item,
214
+ values: values,
215
+ cells: cells,
216
+ });
217
+ }
218
+ }
219
+ return rows;
220
+ }
221
+ function createGridExportData(params) {
222
+ var _a, _b;
223
+ var columnScope = (_b = (_a = params.options) === null || _a === void 0 ? void 0 : _a.columns) !== null && _b !== void 0 ? _b : 'visible';
224
+ var columns = resolveExportColumns(params.columns, params.sourceColumns, columnScope);
225
+ var rows = resolveExportRows(columns, params);
226
+ return {
227
+ columns: columns,
228
+ rows: rows,
229
+ };
230
+ }