-
Notifications
You must be signed in to change notification settings - Fork 209
[bugfix] conditionally hide a ExcelColumn from sheetData #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
coolbeatz71
wants to merge
1
commit into
securedeveloper:master
Choose a base branch
from
coolbeatz71:bug-conditional-column
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; }; }(); | ||
|
@@ -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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this ignores falsy column from the sheetRow array |
||
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('<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; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this ignores falsy column from the sheetData array