Skip to content

Commit

Permalink
IOS bugfix to open excel files
Browse files Browse the repository at this point in the history
Set the right MIME type for xlsx to allow detection in IOS systems.
File saver library update to the last one.
  • Loading branch information
rdcalle committed Jul 10, 2018
1 parent 9a630c4 commit e81496d
Show file tree
Hide file tree
Showing 4 changed files with 2,031 additions and 635 deletions.
278 changes: 142 additions & 136 deletions dist/ExcelPlugin/components/ExcelFile.js
Original file line number Diff line number Diff line change
@@ -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; }; }();
Expand Down Expand Up @@ -35,158 +35,164 @@ 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;
}
}, {
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);
}
});

var fileExtension = this.getFileExtension();
var fileName = this.getFileName();
var wbout = _xlsx2.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 this.defaultFileExtension;
}
}, {
key: "getFileNameWithExtension",
value: function getFileNameWithExtension(filename, extension) {
return filename + "." + extension;
_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) {
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;
}
}, {
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 = _xlsx2.default.write(wb, {
bookType: fileExtension,
bookSST: true,
type: "binary"
});

(0, _fileSaver.saveAs)(new Blob([(0, _DataUtil.strToArrBuffer)(wbout)], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
}), 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;
}
}, {
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;
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('<ExcelFile> can only have <ExcelSheet> 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("<ExcelFile> can only have <ExcelSheet> 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;
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-data-export",
"version": "0.4.2",
"version": "0.5.0",
"main": "dist/index.js",
"description": "A set of tools to export dataset from react to different formats.",
"repository": {
Expand All @@ -20,42 +20,41 @@
},
"homepage": "https://github.com/securedeveloper/react-data-export#readme",
"dependencies": {
"file-saver": "1.3.3",
"file-saver": "1.3.8",
"xlsx": "git+https://github.com/securedeveloper/js-xlsx.git"
},
"devDependencies": {
"@commitlint/cli": "6.1.3",
"@commitlint/config-conventional": "6.1.3",
"babel-cli": "6.26.0",
"babel-core": "6.26.0",
"babel-jest": "22.4.1",
"babel-eslint": "8.0.1",
"babel-jest": "22.4.1",
"babel-plugin-transform-class-properties": "6.24.1",
"babel-preset-es2015": "6.24.1",
"babel-preset-react": "6.24.1",
"babel-register": "6.26.0",
"cross-env": "5.1.0",
"husky": "0.15.0-rc.13",
"enzyme": "3.3.0",
"enzyme-adapter-react-16": "1.1.1",
"eslint": "4.8.0",
"eslint-plugin-import": "2.7.0",
"eslint-plugin-jsx-a11y": "6.0.2",
"eslint-plugin-jest": "21.14.0",
"eslint-plugin-jsx-a11y": "6.0.2",
"eslint-plugin-react": "7.4.0",
"eslint-watch": "3.1.3",
"nyc": "11.2.1",
"husky": "0.15.0-rc.13",
"jest": "22.4.2",
"jest-environment-jsdom": "22.4.1",
"jest-environment-jsdom-global": "1.0.3",
"jest-environment-node": "22.4.1",
"nyc": "11.2.1",
"prop-types": "15.6.0",
"react": "16.0.0",
"react-dom": "16.0.0",
"standard-version": "4.2.0"
},
"scripts": {
"commitmsg": "commitlint -e $GIT_PARAMS",
"release": "standard-version",
"test": "npm run lint && jest .",
"test:coverage": "nyc npm t",
Expand Down
Loading

0 comments on commit e81496d

Please sign in to comment.