@@ -4,25 +4,17 @@ import type { BookType, WorkSheet } from "xlsx-js-style";
44import { excelSheetFromAoA , excelSheetFromDataSet } from "../utils/DataUtil" ;
55import type {
66 ExcelColumnProps ,
7+ ExcelFileProps ,
78 ExcelSheetProps ,
89 ExcelValue ,
910} from "react-xlsx-wrapper" ;
1011
11- interface ExcelFileProps {
12- hideElement ?: boolean ;
13- filename ?: string ;
14- fileExtension ?: BookType ;
15- element ?: React . ReactNode ;
16- children : React . ReactElement < ExcelSheetProps > [ ] ;
17- }
18-
1912class ExcelFile extends React . Component < ExcelFileProps > {
2013 state = {
2114 fileName : "Download" ,
2215 fileExtension : "xlsx" as BookType ,
16+ hideElement : false ,
2317 } ;
24- static ExcelColumn : typeof import ( "/Users/susanta96/Develop/OwnProject/AS-Devs/react-xlsx-wrapper/src/ExcelPlugin/elements/ExcelColumn" ) . default ;
25- static ExcelSheet : typeof import ( "/Users/susanta96/Develop/OwnProject/AS-Devs/react-xlsx-wrapper/src/ExcelPlugin/elements/ExcelSheet" ) . default ;
2618
2719 componentDidMount ( ) {
2820 if ( this . props . filename ) {
@@ -35,9 +27,15 @@ class ExcelFile extends React.Component<ExcelFileProps> {
3527 fileExtension : this . props . fileExtension ,
3628 } ) ;
3729 }
30+
31+ if ( this . props . hideElement ) {
32+ this . setState ( {
33+ hideElement : this . props . hideElement ,
34+ } ) ;
35+ }
3836 }
3937
40- createSheetData = ( sheet : React . ReactElement < ExcelSheetProps > ) => {
38+ createSheetData = ( sheet : React . ReactElement < any > ) => {
4139 const columns = sheet . props . children ;
4240 const sheetData = [
4341 React . Children . map (
@@ -71,20 +69,23 @@ class ExcelFile extends React.Component<ExcelFileProps> {
7169 const fileName = this . getFileName ( ) ;
7270 const fileExtension : BookType = this . getFileExtension ( ) ;
7371
74- React . Children . forEach ( this . props . children , ( sheet ) => {
75- let ws : WorkSheet = { } ;
76- const wsName = sheet . props . name || fileName . split ( "." ) [ 0 ] || "Sheet1" ;
77- if (
78- typeof sheet . props . dataSet === "undefined" ||
79- sheet . props . dataSet . length === 0
80- ) {
81- ws = excelSheetFromAoA ( this . createSheetData ( sheet ) ) ;
82- } else {
83- ws = excelSheetFromDataSet ( sheet . props . dataSet ) ;
72+ React . Children . forEach < React . ReactElement < ExcelSheetProps > > (
73+ this . props . children ,
74+ ( sheet ) => {
75+ let ws : WorkSheet = { } ;
76+ const wsName = sheet . props . name || fileName . split ( "." ) [ 0 ] || "Sheet1" ;
77+ if (
78+ typeof sheet . props . dataSet === "undefined" ||
79+ sheet . props . dataSet . length === 0
80+ ) {
81+ ws = excelSheetFromAoA ( this . createSheetData ( sheet ) ) ;
82+ } else {
83+ ws = excelSheetFromDataSet ( sheet . props . dataSet ) ;
84+ }
85+ // add worksheet to workbook
86+ utils . book_append_sheet ( wb , ws , wsName ) ;
8487 }
85- // add worksheet to workbook
86- utils . book_append_sheet ( wb , ws , wsName ) ;
87- } ) ;
88+ ) ;
8889
8990 writeFile ( wb , fileName , {
9091 bookType : fileExtension ,
@@ -141,9 +142,9 @@ class ExcelFile extends React.Component<ExcelFileProps> {
141142 } ;
142143
143144 render ( ) {
144- const { hideElement , element } = this . props ;
145+ const { element } = this . props ;
145146
146- if ( hideElement ) {
147+ if ( this . state . hideElement === true ) {
147148 return null ;
148149 } else {
149150 return < span onClick = { this . handleDownload } > { element } </ span > ;
0 commit comments