nillud-data-table 1.0.0 → 1.0.2

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/index.mjs DELETED
@@ -1,796 +0,0 @@
1
- // components/DataTable.tsx
2
- import { useImperativeHandle, useEffect as useEffect2, useState, useCallback, useMemo as useMemo2, forwardRef } from "react";
3
-
4
- // components/TableHeader.tsx
5
- import React from "react";
6
-
7
- // components/Column.tsx
8
- import { useMemo } from "react";
9
-
10
- // components/img/SortDown.tsx
11
- import { jsx } from "react/jsx-runtime";
12
- var SortDown = () => {
13
- return /* @__PURE__ */ jsx("svg", { xmlns: "http://www.w3.org/2000/svg", width: "16", height: "16", fill: "currentColor", className: "bi bi-caret-down-fill", viewBox: "0 0 16 16", children: /* @__PURE__ */ jsx("path", { d: "M7.247 11.14 2.451 5.658C1.885 5.013 2.345 4 3.204 4h9.592a1 1 0 0 1 .753 1.659l-4.796 5.48a1 1 0 0 1-1.506 0z" }) });
14
- };
15
- var SortDown_default = SortDown;
16
-
17
- // components/img/SortUp.tsx
18
- import { jsx as jsx2 } from "react/jsx-runtime";
19
- var SortUp = () => {
20
- return /* @__PURE__ */ jsx2("svg", { xmlns: "http://www.w3.org/2000/svg", width: "16", height: "16", fill: "currentColor", className: "bi bi-caret-up-fill", viewBox: "0 0 16 16", children: /* @__PURE__ */ jsx2("path", { d: "m7.247 4.86-4.796 5.481c-.566.647-.106 1.659.753 1.659h9.592a1 1 0 0 0 .753-1.659l-4.796-5.48a1 1 0 0 0-1.506 0z" }) });
21
- };
22
- var SortUp_default = SortUp;
23
-
24
- // components/Column.tsx
25
- import { jsx as jsx3, jsxs } from "react/jsx-runtime";
26
- var Column = ({ column, getSortField, sortBy, getFilters, filters }) => {
27
- var _a;
28
- const currentSort = useMemo(() => {
29
- return sortBy.col === column.field ? sortBy.type : null;
30
- }, [sortBy, column.field]);
31
- const toggleSort = () => {
32
- const nextType = currentSort === "asc" ? "desc" : "asc";
33
- getSortField({ col: column.field, type: nextType });
34
- };
35
- const onFilterChange = (e) => {
36
- getFilters({ ...filters, [column.field]: e.target.value });
37
- };
38
- return /* @__PURE__ */ jsxs("div", { className: "ndt-column", children: [
39
- /* @__PURE__ */ jsxs("div", { className: "ndt-column-head", children: [
40
- /* @__PURE__ */ jsx3("span", { children: column.title }),
41
- typeof column.autoinc === "undefined" && (typeof column.sortable === "undefined" || column.sortable) && /* @__PURE__ */ jsx3("div", { className: "ndt-sorter", onClick: toggleSort, children: currentSort === "asc" ? /* @__PURE__ */ jsx3(SortDown_default, {}) : currentSort === "desc" ? /* @__PURE__ */ jsx3(SortUp_default, {}) : null })
42
- ] }),
43
- /* @__PURE__ */ jsx3("div", { className: "ndt-column-footer", children: typeof column.autoinc === "undefined" && (typeof column.filterable === "undefined" || column.filterable) && /* @__PURE__ */ jsx3(
44
- "input",
45
- {
46
- type: "text",
47
- value: (_a = filters[column.field]) != null ? _a : "",
48
- onChange: onFilterChange,
49
- placeholder: "\u0424\u0438\u043B\u044C\u0442\u0440..."
50
- }
51
- ) })
52
- ] });
53
- };
54
- var Column_default = Column;
55
-
56
- // components/TableHeader.tsx
57
- import { Fragment, jsx as jsx4, jsxs as jsxs2 } from "react/jsx-runtime";
58
- var Header = ({ columns, getSortField, sortBy, getFilters, filters, widths, headerGroup }) => {
59
- const renderHeaderGroup = () => headerGroup && /* @__PURE__ */ jsx4("div", { className: "ndt-table-columns", style: { gridTemplateColumns: widths || "auto" }, children: headerGroup.map((col, id) => /* @__PURE__ */ jsx4("div", { className: "ndt-column", style: { gridColumn: `span ${col.cols || 1}` }, children: /* @__PURE__ */ jsx4("div", { className: "ndt-column-head", children: /* @__PURE__ */ jsx4("span", { children: col.title }) }) }, `header-group-${id}`)) });
60
- const renderColumns = () => columns && columns.length > 0 ? columns.map((column, id) => /* @__PURE__ */ jsx4(
61
- Column_default,
62
- {
63
- column,
64
- getSortField,
65
- sortBy,
66
- getFilters,
67
- filters
68
- },
69
- `column-${id}`
70
- )) : /* @__PURE__ */ jsx4("div", { className: "ndt-data-error", children: "\u041E\u0448\u0438\u0431\u043A\u0430: columns is undefined" });
71
- return /* @__PURE__ */ jsxs2(Fragment, { children: [
72
- renderHeaderGroup(),
73
- /* @__PURE__ */ jsx4("div", { className: "ndt-table-columns", style: { gridTemplateColumns: widths || "auto" }, children: renderColumns() })
74
- ] });
75
- };
76
- var TableHeader_default = React.memo(Header);
77
-
78
- // components/TableBody.tsx
79
- import React2 from "react";
80
-
81
- // components/Cell.tsx
82
- import { jsx as jsx5 } from "react/jsx-runtime";
83
- var Cell = ({
84
- row,
85
- column,
86
- rowId,
87
- isTitles
88
- }) => {
89
- const rawValue = row[column.field];
90
- const stringValue = typeof rawValue !== "undefined" && rawValue !== null ? String(rawValue) : "";
91
- const content = column.formatter ? column.formatter(stringValue, row) : typeof column.autoinc !== "undefined" ? /* @__PURE__ */ jsx5("span", { children: rowId + 1 }) : /* @__PURE__ */ jsx5("span", { children: stringValue });
92
- return /* @__PURE__ */ jsx5(
93
- "div",
94
- {
95
- className: "ndt-cell",
96
- title: isTitles && stringValue ? stringValue : "",
97
- children: content
98
- }
99
- );
100
- };
101
- var Cell_default = Cell;
102
-
103
- // components/Row.tsx
104
- import { jsx as jsx6 } from "react/jsx-runtime";
105
- var Row = ({ rowId, columns, row, widths, isTitles }) => /* @__PURE__ */ jsx6("div", { className: "ndt-table-row", style: { gridTemplateColumns: widths }, children: columns.map((column, id) => /* @__PURE__ */ jsx6(
106
- Cell_default,
107
- {
108
- row,
109
- column,
110
- rowId,
111
- isTitles
112
- },
113
- `cell-${rowId}-${id}`
114
- )) });
115
- var Row_default = Row;
116
-
117
- // utils/groupDataBy.ts
118
- var groupDataBy = (data, key) => {
119
- const groups = /* @__PURE__ */ new Map();
120
- data.forEach((item) => {
121
- const groupKey = item[key] || "\u2014";
122
- if (!groups.has(String(groupKey))) {
123
- groups.set(String(groupKey), []);
124
- }
125
- groups.get(String(groupKey)).push(item);
126
- });
127
- return Array.from(groups.entries()).map(([key2, items]) => ({ key: key2, items }));
128
- };
129
-
130
- // components/TableBody.tsx
131
- import { jsx as jsx7, jsxs as jsxs3 } from "react/jsx-runtime";
132
- var TableBody = ({
133
- columns,
134
- tableData,
135
- scrollable,
136
- scrollHeight,
137
- widths,
138
- groupBy,
139
- collapsedGroups = {},
140
- toggleGroup,
141
- isTitles
142
- }) => {
143
- const grouped = groupBy ? groupDataBy(tableData, groupBy) : [];
144
- if (!tableData || tableData.length === 0) {
145
- return /* @__PURE__ */ jsx7("div", { className: `ndt-table-body${scrollable ? " ndt-table-body-scrollable" : ""}`, style: scrollable ? { height: scrollHeight } : {}, children: /* @__PURE__ */ jsx7("div", { className: "ndt-table-row", style: { height: "100%" }, children: /* @__PURE__ */ jsx7("div", { className: "ndt-row-item", style: { margin: "auto", padding: 20, fontWeight: "bold" }, children: "\u0414\u0430\u043D\u043D\u044B\u0445 \u043D\u0435\u0442" }) }) });
146
- }
147
- const renderGroupedRows = () => grouped.map((group, id) => /* @__PURE__ */ jsxs3(React2.Fragment, { children: [
148
- /* @__PURE__ */ jsxs3(
149
- "div",
150
- {
151
- className: "ndt-group-header",
152
- onClick: () => toggleGroup == null ? void 0 : toggleGroup(group.key),
153
- children: [
154
- /* @__PURE__ */ jsx7("span", { style: { marginRight: 8 }, children: collapsedGroups[group.key] ? "\u25B6" : "\u25BC" }),
155
- group.key,
156
- " (",
157
- group.items.length,
158
- ")"
159
- ]
160
- }
161
- ),
162
- !collapsedGroups[group.key] && group.items.map((element, id2) => /* @__PURE__ */ jsx7(
163
- Row_default,
164
- {
165
- rowId: id2,
166
- row: element,
167
- columns,
168
- widths,
169
- isTitles
170
- },
171
- `row-${group.key}-${id2}`
172
- ))
173
- ] }, `row-${group.key}-${id}`));
174
- const renderFlatRows = () => tableData.map((element, id) => /* @__PURE__ */ jsx7(
175
- Row_default,
176
- {
177
- rowId: id,
178
- row: element,
179
- columns,
180
- widths
181
- },
182
- `row-${id}`
183
- ));
184
- return /* @__PURE__ */ jsx7("div", { className: `ndt-table-body${scrollable ? " ndt-table-body-scrollable" : ""}`, style: scrollable ? { height: scrollHeight } : {}, children: groupBy ? renderGroupedRows() : renderFlatRows() });
185
- };
186
- var TableBody_default = TableBody;
187
-
188
- // components/img/NextIcon.tsx
189
- import { jsx as jsx8 } from "react/jsx-runtime";
190
- var NextIcon = () => {
191
- return /* @__PURE__ */ jsx8("svg", { width: "41", height: "65", viewBox: "0 0 41 65", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: /* @__PURE__ */ jsx8("path", { d: "M0.674316 57.2669L25.3872 32.5L0.674316 7.73312L8.28244 0.125L40.6574 32.5L8.28244 64.875L0.674316 57.2669Z", fill: "#666666" }) });
192
- };
193
- var NextIcon_default = NextIcon;
194
-
195
- // components/img/LastIcon.tsx
196
- import { jsx as jsx9 } from "react/jsx-runtime";
197
- var LastIcon = () => {
198
- return /* @__PURE__ */ jsx9("svg", { width: "68", height: "65", viewBox: "0 0 68 65", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: /* @__PURE__ */ jsx9("path", { d: "M0.185059 7.73312L24.9519 32.5L0.185059 57.2669L7.79318 64.875L40.1682 32.5L7.79318 0.125L0.185059 7.73312ZM56.3557 0.125H67.1474V64.875H56.3557V0.125Z", fill: "#666666" }) });
199
- };
200
- var LastIcon_default = LastIcon;
201
-
202
- // components/img/PrevIcon.tsx
203
- import { jsx as jsx10 } from "react/jsx-runtime";
204
- var PrevIcon = () => {
205
- return /* @__PURE__ */ jsx10("svg", { width: "41", height: "65", viewBox: "0 0 41 65", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: /* @__PURE__ */ jsx10("path", { d: "M40.6574 57.2669L15.9445 32.5L40.6574 7.73312L33.0493 0.125L0.674316 32.5L33.0493 64.875L40.6574 57.2669Z", fill: "#666666" }) });
206
- };
207
- var PrevIcon_default = PrevIcon;
208
-
209
- // components/img/FirstIcon.tsx
210
- import { jsx as jsx11 } from "react/jsx-runtime";
211
- var FirstIcon = () => {
212
- return /* @__PURE__ */ jsx11("svg", { width: "68", height: "65", viewBox: "0 0 68 65", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: /* @__PURE__ */ jsx11("path", { d: "M67.1474 57.2669L42.3805 32.5L67.1474 7.73312L59.5392 0.125L27.1642 32.5L59.5392 64.875L67.1474 57.2669ZM0.185059 0.125H10.9767V64.875H0.185059V0.125Z", fill: "#666666" }) });
213
- };
214
- var FirstIcon_default = FirstIcon;
215
-
216
- // components/TableFooter.tsx
217
- import { jsx as jsx12, jsxs as jsxs4 } from "react/jsx-runtime";
218
- var TableFooter = ({
219
- tableData,
220
- paginationCounts,
221
- paginationSize,
222
- getPaginationSize,
223
- paginationPage,
224
- getPaginationPage
225
- }) => {
226
- const totalItems = tableData.length;
227
- const totalPages = paginationSize === 0 ? 1 : Math.ceil(totalItems / paginationSize);
228
- if (totalItems === 0) return null;
229
- const handleCountChange = (e) => {
230
- getPaginationSize(Number(e.target.value));
231
- };
232
- const handlePageChange = (newPage) => {
233
- if (newPage >= 0 && newPage < totalPages) {
234
- getPaginationPage(newPage);
235
- }
236
- };
237
- const renderPageNumbers = () => {
238
- if (totalPages <= 1) return null;
239
- const pages = [];
240
- const maxVisible = 5;
241
- let start = Math.max(0, paginationPage - Math.floor(maxVisible / 2));
242
- const end = Math.min(totalPages - 1, start + maxVisible - 1);
243
- if (end - start + 1 < maxVisible) {
244
- start = Math.max(0, end - maxVisible + 1);
245
- }
246
- if (start > 0) {
247
- pages.push(
248
- /* @__PURE__ */ jsx12("button", { onClick: () => handlePageChange(0), children: "1" }, "page-0")
249
- );
250
- if (start > 1) {
251
- pages.push(/* @__PURE__ */ jsx12("span", { children: "..." }, "ellipsis-start"));
252
- }
253
- }
254
- for (let i = start; i <= end; i++) {
255
- pages.push(
256
- /* @__PURE__ */ jsx12(
257
- "button",
258
- {
259
- className: i === paginationPage ? "btn-active" : "",
260
- onClick: () => handlePageChange(i),
261
- disabled: i === paginationPage,
262
- children: i + 1
263
- },
264
- `page-${i}`
265
- )
266
- );
267
- }
268
- if (end < totalPages - 1) {
269
- if (end < totalPages - 2) {
270
- pages.push(/* @__PURE__ */ jsx12("span", { children: "..." }, "ellipsis-end"));
271
- }
272
- pages.push(
273
- /* @__PURE__ */ jsx12("button", { onClick: () => handlePageChange(totalPages - 1), children: totalPages }, `page-${totalPages - 1}`)
274
- );
275
- }
276
- return pages;
277
- };
278
- const firstItem = paginationSize === 0 ? 1 : paginationPage * paginationSize + 1;
279
- const lastItem = paginationSize === 0 ? totalItems : Math.min((paginationPage + 1) * paginationSize, totalItems);
280
- return /* @__PURE__ */ jsxs4("div", { className: "ndt-table-footer", children: [
281
- /* @__PURE__ */ jsxs4("div", { className: "ndt-footer-count", children: [
282
- /* @__PURE__ */ jsxs4("span", { children: [
283
- "\u041F\u043E\u043A\u0430\u0437\u0430\u043D\u044B \u0441\u0442\u0440\u043E\u043A\u0438 \u0441 ",
284
- firstItem,
285
- " \u043F\u043E ",
286
- lastItem,
287
- ", "
288
- ] }),
289
- /* @__PURE__ */ jsxs4("span", { children: [
290
- "\u0412\u0441\u0435\u0433\u043E: ",
291
- totalItems
292
- ] })
293
- ] }),
294
- paginationCounts && /* @__PURE__ */ jsxs4("div", { className: "ndt-footer-pagination", children: [
295
- /* @__PURE__ */ jsxs4("div", { className: "ndt-pagination-counts", children: [
296
- /* @__PURE__ */ jsx12("span", { children: "\u041F\u043E\u043A\u0430\u0437\u044B\u0432\u0430\u0442\u044C \u0441\u0442\u0440\u043E\u043A: " }),
297
- /* @__PURE__ */ jsx12("select", { value: paginationSize, onChange: handleCountChange, children: paginationCounts.map((count) => /* @__PURE__ */ jsx12("option", { value: count, children: count === 0 ? "\u0412\u0441\u0435" : count }, `count-${count}`)) })
298
- ] }),
299
- /* @__PURE__ */ jsxs4("div", { className: "ndt-pagination-buttons", children: [
300
- /* @__PURE__ */ jsx12(
301
- "button",
302
- {
303
- disabled: paginationPage === 0,
304
- onClick: () => handlePageChange(0),
305
- "aria-label": "\u041F\u0435\u0440\u0432\u0430\u044F \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u0430",
306
- children: /* @__PURE__ */ jsx12(FirstIcon_default, {})
307
- }
308
- ),
309
- /* @__PURE__ */ jsx12(
310
- "button",
311
- {
312
- disabled: paginationPage === 0,
313
- onClick: () => handlePageChange(paginationPage - 1),
314
- "aria-label": "\u041F\u0440\u0435\u0434\u044B\u0434\u0443\u0449\u0430\u044F \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u0430",
315
- children: /* @__PURE__ */ jsx12(PrevIcon_default, {})
316
- }
317
- ),
318
- /* @__PURE__ */ jsx12("div", { className: "ndt-buttons-num", children: renderPageNumbers() }),
319
- /* @__PURE__ */ jsx12(
320
- "button",
321
- {
322
- disabled: paginationPage >= totalPages - 1,
323
- onClick: () => handlePageChange(paginationPage + 1),
324
- "aria-label": "\u0421\u043B\u0435\u0434\u0443\u044E\u0449\u0430\u044F \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u0430",
325
- children: /* @__PURE__ */ jsx12(NextIcon_default, {})
326
- }
327
- ),
328
- /* @__PURE__ */ jsx12(
329
- "button",
330
- {
331
- disabled: paginationPage >= totalPages - 1,
332
- onClick: () => handlePageChange(totalPages - 1),
333
- "aria-label": "\u041F\u043E\u0441\u043B\u0435\u0434\u043D\u044F\u044F \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u0430",
334
- children: /* @__PURE__ */ jsx12(LastIcon_default, {})
335
- }
336
- )
337
- ] })
338
- ] })
339
- ] });
340
- };
341
- var TableFooter_default = TableFooter;
342
-
343
- // components/functions/sort-data.ts
344
- var sortByAsc = (data, column) => {
345
- const sortedData = data.sort((a, b) => {
346
- if (!isNaN(+a[column]) && !isNaN(+b[column])) {
347
- return +a[column] - +b[column];
348
- }
349
- if (`${a[column]}`.toLowerCase() < `${b[column]}`.toLowerCase()) {
350
- return -1;
351
- }
352
- if (`${a[column]}`.toLowerCase() > `${b[column]}`.toLowerCase()) {
353
- return 1;
354
- }
355
- return 0;
356
- });
357
- return sortedData;
358
- };
359
- var sortByDesc = (data, column) => {
360
- const sortedData = data.sort((a, b) => {
361
- if (!isNaN(+a[column]) && !isNaN(+b[column])) {
362
- return +b[column] - +a[column];
363
- }
364
- if (`${a[column]}`.toLowerCase() > `${b[column]}`.toLowerCase()) {
365
- return -1;
366
- }
367
- if (`${a[column]}`.toLowerCase() < `${b[column]}`.toLowerCase()) {
368
- return 1;
369
- }
370
- return 0;
371
- });
372
- return sortedData;
373
- };
374
- var sortData = (data, col, type) => {
375
- if (type === "asc") {
376
- return sortByAsc(data, col);
377
- } else {
378
- return sortByDesc(data, col);
379
- }
380
- };
381
- var filterData = (data, filter, value) => {
382
- if (value == "") return data;
383
- const filteredData = data.filter((element) => `${element[filter]}`.toLowerCase().includes(value.toLowerCase()));
384
- return filteredData;
385
- };
386
-
387
- // components/export/WordExport.tsx
388
- import {
389
- AlignmentType,
390
- Document,
391
- Packer,
392
- PageOrientation,
393
- Paragraph,
394
- Table,
395
- TableCell,
396
- TableRow,
397
- TextRun,
398
- VerticalAlign,
399
- WidthType
400
- } from "docx";
401
- import { saveAs } from "file-saver";
402
-
403
- // utils/exportUtils/ExportHelpers.ts
404
- function prepareExportRows(columns, data) {
405
- return data.map(
406
- (row) => columns.map((col) => {
407
- const value = row[col.field];
408
- return typeof col.exportCustomCell !== "undefined" ? col.exportCustomCell(String(value), row) : String(value != null ? value : "");
409
- })
410
- );
411
- }
412
- function prepareExportHeaders(columns) {
413
- return columns.map((col) => col.title);
414
- }
415
-
416
- // components/export/WordExport.tsx
417
- import { jsx as jsx13 } from "react/jsx-runtime";
418
- var WordExport = ({
419
- wordData,
420
- columns,
421
- title,
422
- options = {
423
- fontSize: 20,
424
- boldHeaders: false,
425
- autoLandscape: false,
426
- maxColumnsBeforeLandscape: 5
427
- }
428
- // exportCustomColumns
429
- }) => {
430
- const createNewWord = async () => {
431
- const {
432
- fontSize = 0,
433
- boldHeaders = true,
434
- autoLandscape = true,
435
- maxColumnsBeforeLandscape = 5
436
- } = options;
437
- const isLandscape = autoLandscape && columns.length > maxColumnsBeforeLandscape;
438
- const headerCells = prepareExportHeaders(columns).map((header) => new TableCell({
439
- children: [new Paragraph({
440
- children: [new TextRun({
441
- text: header,
442
- size: fontSize,
443
- bold: boldHeaders
444
- })],
445
- alignment: AlignmentType.CENTER
446
- })],
447
- verticalAlign: VerticalAlign.CENTER
448
- }));
449
- const tableHeaderRow = new TableRow({ children: headerCells });
450
- const rows = prepareExportRows(columns, wordData).map((cells) => {
451
- const rowCells = cells.map(
452
- (value) => new TableCell({
453
- children: [new Paragraph({
454
- children: [new TextRun({
455
- text: value,
456
- size: fontSize
457
- })],
458
- alignment: AlignmentType.CENTER
459
- })],
460
- verticalAlign: VerticalAlign.CENTER
461
- })
462
- );
463
- return new TableRow({ children: rowCells });
464
- });
465
- const table = new Table({
466
- rows: [tableHeaderRow, ...rows],
467
- width: { size: 11e3, type: WidthType.DXA },
468
- indent: { size: -1e3, type: WidthType.DXA }
469
- });
470
- const doc = new Document({
471
- sections: [{
472
- children: [table, new Paragraph({ text: "" })],
473
- properties: isLandscape ? { page: { size: { orientation: PageOrientation.LANDSCAPE } } } : {}
474
- }]
475
- });
476
- Packer.toBlob(doc).then((blob) => {
477
- saveAs(blob, `${title}.docx`);
478
- });
479
- };
480
- return /* @__PURE__ */ jsx13("button", { className: `ndt-buttonExport ndt-Word}`, onClick: createNewWord, children: "\u0421\u043A\u0430\u0447\u0430\u0442\u044C Word" });
481
- };
482
- var WordExport_default = WordExport;
483
-
484
- // utils/exportUtils/exportUtils.ts
485
- var generateExcelColumns = (columns, exportCustomColumns) => {
486
- let excelColumns = columns.map((column) => ({
487
- header: column.title,
488
- key: column.field,
489
- width: 20
490
- }));
491
- if (exportCustomColumns) {
492
- exportCustomColumns.forEach((custom) => {
493
- excelColumns = excelColumns.map(
494
- (col) => col.key === custom.key ? { ...col, ...custom } : col
495
- );
496
- });
497
- }
498
- return excelColumns;
499
- };
500
- var applyHeaderStyles = (row, columnCount) => {
501
- row.height = 40;
502
- row.font = { size: 12, bold: true };
503
- row.alignment = { vertical: "middle", horizontal: "center" };
504
- for (let i = 1; i <= columnCount; i++) {
505
- const cell = row.getCell(i);
506
- cell.alignment = { wrapText: true, vertical: "middle", horizontal: "center" };
507
- cell.border = {
508
- top: { style: "thin" },
509
- left: { style: "thin" },
510
- bottom: { style: "thin" },
511
- right: { style: "thin" }
512
- };
513
- }
514
- };
515
- var applyRowStyles = (row, columnCount) => {
516
- row.height = 40;
517
- row.font = { size: 12 };
518
- row.alignment = { vertical: "middle", horizontal: "center" };
519
- for (let i = 1; i <= columnCount; i++) {
520
- const cell = row.getCell(i);
521
- cell.alignment = { wrapText: true, vertical: "middle", horizontal: "center" };
522
- cell.border = {
523
- top: { style: "thin" },
524
- left: { style: "thin" },
525
- bottom: { style: "thin" },
526
- right: { style: "thin" }
527
- };
528
- }
529
- };
530
- var generateExcelDataRows = (columns, data) => {
531
- return data.map((element) => {
532
- const rowData = {};
533
- columns.forEach((col) => {
534
- const value = element[col.field];
535
- rowData[col.field] = typeof col.exportCustomCell !== "undefined" ? col.exportCustomCell(String(value), element) : value != null ? value : "";
536
- });
537
- return rowData;
538
- });
539
- };
540
- var setColumnAutoWidths = (sheet) => {
541
- var _a;
542
- (_a = sheet.columns) == null ? void 0 : _a.forEach((column) => {
543
- var _a2;
544
- let maxLength = 10;
545
- (_a2 = column.eachCell) == null ? void 0 : _a2.call(column, { includeEmpty: true }, (cell) => {
546
- const cellValue = cell.value ? String(cell.value) : "";
547
- maxLength = Math.max(maxLength, cellValue.length + 5);
548
- });
549
- column.width = maxLength;
550
- });
551
- };
552
-
553
- // components/export/ExportExcel.tsx
554
- import ExcelJS from "exceljs";
555
- import { jsx as jsx14 } from "react/jsx-runtime";
556
- var ExportExcel = ({ columns, excelData, title, exportCustomColumns }) => {
557
- const exportExcel = () => {
558
- const workbook = new ExcelJS.Workbook();
559
- const sheet = workbook.addWorksheet(title, {
560
- pageSetup: {
561
- fitToPage: true,
562
- fitToHeight: 2,
563
- fitToWidth: 1,
564
- orientation: "landscape"
565
- },
566
- headerFooter: {
567
- oddFooter: "\u0421\u0442\u0440\u0430\u043D\u0438\u0446\u0430 &P \u0438\u0437 &N",
568
- evenFooter: "\u0421\u0442\u0440\u0430\u043D\u0438\u0446\u0430 &P \u0438\u0437 &N"
569
- }
570
- });
571
- const excelColumns = generateExcelColumns(columns, exportCustomColumns);
572
- sheet.columns = excelColumns;
573
- const headerRow = sheet.getRow(1);
574
- applyHeaderStyles(headerRow, sheet.columns.length);
575
- const dataRows = generateExcelDataRows(columns, excelData);
576
- dataRows.forEach((data) => {
577
- const row = sheet.addRow(data);
578
- applyRowStyles(row, sheet.columns.length);
579
- });
580
- setColumnAutoWidths(sheet);
581
- if (excelData.length > 15) {
582
- sheet.pageSetup.fitToHeight = Math.floor(excelData.length / 15);
583
- } else {
584
- sheet.pageSetup.fitToHeight = 1;
585
- }
586
- workbook.xlsx.writeBuffer().then((data) => {
587
- const blob = new Blob([data], {
588
- type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
589
- });
590
- const url = window.URL.createObjectURL(blob);
591
- const anchor = document.createElement("a");
592
- anchor.href = url;
593
- anchor.download = `${title}.xlsx`;
594
- anchor.click();
595
- window.URL.revokeObjectURL(url);
596
- });
597
- };
598
- return /* @__PURE__ */ jsx14("button", { className: `ndt-buttonExport ndt-Excel`, onClick: exportExcel, children: "\u0421\u043A\u0430\u0447\u0430\u0442\u044C Excel" });
599
- };
600
- var ExportExcel_default = ExportExcel;
601
-
602
- // components/ExportSection.tsx
603
- import { Fragment as Fragment2, jsx as jsx15, jsxs as jsxs5 } from "react/jsx-runtime";
604
- var ExportSection = ({ wordBtn, excelBtn, downloadSectionLeftSideContent, tableData, columns, tableName, exportCustomColumns, wordOptions }) => {
605
- return /* @__PURE__ */ jsx15(Fragment2, { children: /* @__PURE__ */ jsxs5("div", { className: "ndt-download-section", children: [
606
- /* @__PURE__ */ jsx15("div", { className: "ndt-download-content", children: (wordBtn || excelBtn) && downloadSectionLeftSideContent !== null && downloadSectionLeftSideContent }),
607
- /* @__PURE__ */ jsxs5("div", { className: "ndt-download-buttons", children: [
608
- wordBtn && /* @__PURE__ */ jsx15(
609
- WordExport_default,
610
- {
611
- wordData: tableData,
612
- columns,
613
- title: tableName,
614
- exportCustomColumns,
615
- options: wordOptions
616
- }
617
- ),
618
- excelBtn && /* @__PURE__ */ jsx15(
619
- ExportExcel_default,
620
- {
621
- excelData: tableData,
622
- columns,
623
- title: tableName,
624
- exportCustomColumns
625
- }
626
- )
627
- ] })
628
- ] }) });
629
- };
630
- var ExportSection_default = ExportSection;
631
-
632
- // utils/useDebouncedEffect.tsx
633
- import { useEffect } from "react";
634
- function useDebouncedEffect(callback, deps, delay) {
635
- useEffect(() => {
636
- const handler = setTimeout(() => callback(), delay);
637
- return () => clearTimeout(handler);
638
- }, [...deps, delay]);
639
- }
640
-
641
- // components/DataTable.tsx
642
- import { jsx as jsx16, jsxs as jsxs6 } from "react/jsx-runtime";
643
- var DataTable = forwardRef(({
644
- tableData,
645
- columns,
646
- tableName = "table-data",
647
- loading = false,
648
- loadingElement = null,
649
- isFooter = false,
650
- paginationCounts = null,
651
- scrollable = false,
652
- scrollHeight = 300,
653
- exportCustomColumns = null,
654
- excelBtn = false,
655
- wordBtn = false,
656
- downloadSectionLeftSideContent = null,
657
- headerGroup = null,
658
- groupBy = null,
659
- isTitles = false,
660
- wordOptions
661
- }, ref) => {
662
- const [filters, setFilters] = useState({});
663
- const [sortBy, setSortBy] = useState({ col: "", type: "asc" });
664
- const [paginationSize, setPaginationSize] = useState((paginationCounts == null ? void 0 : paginationCounts[0]) || 10);
665
- const [paginationPage, setPaginationPage] = useState(0);
666
- const [collapsedGroups, setCollapsedGroups] = useState({});
667
- const toggleGroup = (groupKey) => {
668
- setCollapsedGroups((prev) => ({
669
- ...prev,
670
- [groupKey]: !prev[groupKey]
671
- }));
672
- };
673
- const widths = useMemo2(() => {
674
- return columns.map((c) => c.width ? `${c.width}px` : "1fr").join(" ");
675
- }, [columns]);
676
- const loadFromLocalStorage = useCallback(() => {
677
- try {
678
- const s = localStorage.getItem(`${tableName}-sort-by`);
679
- const f = localStorage.getItem(`${tableName}-filters`);
680
- const c = localStorage.getItem(`${tableName}-counts`);
681
- const p = localStorage.getItem(`${tableName}-page`);
682
- if (s) setSortBy(JSON.parse(s));
683
- if (f) setFilters(JSON.parse(f));
684
- if (c) setPaginationSize(c === "all" ? 0 : Number(c));
685
- if (p) setPaginationPage(Number(p));
686
- } catch (e) {
687
- console.error("Error parsing localStorage data:", e);
688
- setSortBy({ col: "", type: "asc" });
689
- setFilters({});
690
- setPaginationSize((paginationCounts == null ? void 0 : paginationCounts[0]) || 10);
691
- setPaginationPage(0);
692
- }
693
- }, [tableName, paginationCounts]);
694
- useEffect2(() => {
695
- loadFromLocalStorage();
696
- }, [loadFromLocalStorage]);
697
- const processedData = useMemo2(() => {
698
- let result = [...tableData];
699
- const columnMap = new Map(columns.map((col) => [col.field, col]));
700
- for (const field in filters) {
701
- const filterValue = String(filters[field]);
702
- if (filterValue === "") continue;
703
- const column = columnMap.get(field);
704
- if (!column) continue;
705
- result = column.headerFilter ? result.filter((e) => column.headerFilter(filterValue, String(e[field]))) : filterData(result, field, filterValue);
706
- }
707
- if (sortBy.col) {
708
- result = sortData(result, sortBy.col, sortBy.type);
709
- }
710
- return result;
711
- }, [tableData, filters, sortBy, columns]);
712
- const displayData = useMemo2(() => {
713
- if (paginationSize === 0) return processedData;
714
- const start = paginationPage * paginationSize;
715
- return processedData.slice(start, start + paginationSize);
716
- }, [processedData, paginationPage, paginationSize]);
717
- useEffect2(() => {
718
- setPaginationPage(0);
719
- }, [filters, sortBy]);
720
- useDebouncedEffect(() => {
721
- localStorage.setItem(`${tableName}-filters`, JSON.stringify(filters));
722
- }, [filters, tableName], 500);
723
- useDebouncedEffect(() => {
724
- localStorage.setItem(`${tableName}-sort-by`, JSON.stringify(sortBy));
725
- }, [sortBy, tableName], 500);
726
- useEffect2(() => {
727
- localStorage.setItem(`${tableName}-counts`, paginationSize === 0 ? "all" : paginationSize.toString());
728
- }, [paginationSize, tableName]);
729
- useEffect2(() => {
730
- localStorage.setItem(`${tableName}-page`, paginationPage.toString());
731
- }, [paginationPage, tableName]);
732
- useImperativeHandle(ref, () => ({
733
- getData: () => processedData,
734
- getCurrentData: () => displayData
735
- }), [processedData, displayData]);
736
- return /* @__PURE__ */ jsxs6("div", { className: "ndt-table-container", children: [
737
- (wordBtn || excelBtn) && /* @__PURE__ */ jsx16(
738
- ExportSection_default,
739
- {
740
- wordBtn,
741
- excelBtn,
742
- downloadSectionLeftSideContent,
743
- tableData: displayData,
744
- columns,
745
- tableName,
746
- exportCustomColumns,
747
- wordOptions
748
- }
749
- ),
750
- /* @__PURE__ */ jsxs6("div", { className: "ndt-table", children: [
751
- /* @__PURE__ */ jsx16(
752
- TableHeader_default,
753
- {
754
- columns,
755
- sortBy,
756
- getSortField: setSortBy,
757
- filters,
758
- getFilters: setFilters,
759
- widths,
760
- headerGroup
761
- }
762
- ),
763
- loading ? loadingElement !== null ? loadingElement : /* @__PURE__ */ jsx16("span", { style: { marginLeft: 10, fontWeight: "bold" }, children: "\u0417\u0430\u0433\u0440\u0443\u0437\u043A\u0430 \u0434\u0430\u043D\u043D\u044B\u0445..." }) : /* @__PURE__ */ jsx16(
764
- TableBody_default,
765
- {
766
- tableData: displayData,
767
- columns,
768
- scrollable,
769
- scrollHeight,
770
- widths,
771
- groupBy,
772
- collapsedGroups,
773
- toggleGroup,
774
- isTitles
775
- }
776
- ),
777
- isFooter && /* @__PURE__ */ jsx16(
778
- TableFooter_default,
779
- {
780
- paginationCounts,
781
- tableData: processedData,
782
- paginationSize,
783
- getPaginationSize: setPaginationSize,
784
- paginationPage,
785
- getPaginationPage: setPaginationPage
786
- }
787
- )
788
- ] })
789
- ] });
790
- });
791
- DataTable.displayName = "DataTable";
792
- var DataTable_default = DataTable;
793
- export {
794
- DataTable_default as DataTable
795
- };
796
- //# sourceMappingURL=index.mjs.map