react-spreadsheet
Version:
Simple, customizable yet performant spreadsheet for React
139 lines (123 loc) • 4.29 kB
JavaScript
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import React, { Component } from "react";
import classnames from "classnames";
import { connect } from "unistore/react";
import * as Matrix from "./matrix";
import * as Actions from "./actions";
import * as Types from "./types";
import { getCellDimensions } from "./util";
var ActiveCell = /*#__PURE__*/function (_Component) {
_inherits(ActiveCell, _Component);
var _super = _createSuper(ActiveCell);
function ActiveCell() {
var _this;
_classCallCheck(this, ActiveCell);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
_this.state = {
cellBeforeUpdate: null
};
_this.handleChange = function (row, column, cell) {
var _this$props = _this.props,
setCellData = _this$props.setCellData,
getBindingsForCell = _this$props.getBindingsForCell;
var bindings = getBindingsForCell(cell);
setCellData({
row: row,
column: column
}, cell, bindings);
};
return _this;
}
_createClass(ActiveCell, [{
key: "componentDidUpdate",
// NOTE: Currently all logics here belongs to commit event
value: function componentDidUpdate(prevProps) {
var _this$props2 = this.props,
cell = _this$props2.cell,
mode = _this$props2.mode,
commit = _this$props2.commit;
if (cell || cell === undefined) {
if (prevProps.mode === "view" && mode === "edit") {
this.setState({
cellBeforeUpdate: prevProps.cell
});
} else if (prevProps.mode === "edit" && prevProps.mode !== this.props.mode && prevProps.cell && prevProps.cell !== this.state.cellBeforeUpdate) {
commit([{
prevCell: this.state.cellBeforeUpdate,
nextCell: prevProps.cell
}]);
}
}
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var DataEditor = this.props.DataEditor;
var _this$props3 = this.props,
getValue = _this$props3.getValue,
row = _this$props3.row,
column = _this$props3.column,
cell = _this$props3.cell,
width = _this$props3.width,
height = _this$props3.height,
top = _this$props3.top,
left = _this$props3.left,
hidden = _this$props3.hidden,
mode = _this$props3.mode,
edit = _this$props3.edit;
DataEditor = cell && cell.DataEditor || DataEditor;
var readOnly = cell && cell.readOnly;
return hidden ? null : /*#__PURE__*/React.createElement("div", {
className: classnames("Spreadsheet__active-cell", "Spreadsheet__active-cell--".concat(mode)),
style: {
width: width,
height: height,
top: top,
left: left
},
onClick: mode === "view" && !readOnly ? edit : undefined
}, mode === "edit" && /*#__PURE__*/React.createElement(DataEditor, {
row: row,
column: column,
cell: cell,
onChange: function onChange(cell) {
return _this2.handleChange(row, column, cell);
},
getValue: getValue
}));
}
}]);
return ActiveCell;
}(Component);
var mapStateToProps = function mapStateToProps(state) {
var dimensions = state.active && getCellDimensions(state.active, state);
if (!state.active || !dimensions) {
return {
hidden: true
};
}
return _objectSpread({
hidden: false
}, state.active, {
// $FlowFixMe
cell: Matrix.get(state.active.row, state.active.column, state.data),
width: dimensions.width,
height: dimensions.height,
top: dimensions.top,
left: dimensions.left,
mode: state.mode
});
};
export default connect(mapStateToProps, {
setCellData: Actions.setCellData,
edit: Actions.edit,
commit: Actions.commit
})(ActiveCell);