nillud-data-table 1.0.6 → 1.0.7

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.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import React$1, { ReactElement } from 'react';
2
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
3
 
3
4
  type TableElement = {
4
5
  [key: string]: string | number;
@@ -67,4 +68,21 @@ type DataTableRef = {
67
68
 
68
69
  declare const DataTable: React$1.ForwardRefExoticComponent<TableProps & React$1.RefAttributes<DataTableRef>>;
69
70
 
70
- export { type Column, DataTable, type TableElement, type TableProps };
71
+ type Props$1 = {
72
+ wordData: TableData;
73
+ columns: Array<Column>;
74
+ title: string;
75
+ options?: ExportOptions;
76
+ exportCustomColumns?: TableProps["exportCustomColumns"];
77
+ };
78
+ declare const WordExport: ({ wordData, columns, title, options }: Props$1) => react_jsx_runtime.JSX.Element;
79
+
80
+ type Props = {
81
+ columns: Array<Column>;
82
+ excelData: Array<TableElement>;
83
+ title: string;
84
+ exportCustomColumns: TableProps["exportCustomColumns"];
85
+ };
86
+ declare const ExportExcel: ({ columns, excelData, title, exportCustomColumns }: Props) => react_jsx_runtime.JSX.Element;
87
+
88
+ export { type Column, DataTable, ExportExcel, type TableElement, type TableProps, WordExport };
package/dist/index.js CHANGED
@@ -590,6 +590,222 @@ var DataTable = forwardRef(({
590
590
  });
591
591
  DataTable.displayName = "DataTable";
592
592
  var DataTable_default = DataTable;
593
+
594
+ // utils/exportUtils/ExportHelpers.ts
595
+ function prepareExportRows(columns, data) {
596
+ return data.map(
597
+ (row) => columns.map((col) => {
598
+ const value = row[col.field];
599
+ return typeof col.exportCustomCell !== "undefined" ? col.exportCustomCell(String(value), row) : String(value != null ? value : "");
600
+ })
601
+ );
602
+ }
603
+ function prepareExportHeaders(columns) {
604
+ return columns.map((col) => col.title);
605
+ }
606
+
607
+ // components/export/WordExport.tsx
608
+ import { jsx as jsx15 } from "react/jsx-runtime";
609
+ var WordExport = ({
610
+ wordData,
611
+ columns,
612
+ title,
613
+ options = {
614
+ fontSize: 20,
615
+ boldHeaders: false,
616
+ autoLandscape: false,
617
+ maxColumnsBeforeLandscape: 5
618
+ }
619
+ // exportCustomColumns
620
+ }) => {
621
+ const createNewWord = async () => {
622
+ const {
623
+ AlignmentType,
624
+ Document,
625
+ Packer,
626
+ PageOrientation,
627
+ // PageOrientation,
628
+ Paragraph,
629
+ Table,
630
+ TableCell,
631
+ TableRow,
632
+ TextRun,
633
+ VerticalAlign,
634
+ WidthType
635
+ } = await import("docx");
636
+ const { saveAs } = await import("file-saver");
637
+ const {
638
+ fontSize = 0,
639
+ boldHeaders = true,
640
+ autoLandscape = true,
641
+ maxColumnsBeforeLandscape = 5
642
+ } = options;
643
+ const isLandscape = autoLandscape && columns.length > maxColumnsBeforeLandscape;
644
+ const headerCells = prepareExportHeaders(columns).map((header) => new TableCell({
645
+ children: [new Paragraph({
646
+ children: [new TextRun({
647
+ text: header,
648
+ size: fontSize,
649
+ bold: boldHeaders
650
+ })],
651
+ alignment: AlignmentType.CENTER
652
+ })],
653
+ verticalAlign: VerticalAlign.CENTER
654
+ }));
655
+ const tableHeaderRow = new TableRow({ children: headerCells });
656
+ const rows = prepareExportRows(columns, wordData).map((cells) => {
657
+ const rowCells = cells.map(
658
+ (value) => new TableCell({
659
+ children: [new Paragraph({
660
+ children: [new TextRun({
661
+ text: value,
662
+ size: fontSize
663
+ })],
664
+ alignment: AlignmentType.CENTER
665
+ })],
666
+ verticalAlign: VerticalAlign.CENTER
667
+ })
668
+ );
669
+ return new TableRow({ children: rowCells });
670
+ });
671
+ const table = new Table({
672
+ rows: [tableHeaderRow, ...rows],
673
+ width: { size: 11e3, type: WidthType.DXA },
674
+ indent: { size: -1e3, type: WidthType.DXA }
675
+ });
676
+ const doc = new Document({
677
+ sections: [{
678
+ children: [table, new Paragraph({ text: "" })],
679
+ properties: isLandscape ? { page: { size: { orientation: PageOrientation.LANDSCAPE } } } : {}
680
+ }]
681
+ });
682
+ Packer.toBlob(doc).then((blob) => {
683
+ saveAs(blob, `${title}.docx`);
684
+ });
685
+ };
686
+ return /* @__PURE__ */ jsx15("button", { className: `ndt-buttonExport ndt-Word}`, onClick: createNewWord, children: "\u0421\u043A\u0430\u0447\u0430\u0442\u044C Word" });
687
+ };
688
+ var WordExport_default = WordExport;
689
+
690
+ // utils/exportUtils/exportUtils.ts
691
+ var generateExcelColumns = (columns, exportCustomColumns) => {
692
+ let excelColumns = columns.map((column) => ({
693
+ header: column.title,
694
+ key: column.field,
695
+ width: 20
696
+ }));
697
+ if (exportCustomColumns) {
698
+ exportCustomColumns.forEach((custom) => {
699
+ excelColumns = excelColumns.map(
700
+ (col) => col.key === custom.key ? { ...col, ...custom } : col
701
+ );
702
+ });
703
+ }
704
+ return excelColumns;
705
+ };
706
+ var applyHeaderStyles = (row, columnCount) => {
707
+ row.height = 40;
708
+ row.font = { size: 12, bold: true };
709
+ row.alignment = { vertical: "middle", horizontal: "center" };
710
+ for (let i = 1; i <= columnCount; i++) {
711
+ const cell = row.getCell(i);
712
+ cell.alignment = { wrapText: true, vertical: "middle", horizontal: "center" };
713
+ cell.border = {
714
+ top: { style: "thin" },
715
+ left: { style: "thin" },
716
+ bottom: { style: "thin" },
717
+ right: { style: "thin" }
718
+ };
719
+ }
720
+ };
721
+ var applyRowStyles = (row, columnCount) => {
722
+ row.height = 40;
723
+ row.font = { size: 12 };
724
+ row.alignment = { vertical: "middle", horizontal: "center" };
725
+ for (let i = 1; i <= columnCount; i++) {
726
+ const cell = row.getCell(i);
727
+ cell.alignment = { wrapText: true, vertical: "middle", horizontal: "center" };
728
+ cell.border = {
729
+ top: { style: "thin" },
730
+ left: { style: "thin" },
731
+ bottom: { style: "thin" },
732
+ right: { style: "thin" }
733
+ };
734
+ }
735
+ };
736
+ var generateExcelDataRows = (columns, data) => {
737
+ return data.map((element) => {
738
+ const rowData = {};
739
+ columns.forEach((col) => {
740
+ const value = element[col.field];
741
+ rowData[col.field] = typeof col.exportCustomCell !== "undefined" ? col.exportCustomCell(String(value), element) : value != null ? value : "";
742
+ });
743
+ return rowData;
744
+ });
745
+ };
746
+ var setColumnAutoWidths = (sheet) => {
747
+ var _a;
748
+ (_a = sheet.columns) == null ? void 0 : _a.forEach((column) => {
749
+ var _a2;
750
+ let maxLength = 10;
751
+ (_a2 = column.eachCell) == null ? void 0 : _a2.call(column, { includeEmpty: true }, (cell) => {
752
+ const cellValue = cell.value ? String(cell.value) : "";
753
+ maxLength = Math.max(maxLength, cellValue.length + 5);
754
+ });
755
+ column.width = maxLength;
756
+ });
757
+ };
758
+
759
+ // components/export/ExportExcel.tsx
760
+ import { jsx as jsx16 } from "react/jsx-runtime";
761
+ var ExportExcel = ({ columns, excelData, title, exportCustomColumns }) => {
762
+ const exportExcel = async () => {
763
+ const ExcelJS = await import("exceljs");
764
+ const workbook = new ExcelJS.Workbook();
765
+ const sheet = workbook.addWorksheet(title, {
766
+ pageSetup: {
767
+ fitToPage: true,
768
+ fitToHeight: 2,
769
+ fitToWidth: 1,
770
+ orientation: "landscape"
771
+ },
772
+ headerFooter: {
773
+ oddFooter: "\u0421\u0442\u0440\u0430\u043D\u0438\u0446\u0430 &P \u0438\u0437 &N",
774
+ evenFooter: "\u0421\u0442\u0440\u0430\u043D\u0438\u0446\u0430 &P \u0438\u0437 &N"
775
+ }
776
+ });
777
+ const excelColumns = generateExcelColumns(columns, exportCustomColumns);
778
+ sheet.columns = excelColumns;
779
+ const headerRow = sheet.getRow(1);
780
+ applyHeaderStyles(headerRow, sheet.columns.length);
781
+ const dataRows = generateExcelDataRows(columns, excelData);
782
+ dataRows.forEach((data) => {
783
+ const row = sheet.addRow(data);
784
+ applyRowStyles(row, sheet.columns.length);
785
+ });
786
+ setColumnAutoWidths(sheet);
787
+ if (excelData.length > 15) {
788
+ sheet.pageSetup.fitToHeight = Math.floor(excelData.length / 15);
789
+ } else {
790
+ sheet.pageSetup.fitToHeight = 1;
791
+ }
792
+ workbook.xlsx.writeBuffer().then((data) => {
793
+ const blob = new Blob([data], {
794
+ type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
795
+ });
796
+ const url = window.URL.createObjectURL(blob);
797
+ const anchor = document.createElement("a");
798
+ anchor.href = url;
799
+ anchor.download = `${title}.xlsx`;
800
+ anchor.click();
801
+ window.URL.revokeObjectURL(url);
802
+ });
803
+ };
804
+ return /* @__PURE__ */ jsx16("button", { className: `ndt-buttonExport ndt-Excel`, onClick: exportExcel, children: "\u0421\u043A\u0430\u0447\u0430\u0442\u044C Excel" });
805
+ };
806
+ var ExportExcel_default = ExportExcel;
593
807
  export {
594
- DataTable_default as DataTable
808
+ DataTable_default as DataTable,
809
+ ExportExcel_default as ExportExcel,
810
+ WordExport_default as WordExport
595
811
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nillud-data-table",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",