1
+ "use strict" ;
2
+
3
+ Object . defineProperty ( exports , "__esModule" , {
4
+ value : true
5
+ } ) ;
6
+
7
+ var _createClass = function ( ) {
8
+ function defineProperties ( target , props ) {
9
+ for ( var i = 0 ; i < props . length ; i ++ ) {
10
+ var descriptor = props [ i ] ;
11
+ descriptor . enumerable = descriptor . enumerable || false ;
12
+ descriptor . configurable = true ;
13
+ if ( "value" in descriptor ) descriptor . writable = true ;
14
+ Object . defineProperty ( target , descriptor . key , descriptor ) ;
15
+ }
16
+ }
17
+
18
+ return function ( Constructor , protoProps , staticProps ) {
19
+ if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ;
20
+ if ( staticProps ) defineProperties ( Constructor , staticProps ) ;
21
+ return Constructor ;
22
+ } ;
23
+ } ( ) ;
24
+
25
+ var _react = require ( "react" ) ;
26
+
27
+ var _react2 = _interopRequireDefault ( _react ) ;
28
+
29
+ var _propTypes = require ( "prop-types" ) ;
30
+
31
+ var _propTypes2 = _interopRequireDefault ( _propTypes ) ;
32
+
33
+ var _fileSaver = require ( "file-saver" ) ;
34
+
35
+ var _xlsx = require ( "xlsx" ) ;
36
+
37
+ var _xlsx2 = _interopRequireDefault ( _xlsx ) ;
38
+
39
+ var _ExcelSheet = require ( "../elements/ExcelSheet" ) ;
40
+
41
+ var _ExcelSheet2 = _interopRequireDefault ( _ExcelSheet ) ;
42
+
43
+ var _DataUtil = require ( "../utils/DataUtil" ) ;
44
+
45
+ function _interopRequireDefault ( obj ) {
46
+ return obj && obj . __esModule ? obj : { default : obj } ;
47
+ }
48
+
49
+ function _classCallCheck ( instance , Constructor ) {
50
+ if ( ! ( instance instanceof Constructor ) ) {
51
+ throw new TypeError ( "Cannot call a class as a function" ) ;
52
+ }
53
+ }
54
+
55
+ function _possibleConstructorReturn ( self , call ) {
56
+ if ( ! self ) {
57
+ throw new ReferenceError ( "this hasn't been initialised - super() hasn't been called" ) ;
58
+ }
59
+ return call && ( typeof call === "object" || typeof call === "function" ) ? call : self ;
60
+ }
61
+
62
+ function _inherits ( subClass , superClass ) {
63
+ if ( typeof superClass !== "function" && superClass !== null ) {
64
+ throw new TypeError ( "Super expression must either be null or a function, not " + typeof superClass ) ;
65
+ }
66
+ subClass . prototype = Object . create ( superClass && superClass . prototype , {
67
+ constructor : {
68
+ value : subClass ,
69
+ enumerable : false ,
70
+ writable : true ,
71
+ configurable : true
72
+ }
73
+ } ) ;
74
+ if ( superClass ) Object . setPrototypeOf ? Object . setPrototypeOf ( subClass , superClass ) : subClass . __proto__ = superClass ;
75
+ }
76
+
77
+ var ExcelFile = function ( _React$Component ) {
78
+ _inherits ( ExcelFile , _React$Component ) ;
79
+
80
+ function ExcelFile ( props ) {
81
+ _classCallCheck ( this , ExcelFile ) ;
82
+
83
+ var _this = _possibleConstructorReturn ( this , ( ExcelFile . __proto__ || Object . getPrototypeOf ( ExcelFile ) ) . call ( this , props ) ) ;
84
+
85
+ _this . handleDownload = _this . download . bind ( _this ) ;
86
+ _this . createSheetData = _this . createSheetData . bind ( _this ) ;
87
+ return _this ;
88
+ }
89
+
90
+ _createClass ( ExcelFile , [ {
91
+ key : "createSheetData" ,
92
+ value : function createSheetData ( sheet ) {
93
+ var columns = sheet . props . children ;
94
+ var sheetData = [ _react2 . default . Children . map ( columns , function ( column ) {
95
+ return column . props . label ;
96
+ } ) ] ;
97
+ var data = typeof sheet . props . data === 'function' ? sheet . props . data ( ) : sheet . props . data ;
98
+
99
+ data . forEach ( function ( row ) {
100
+ var sheetRow = [ ] ;
101
+
102
+ _react2 . default . Children . forEach ( columns , function ( column ) {
103
+ var getValue = typeof column . props . value === 'function' ? column . props . value : function ( row ) {
104
+ return row [ column . props . value ] ;
105
+ } ;
106
+ sheetRow . push ( getValue ( row ) || '' ) ;
107
+ } ) ;
108
+
109
+ sheetData . push ( sheetRow ) ;
110
+ } ) ;
111
+
112
+ return sheetData ;
113
+ }
114
+ } , {
115
+ key : "download" ,
116
+ value : function download ( ) {
117
+ var _this2 = this ;
118
+
119
+ var wb = {
120
+ SheetNames : _react2 . default . Children . map ( this . props . children , function ( sheet ) {
121
+ return sheet . props . name ;
122
+ } ) ,
123
+ Sheets : { }
124
+ } ;
125
+
126
+ _react2 . default . Children . forEach ( this . props . children , function ( sheet ) {
127
+ wb . Sheets [ sheet . props . name ] = ( 0 , _DataUtil . excelSheetFromAoA ) ( _this2 . createSheetData ( sheet ) ) ;
128
+ } ) ;
129
+
130
+ var wbout = _xlsx2 . default . write ( wb , { bookType : 'xlsx' , bookSST : true , type : 'binary' } ) ;
131
+ ( 0 , _fileSaver . saveAs ) ( new Blob ( [ ( 0 , _DataUtil . strToArrBuffer ) ( wbout ) ] , { type : "application/octet-stream" } ) , this . props . filename ) ;
132
+ }
133
+ } , {
134
+ key : "render" ,
135
+ value : function render ( ) {
136
+ return _react2 . default . createElement (
137
+ "span" ,
138
+ { onClick : this . handleDownload } ,
139
+ this . props . element
140
+ ) ;
141
+ }
142
+ } ] ) ;
143
+
144
+ return ExcelFile ;
145
+ } ( _react2 . default . Component ) ;
146
+
147
+ ExcelFile . props = {
148
+ filename : _propTypes2 . default . string ,
149
+ element : _propTypes2 . default . any ,
150
+ children : function children ( props , propName , componentName ) {
151
+ _react2 . default . Children . forEach ( props [ propName ] , function ( child ) {
152
+ if ( child . type !== _ExcelSheet2 . default ) {
153
+ throw new Error ( '<ExcelFile> can only have <ExcelSheet> as children. ' ) ;
154
+ }
155
+ } ) ;
156
+ }
157
+ } ;
158
+ ExcelFile . defaultProps = {
159
+ filename : "Download.xlsx" ,
160
+ element : _react2 . default . createElement (
161
+ "button" ,
162
+ null ,
163
+ "Download"
164
+ )
165
+ } ;
166
+ exports . default = ExcelFile ;
0 commit comments