nillud-data-table 1.4.0 → 1.4.1

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
@@ -1,4 +1,4 @@
1
- # [React](https://react.dev/) nillud-data-table Component [![npm version](https://img.shields.io/npm/v/data-table-lib)](https://www.npmjs.com/package/data-table-lib)
1
+ # [React](https://react.dev/) DataTable Component [![npm version](https://img.shields.io/npm/v/data-table-lib)](https://www.npmjs.com/package/data-table-lib)
2
2
 
3
3
  ## Возможности
4
4
 
@@ -1,35 +1,49 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import { ReactElement } from 'react';
3
+ import { CellValue } from 'exceljs';
3
4
 
4
5
  type TableElement = {
5
6
  [key: string]: string | number;
6
7
  };
7
- type TableData = Array<TableElement>;
8
8
  type Column = {
9
9
  field: string;
10
10
  title: string;
11
11
  width?: number;
12
12
  autoinc?: boolean;
13
- formatter?: (cell: string, row: TableElement) => ReactElement;
14
- exportCustomCell?: (cell: string, row: TableElement) => string;
15
- headerFilter?: (headerValue: string, rowValue: string) => string;
13
+ formatter?: (cell: string, row: TableElement, column: Column) => ReactElement;
14
+ headerFormatter?: (column: string) => ReactElement;
15
+ exportCustomCell?: (title: string, row: TableElement) => string;
16
+ cellAlignment?: CellAlignment;
17
+ headerFilter?: (headerValue: string, rowValue: string) => boolean;
16
18
  sortable?: boolean;
17
19
  filterable?: boolean;
20
+ selectable?: boolean;
21
+ isSelectableCell?: boolean;
22
+ filterPlaceholder?: string;
23
+ editable?: boolean;
18
24
  };
19
25
  type CustomColumn = {
20
- header: string;
26
+ header?: string;
21
27
  key: string;
22
28
  width?: number;
29
+ exportCustomCell?: (value: string, row: TableElement) => string | CellValue;
23
30
  [key: string]: any;
24
31
  };
25
- type CustomColumns = Array<CustomColumn> | null;
32
+ type CellAlignment = {
33
+ vertical: 'top' | 'middle' | 'bottom';
34
+ horizontal: 'left' | 'center' | 'right';
35
+ };
26
36
  type ExcelExportTypes = {
27
37
  columns: Array<Column>;
28
- excelData: TableData;
29
- title: string;
30
- exportCustomColumns?: CustomColumns;
38
+ excelData?: Array<TableElement>;
39
+ tableRef?: any;
40
+ titleFile: string;
41
+ title?: string;
42
+ text?: string;
43
+ exportCustomColumns?: Array<CustomColumn>;
44
+ customHeight?: number;
31
45
  };
32
46
 
33
- declare const ExportExcel: ({ columns, excelData, title, exportCustomColumns }: ExcelExportTypes) => react_jsx_runtime.JSX.Element;
47
+ declare const ExportExcel: ({ columns, excelData, tableRef, titleFile, title, text, exportCustomColumns, customHeight }: ExcelExportTypes) => react_jsx_runtime.JSX.Element;
34
48
 
35
49
  export { type ExcelExportTypes, ExportExcel };
@@ -1,7 +1,7 @@
1
1
  // components/export/export-excel/exportUtils.ts
