diff --git a/dist/ExcelPlugin/components/ExcelFile.js b/dist/ExcelPlugin/components/ExcelFile.js index ae7d84ba..d5421d5e 100644 --- a/dist/ExcelPlugin/components/ExcelFile.js +++ b/dist/ExcelPlugin/components/ExcelFile.js @@ -1,7 +1,7 @@ "use strict"; Object.defineProperty(exports, "__esModule", { - value: true + value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); @@ -35,158 +35,166 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var ExcelFile = function (_React$Component) { - _inherits(ExcelFile, _React$Component); + _inherits(ExcelFile, _React$Component); - function ExcelFile(props) { - _classCallCheck(this, ExcelFile); + function ExcelFile(props) { + _classCallCheck(this, ExcelFile); - var _this = _possibleConstructorReturn(this, (ExcelFile.__proto__ || Object.getPrototypeOf(ExcelFile)).call(this, props)); + var _this = _possibleConstructorReturn(this, (ExcelFile.__proto__ || Object.getPrototypeOf(ExcelFile)).call(this, props)); - _initialiseProps.call(_this); + _initialiseProps.call(_this); - if (_this.props.hideElement) { - _this.download(); - } else { - _this.handleDownload = _this.download.bind(_this); - } - - _this.createSheetData = _this.createSheetData.bind(_this); - return _this; + if (_this.props.hideElement) { + _this.download(); + } else { + _this.handleDownload = _this.download.bind(_this); } - _createClass(ExcelFile, [{ - key: "createSheetData", - value: function createSheetData(sheet) { - var columns = sheet.props.children; - var sheetData = [_react2.default.Children.map(columns, function (column) { - return column.props.label; - })]; - var data = typeof sheet.props.data === 'function' ? sheet.props.data() : sheet.props.data; - - data.forEach(function (row) { - var sheetRow = []; - - _react2.default.Children.forEach(columns, function (column) { - var getValue = typeof column.props.value === 'function' ? column.props.value : function (row) { - return row[column.props.value]; - }; - var itemValue = getValue(row); - sheetRow.push(isNaN(itemValue) ? itemValue || '' : itemValue); - }); - - sheetData.push(sheetRow); - }); - - return sheetData; + _this.createSheetData = _this.createSheetData.bind(_this); + return _this; + } + + _createClass(ExcelFile, [{ + key: "createSheetData", + value: function createSheetData(sheet) { + var columns = sheet.props.children; + var sheetData = [_react2.default.Children.map(columns, function (column) { + if (![false, undefined, null].includes(column)) { + return column.props.label; } - }, { - key: "download", - value: function download() { - var _this2 = this; - - var wb = { - SheetNames: _react2.default.Children.map(this.props.children, function (sheet) { - return sheet.props.name; - }), - Sheets: {} - }; + })]; + var data = typeof sheet.props.data === "function" ? sheet.props.data() : sheet.props.data; + + data.forEach(function (row) { + var sheetRow = []; - _react2.default.Children.forEach(this.props.children, function (sheet) { - if (typeof sheet.props.dataSet === 'undefined' || sheet.props.dataSet.length === 0) { - wb.Sheets[sheet.props.name] = (0, _DataUtil.excelSheetFromAoA)(_this2.createSheetData(sheet)); - } else { - wb.Sheets[sheet.props.name] = (0, _DataUtil.excelSheetFromDataSet)(sheet.props.dataSet); - } - }); + _react2.default.Children.forEach(columns, function (column) { + if (![false, undefined, null].includes(column)) { + var getValue = typeof column.props.value === "function" ? column.props.value : function (row) { + return row[column.props.value]; + }; + var itemValue = getValue(row); + sheetRow.push(isNaN(itemValue) ? itemValue || "" : itemValue); + } + }); - var fileExtension = this.getFileExtension(); - var fileName = this.getFileName(); - var wbout = _tempaXlsx2.default.write(wb, { bookType: fileExtension, bookSST: true, type: 'binary' }); + sheetData.push(sheetRow); + }); - (0, _fileSaver.saveAs)(new Blob([(0, _DataUtil.strToArrBuffer)(wbout)], { type: "application/octet-stream" }), fileName); - } - }, { - key: "getFileName", - value: function getFileName() { - if (this.props.filename === null || typeof this.props.filename !== 'string') { - throw Error('Invalid file name provided'); - } - return this.getFileNameWithExtension(this.props.filename, this.getFileExtension()); - } - }, { - key: "getFileExtension", - value: function getFileExtension() { - var extension = this.props.fileExtension; - - if (extension.length === 0) { - var slugs = this.props.filename.split('.'); - if (slugs.length === 0) { - throw Error('Invalid file name provided'); - } - extension = slugs[slugs.length - 1]; - } - - if (this.fileExtensions.indexOf(extension) !== -1) { - return extension; - } - - return this.defaultFileExtension; - } - }, { - key: "getFileNameWithExtension", - value: function getFileNameWithExtension(filename, extension) { - return filename + "." + extension; + return sheetData; + } + }, { + key: "download", + value: function download() { + var _this2 = this; + + var wb = { + SheetNames: _react2.default.Children.map(this.props.children, function (sheet) { + return sheet.props.name; + }), + Sheets: {} + }; + + _react2.default.Children.forEach(this.props.children, function (sheet) { + if (typeof sheet.props.dataSet === "undefined" || sheet.props.dataSet.length === 0) { + wb.Sheets[sheet.props.name] = (0, _DataUtil.excelSheetFromAoA)(_this2.createSheetData(sheet)); + } else { + wb.Sheets[sheet.props.name] = (0, _DataUtil.excelSheetFromDataSet)(sheet.props.dataSet); } - }, { - key: "render", - value: function render() { - var _props = this.props, - hideElement = _props.hideElement, - element = _props.element; - - - if (hideElement) { - return null; - } else { - return _react2.default.createElement( - "span", - { onClick: this.handleDownload }, - element - ); - } + }); + + var fileExtension = this.getFileExtension(); + var fileName = this.getFileName(); + var wbout = _tempaXlsx2.default.write(wb, { + bookType: fileExtension, + bookSST: true, + type: "binary" + }); + + (0, _fileSaver.saveAs)(new Blob([(0, _DataUtil.strToArrBuffer)(wbout)], { type: "application/octet-stream" }), fileName); + } + }, { + key: "getFileName", + value: function getFileName() { + if (this.props.filename === null || typeof this.props.filename !== "string") { + throw Error("Invalid file name provided"); + } + return this.getFileNameWithExtension(this.props.filename, this.getFileExtension()); + } + }, { + key: "getFileExtension", + value: function getFileExtension() { + var extension = this.props.fileExtension; + + if (extension.length === 0) { + var slugs = this.props.filename.split("."); + if (slugs.length === 0) { + throw Error("Invalid file name provided"); } - }]); + extension = slugs[slugs.length - 1]; + } + + if (this.fileExtensions.indexOf(extension) !== -1) { + return extension; + } - return ExcelFile; + return this.defaultFileExtension; + } + }, { + key: "getFileNameWithExtension", + value: function getFileNameWithExtension(filename, extension) { + return filename + "." + extension; + } + }, { + key: "render", + value: function render() { + var _props = this.props, + hideElement = _props.hideElement, + element = _props.element; + + + if (hideElement) { + return null; + } else { + return _react2.default.createElement( + "span", + { onClick: this.handleDownload }, + element + ); + } + } + }]); + + return ExcelFile; }(_react2.default.Component); ExcelFile.props = { - hideElement: _propTypes2.default.bool, - filename: _propTypes2.default.string, - fileExtension: _propTypes2.default.string, - element: _propTypes2.default.any, - children: function children(props, propName, componentName) { - _react2.default.Children.forEach(props[propName], function (child) { - if (child.type !== _ExcelSheet2.default) { - throw new Error(' can only have as children. '); - } - }); - } + hideElement: _propTypes2.default.bool, + filename: _propTypes2.default.string, + fileExtension: _propTypes2.default.string, + element: _propTypes2.default.any, + children: function children(props, propName, componentName) { + _react2.default.Children.forEach(props[propName], function (child) { + if (child.type !== _ExcelSheet2.default) { + throw new Error(" can only have as children. "); + } + }); + } }; ExcelFile.defaultProps = { - hideElement: false, - filename: "Download", - fileExtension: "xlsx", - element: _react2.default.createElement( - "button", - null, - "Download" - ) + hideElement: false, + filename: "Download", + fileExtension: "xlsx", + element: _react2.default.createElement( + "button", + null, + "Download" + ) }; var _initialiseProps = function _initialiseProps() { - this.fileExtensions = ['xlsx', 'xls', 'csv', 'txt', 'html']; - this.defaultFileExtension = 'xlsx'; + this.fileExtensions = ["xlsx", "xls", "csv", "txt", "html"]; + this.defaultFileExtension = "xlsx"; }; exports.default = ExcelFile; \ No newline at end of file diff --git a/src/ExcelPlugin/components/ExcelFile.js b/src/ExcelPlugin/components/ExcelFile.js index 753a257b..0b0083cd 100644 --- a/src/ExcelPlugin/components/ExcelFile.js +++ b/src/ExcelPlugin/components/ExcelFile.js @@ -1,128 +1,170 @@ import React from "react"; import PropTypes from "prop-types"; -import {saveAs} from "file-saver"; +import { saveAs } from "file-saver"; import XLSX from "tempa-xlsx"; import ExcelSheet from "../elements/ExcelSheet"; -import {strToArrBuffer, excelSheetFromAoA, excelSheetFromDataSet} from "../utils/DataUtil"; +import { + strToArrBuffer, + excelSheetFromAoA, + excelSheetFromDataSet, +} from "../utils/DataUtil"; class ExcelFile extends React.Component { - fileExtensions = ['xlsx', 'xls', 'csv', 'txt', 'html']; - defaultFileExtension = 'xlsx'; - - static props = { - hideElement: PropTypes.bool, - filename: PropTypes.string, - fileExtension: PropTypes.string, - element: PropTypes.any, - children: function (props, propName, componentName) { - React.Children.forEach(props[propName], child => { - if (child.type !== ExcelSheet) { - throw new Error(' can only have as children. '); - } - }); + fileExtensions = ["xlsx", "xls", "csv", "txt", "html"]; + defaultFileExtension = "xlsx"; + + static props = { + hideElement: PropTypes.bool, + filename: PropTypes.string, + fileExtension: PropTypes.string, + element: PropTypes.any, + children: function (props, propName, componentName) { + React.Children.forEach(props[propName], (child) => { + if (child.type !== ExcelSheet) { + throw new Error( + " can only have as children. ", + ); } - }; - - static defaultProps = { - hideElement: false, - filename: "Download", - fileExtension: "xlsx", - element: - }; + }); + }, + }; + + static defaultProps = { + hideElement: false, + filename: "Download", + fileExtension: "xlsx", + element: , + }; + + constructor(props) { + super(props); + + if (this.props.hideElement) { + this.download(); + } else { + this.handleDownload = this.download.bind(this); + } - constructor(props) { - super(props); + this.createSheetData = this.createSheetData.bind(this); + } - if (this.props.hideElement) { - this.download(); - } else { - this.handleDownload = this.download.bind(this); + createSheetData(sheet) { + const columns = sheet.props.children; + const sheetData = [ + React.Children.map(columns, (column) => { + if (![false, undefined, null].includes(column)) { + return column.props.label; } + }), + ]; + const data = + typeof sheet.props.data === "function" + ? sheet.props.data() + : sheet.props.data; + + data.forEach((row) => { + const sheetRow = []; + + React.Children.forEach(columns, (column) => { + if (![false, undefined, null].includes(column)) { + const getValue = + typeof column.props.value === "function" + ? column.props.value + : (row) => row[column.props.value]; + const itemValue = getValue(row); + sheetRow.push(isNaN(itemValue) ? itemValue || "" : itemValue); + } + }); - this.createSheetData = this.createSheetData.bind(this); - } - - createSheetData(sheet) { - const columns = sheet.props.children; - const sheetData = [React.Children.map(columns, column => column.props.label)]; - const data = typeof (sheet.props.data) === 'function' ? sheet.props.data() : sheet.props.data; - - data.forEach(row => { - const sheetRow = []; + sheetData.push(sheetRow); + }); - React.Children.forEach(columns, column => { - const getValue = typeof (column.props.value) === 'function' ? column.props.value : row => row[column.props.value]; - const itemValue = getValue(row); - sheetRow.push(isNaN(itemValue) ? (itemValue || '') : itemValue); - }); + return sheetData; + } - sheetData.push(sheetRow); - }); + download() { + const wb = { + SheetNames: React.Children.map( + this.props.children, + (sheet) => sheet.props.name, + ), + Sheets: {}, + }; - return sheetData; + React.Children.forEach(this.props.children, (sheet) => { + if ( + typeof sheet.props.dataSet === "undefined" || + sheet.props.dataSet.length === 0 + ) { + wb.Sheets[sheet.props.name] = excelSheetFromAoA( + this.createSheetData(sheet), + ); + } else { + wb.Sheets[sheet.props.name] = excelSheetFromDataSet( + sheet.props.dataSet, + ); + } + }); + + const fileExtension = this.getFileExtension(); + const fileName = this.getFileName(); + const wbout = XLSX.write(wb, { + bookType: fileExtension, + bookSST: true, + type: "binary", + }); + + saveAs( + new Blob([strToArrBuffer(wbout)], { type: "application/octet-stream" }), + fileName, + ); + } + + getFileName() { + if ( + this.props.filename === null || + typeof this.props.filename !== "string" + ) { + throw Error("Invalid file name provided"); } - - download() { - const wb = { - SheetNames: React.Children.map(this.props.children, sheet => sheet.props.name), - Sheets: {} - }; - - React.Children.forEach(this.props.children, sheet => { - if (typeof sheet.props.dataSet === 'undefined' || sheet.props.dataSet.length === 0) { - wb.Sheets[sheet.props.name] = excelSheetFromAoA(this.createSheetData(sheet)); - } else { - wb.Sheets[sheet.props.name] = excelSheetFromDataSet(sheet.props.dataSet); - } - }); - - const fileExtension = this.getFileExtension(); - const fileName = this.getFileName(); - const wbout = XLSX.write(wb, {bookType: fileExtension, bookSST: true, type: 'binary'}); - - saveAs(new Blob([strToArrBuffer(wbout)], {type: "application/octet-stream"}), fileName); + return this.getFileNameWithExtension( + this.props.filename, + this.getFileExtension(), + ); + } + + getFileExtension() { + let extension = this.props.fileExtension; + + if (extension.length === 0) { + const slugs = this.props.filename.split("."); + if (slugs.length === 0) { + throw Error("Invalid file name provided"); + } + extension = slugs[slugs.length - 1]; } - getFileName() { - if (this.props.filename === null || typeof this.props.filename !== 'string') { - throw Error('Invalid file name provided'); - } - return this.getFileNameWithExtension(this.props.filename, this.getFileExtension()); + if (this.fileExtensions.indexOf(extension) !== -1) { + return extension; } - getFileExtension() { - let extension = this.props.fileExtension; + return this.defaultFileExtension; + } - if (extension.length === 0) { - const slugs = this.props.filename.split('.'); - if (slugs.length === 0) { - throw Error('Invalid file name provided'); - } - extension = slugs[slugs.length - 1]; - } + getFileNameWithExtension(filename, extension) { + return `${filename}.${extension}`; + } - if (this.fileExtensions.indexOf(extension) !== -1) { - return extension; - } - - return this.defaultFileExtension; - } + render() { + const { hideElement, element } = this.props; - getFileNameWithExtension(filename, extension) { - return `${filename}.${extension}`; - } - - render() { - const { hideElement, element } = this.props; - - if (hideElement) { - return null; - } else { - return ({element}); - } - + if (hideElement) { + return null; + } else { + return {element}; } + } } export default ExcelFile;