2
2
  var generateExcelColumns = (columns, exportCustomColumns) => {
3
- let excelColumns = columns.map((column) => ({
4
- header: column.title,
3
+ let excelColumns = columns.filter((column) => column.title !== "").map((column) => ({
4
+ title: column.title,
5
5
  key: column.field,
6
6
  width: 20
7
7
  }));
@@ -29,13 +29,18 @@ var applyHeaderStyles = (row, columnCount) => {
29
29
  };
30
30
  }
31
31
  };
32
- var applyRowStyles = (row, columnCount) => {
33
- row.height = 40;
32
+ var applyRowStyles = (row, columnCount, height = 40, columns) => {
33
+ row.height = height;
34
34
  row.font = { size: 12 };
35
35
  row.alignment = { vertical: "middle", horizontal: "center" };
36
36
  for (let i = 1; i <= columnCount; i++) {
37
37
  const cell = row.getCell(i);
38
- cell.alignment = { wrapText: true, vertical: "middle", horizontal: "center" };
38
+ const column = columns[i - 1];
39
+ cell.alignment = {
40
+ wrapText: true,
41
+ vertical: column.cellAlignment ? column.cellAlignment.vertical : "middle",
42
+ horizontal: column.cellAlignment ? column.cellAlignment.horizontal : "center"
43
+ };
39
44
  cell.border = {
40
45
  top: { style: "thin" },
41
46
  left: { style: "thin" },
@@ -44,14 +49,21 @@ var applyRowStyles = (row, columnCount) => {
44
49
  };
45
50
  }
46
51
  };
47
- var generateExcelDataRows = (columns, data) => {
48
- return data.map((element) => {
49
- const rowData = {};
50
- columns.forEach((col) => {
52
+ var generateExcelDataRows = (columns, data, exportCustomColumns) => {
53
+ return data.map((element, rowIndex) => {
54
+ return columns.map((col, colIndex) => {
55
+ const isAutoinc = col.autoinc;
56
+ if (isAutoinc) return rowIndex + 1;
51
57
  const value = element[col.field];
52
- rowData[col.field] = typeof col.exportCustomCell !== "undefined" ? col.exportCustomCell(String(value), element) : value != null ? value : "";
58
+ const customCol = exportCustomColumns == null ? void 0 : exportCustomColumns.find((custom) => custom.key === col.field);
59
+ if (customCol == null ? void 0 : customCol.exportCustomCell) {
60
+ return customCol.exportCustomCell(String(value != null ? value : ""), element);
61
+ }
62
+ if (typeof col.exportCustomCell !== "undefined") {
63
+ return col.exportCustomCell(String(value != null ? value : ""), element);
64
+ }
65
+ return value != null ? value : "";
53
66
  });
54
- return rowData;
55
67
  });
56
68
  };
57
69
  var setColumnAutoWidths = (sheet) => {
@@ -70,10 +82,11 @@ var setColumnAutoWidths = (sheet) => {
70
82
  // components/export/export-excel/ExportExcel.tsx
71
83
  import ExcelJS from "exceljs";
72
84
  import { jsx } from "react/jsx-runtime";
73
- var ExportExcel = ({ columns, excelData, title, exportCustomColumns }) => {
85
+ var ExportExcel = ({ columns, excelData, tableRef, titleFile, title, text, exportCustomColumns, customHeight }) => {
74
86
  const exportExcel = async () => {
87
+ const tableData = excelData || tableRef.current.getData();
75
88
  const workbook = new ExcelJS.Workbook();
76
- const sheet = workbook.addWorksheet(title, {
89
+ const sheet = workbook.addWorksheet(titleFile, {
77
90
  pageSetup: {
78
91
  fitToPage: true,
79
92
  fitToHeight: 2,
@@ -85,18 +98,41 @@ var ExportExcel = ({ columns, excelData, title, exportCustomColumns }) => {
85
98
  evenFooter: "\u0421\u0442\u0440\u0430\u043D\u0438\u0446\u0430 &P \u0438\u0437 &N"
86
99
  }
87
100
  });
101
+ sheet.properties.defaultRowHeight = 20;
102
+ let tableHeaderRow = 1;
103
+ if (title) {
104
+ sheet.getCell("A1").value = title;
105
+ sheet.getRow(1).height = 25;
106
+ sheet.getRow(1).alignment = { vertical: "middle" };
107
+ sheet.getCell("A1").font = {
108
+ size: 18,
109
+ bold: true
110
+ };
111
+ tableHeaderRow++;
112
+ }
113
+ if (text) {
114
+ sheet.getCell("A2").value = text;
115
+ sheet.getRow(2).height = 15;
116
+ sheet.getRow(2).alignment = { vertical: "middle" };
117
+ sheet.getCell("A2").font = {
118
+ size: 12
119
+ };
120
+ tableHeaderRow++;
121
+ }
122
+ if (title || text) tableHeaderRow++;
88
123
  const excelColumns = generateExcelColumns(columns, exportCustomColumns);
89
124
  sheet.columns = excelColumns;
90
- const headerRow = sheet.getRow(1);
125
+ const headerRow = sheet.getRow(tableHeaderRow);
126
+ headerRow.values = excelColumns.map((col) => col.title);
91
127
  applyHeaderStyles(headerRow, sheet.columns.length);
92
- const dataRows = generateExcelDataRows(columns, excelData);
128
+ const dataRows = generateExcelDataRows(columns, tableData);
93
129
  dataRows.forEach((data) => {
94
130
  const row = sheet.addRow(data);
95
- applyRowStyles(row, sheet.columns.length);
131
+ applyRowStyles(row, sheet.columns.length, customHeight, columns);
96
132
  });
97
133
  setColumnAutoWidths(sheet);
98
- if (excelData.length > 15) {
99
- sheet.pageSetup.fitToHeight = Math.floor(excelData.length / 15);
134
+ if (tableData.length > 15) {
135
+ sheet.pageSetup.fitToHeight = Math.floor(tableData.length / 15);
100
136
  } else {
101
137
  sheet.pageSetup.fitToHeight = 1;
102
138
  }
@@ -107,7 +143,7 @@ var ExportExcel = ({ columns, excelData, title, exportCustomColumns }) => {
107
143
  const url = window.URL.createObjectURL(blob);
108
144
  const anchor = document.createElement("a");
109
145
  anchor.href = url;
110
- anchor.download = `${title}.xlsx`;
146
+ anchor.download = `${titleFile}.xlsx`;
111
147
  anchor.click();
112
148
  window.URL.revokeObjectURL(url);
113
149
  });
package/dist/index.d.ts CHANGED
@@ -4,6 +4,10 @@ type TableElement = {
4
4
  [key: string]: string | number;
5
5
  };
6
6
  type TableData = Array<TableElement>;
7
+ type CellAlignment = {
8
+ vertical: 'top' | 'middle' | 'bottom';
9
+ horizontal: 'left' | 'center' | 'right';
10
+ };
7
11
  type Column = {
8
12
  field: string;
9
13
  title: string;
@@ -12,6 +16,7 @@ type Column = {
12
16
  formatter?: (cell: string, row: TableElement, column: Column) => ReactElement;
13
17
  headerFormatter?: (column: string) => ReactElement;
14
18
  exportCustomCell?: (title: string, row: TableElement) => string;
19
+ cellAlignment?: CellAlignment;
15
20
  headerFilter?: (headerValue: string, rowValue: string) => boolean;
16
21
  sortable?: boolean;
17
22
  filterable?: boolean;
package/dist/index.js CHANGED
@@ -573,12 +573,8 @@ var DataTable = forwardRef(({
573
573
  try {
574
574
  const s = localStorage.getItem(`${tableName}-sort-by`);
575
575
  const f = localStorage.getItem(`${tableName}-filters`);
576
- const c = localStorage.getItem(`${tableName}-counts`);
577
- const p = localStorage.getItem(`${tableName}-page`);
578
576
  if (s) setSortBy(JSON.parse(s));
579
577
  if (f) setFilters(JSON.parse(f));
580
- if (c) setPaginationSize(c === "all" ? 0 : Number(c));
581
- if (p) setPaginationPage(Number(p));
582
578
  } catch (e) {
583
579
  console.error("Error parsing localStorage data:", e);
584
580
  setSortBy({ col: "", type: "asc" });
@@ -627,12 +623,6 @@ var DataTable = forwardRef(({
627
623
  useDebouncedEffect(() => {
628
624
  localStorage.setItem(`${tableName}-sort-by`, JSON.stringify(sortBy));
629
625
  }, [sortBy, tableName], 500);
630
- useEffect2(() => {
631
- localStorage.setItem(`${tableName}-counts`, paginationSize === 0 ? "all" : paginationSize.toString());
632
- }, [paginationSize, tableName]);
633
- useEffect2(() => {
634
- localStorage.setItem(`${tableName}-page`, paginationPage.toString());
635
- }, [paginationPage, tableName]);
636
626
  useImperativeHandle(ref, () => ({
637
627
  getData: () => processedData,
638
628
  getCurrentData: () => displayData,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nillud-data-table",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",