@integec/grid-tools
Version:
Integ Grid Tools
373 lines (283 loc) • 15.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _isNil2 = _interopRequireDefault(require("ramda/src/isNil"));
var _react = _interopRequireDefault(require("react"));
var _utils = require("./utils");
var _constants = require("./constants.js");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { if (i % 2) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } else { Object.defineProperties(target, Object.getOwnPropertyDescriptors(arguments[i])); } } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/* eslint-enable no-unused-vars */
// import PropTypes from 'prop-types'
// TODO: determine prop types later
// eslint-disable-next-line standard/object-curly-even-spacing
var getInitialState = function getInitialState(_ref) {
var rowIndex = _ref.rowIndex,
header = _ref.header,
data = _ref.data;
var value = (0, _utils.extractData)({
header: header,
rowData: data[rowIndex]
}) || '';
var display = (0, _utils.formatData)({
header: header,
value: value,
rowData: data[rowIndex]
});
return {
value: value,
display: display
};
};
var CellEditContainer =
/*#__PURE__*/
function (_React$Component) {
_inherits(CellEditContainer, _React$Component);
function CellEditContainer() {
var _getPrototypeOf2;
var _this;
_classCallCheck(this, CellEditContainer);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(CellEditContainer)).call.apply(_getPrototypeOf2, [this].concat(args)));
_defineProperty(_assertThisInitialized(_this), "state", getInitialState(_this.props));
_defineProperty(_assertThisInitialized(_this), "valueChanged", function (value) {
// console.log('value changed to', value)
// TODO: prevent string and number
var _this$props = _this.props,
header = _this$props.header,
data = _this$props.data,
rowIndex = _this$props.rowIndex;
var display = (0, _utils.formatData)({
header: header,
value: value,
rowData: data[rowIndex]
});
_this.setState({
value: value,
display: display
});
});
_defineProperty(_assertThisInitialized(_this), "inputValueChanged", function (e) {
_this.valueChanged(e.target.value);
});
_defineProperty(_assertThisInitialized(_this), "refHandler", function (n) {
return _this.node = n;
});
_defineProperty(_assertThisInitialized(_this), "commitEdit", function (committedValue) {
/**
* TODO the following could be dynamic for example 1 value changing 2
* may need special logic or tricks from header to describe this logic
**/
var _this$props2 = _this.props,
rowIndex = _this$props2.rowIndex,
header = _this$props2.header,
data = _this$props2.data,
commitRowEdit = _this$props2.commitRowEdit;
var value = (0, _isNil2["default"])(committedValue) ? _this.state.value : committedValue;
var rowData = data[rowIndex]; // if (header.setInvalidMessage && header.setInvalidMessage({ header, rowData, value, rowIndex, data })) {
// this.cancelEdit()
// return
// }
if (commitRowEdit) {
commitRowEdit({
currentRow: rowData,
editedRow: _objectSpread({}, rowData, _defineProperty({}, header.ident, value)),
rowIndex: rowIndex,
header: header,
data: data
});
} else {
console.warn('commitRowEdit must be set on CellEditContainer for edit to have effect');
}
});
_defineProperty(_assertThisInitialized(_this), "cancelEdit", function (_) {
var _this$props3 = _this.props,
rowIndex = _this$props3.rowIndex,
header = _this$props3.header,
data = _this$props3.data,
cancelEdit = _this$props3.cancelEdit;
if (cancelEdit) {
cancelEdit({
rowIndex: rowIndex,
header: header,
data: data
});
}
});
_defineProperty(_assertThisInitialized(_this), "leaveEditState", function () {
if (_this.props.value !== _this.state.value) {
_this.commitEdit();
return;
}
_this.cancelEdit();
});
_defineProperty(_assertThisInitialized(_this), "selectLeftCell", function () {
var selectLeft = _this.props.selectLeft;
_this.leaveEditState();
selectLeft && selectLeft();
});
_defineProperty(_assertThisInitialized(_this), "selectRightCell", function () {
var selectRight = _this.props.selectRight;
_this.leaveEditState();
selectRight && selectRight();
});
_defineProperty(_assertThisInitialized(_this), "selectTopCell", function () {
var selectTop = _this.props.selectTop;
_this.leaveEditState();
selectTop && selectTop();
});
_defineProperty(_assertThisInitialized(_this), "selectBottomCell", function () {
var selectBottom = _this.props.selectBottom;
_this.leaveEditState();
selectBottom && selectBottom();
});
_defineProperty(_assertThisInitialized(_this), "inputKeyDown", function (e) {
// console.log('key typed in input', e.keyCode)
// enter
if (e.keyCode === 13) {
_this.commitEdit();
} // escape
if (e.keyCode === 27) {
_this.cancelEdit();
} // right arrow
if (e.keyCode === 39 && _this.node && typeof _this.node.selectionStart === 'number' && _this.node.selectionStart === _this.node.value.length && _this.node.selectionStart === _this.node.selectionEnd) {
_this.selectRightCell();
} // tab
if (e.keyCode === 9) {
e.preventDefault();
_this.selectRightCell();
} // left arrow
if (e.keyCode === 37 && _this.node && typeof _this.node.selectionStart === 'number' && _this.node.selectionStart === 0 && _this.node.selectionStart === _this.node.selectionEnd) {
_this.selectLeftCell();
} // up arrow
if (e.keyCode === 38) _this.selectTopCell(); // down arrow
if (e.keyCode === 40) _this.selectBottomCell();
});
_defineProperty(_assertThisInitialized(_this), "dropdownKeyDown", function (e) {
// console.log('key typed in dropdown selector', e.keyCode)
// escape
if (e.keyCode === 27) _this.cancelEdit(); // right arrow
if (e.keyCode === 39) _this.selectRightCell(); // tab
if (e.keyCode === 9) {
e.preventDefault();
_this.selectRightCell();
} // left arrow
if (e.keyCode === 37) _this.selectLeftCell();
});
_defineProperty(_assertThisInitialized(_this), "dropdownInputKeyDown", function (e) {
// console.log('key typed in dropdown input', e.keyCode)
// escape
if (e.keyCode === 27) {
_this.cancelEdit();
} // right arrow
if (e.keyCode === 39 && _this.node && typeof _this.node.selectionStart === 'number' && _this.node.selectionStart === _this.node.value.length && _this.node.selectionStart === _this.node.selectionEnd) {
_this.selectRightCell();
} // tab
if (e.keyCode === 9) {
e.preventDefault();
_this.selectRightCell();
} // left arrow
if (e.keyCode === 37 && _this.node && typeof _this.node.selectionStart === 'number' && _this.node.selectionStart === 0 && _this.node.selectionStart === _this.node.selectionEnd) {
_this.selectLeftCell();
}
});
_defineProperty(_assertThisInitialized(_this), "getCommonProps", function () {
var _this$props4 = _this.props,
style = _this$props4.style,
className = _this$props4.className,
rowIndex = _this$props4.rowIndex,
columnIndex = _this$props4.columnIndex,
width = _this$props4.width,
alignment = _this$props4.alignment,
height = _this$props4.height,
fontSize = _this$props4.fontSize,
fontWeight = _this$props4.fontWeight;
var _this$state = _this.state,
value = _this$state.value,
display = _this$state.display;
return {
style: style,
className: className,
COLUMN_INDEX_ATTRIBUTE: columnIndex,
ROW_INDEX_ATTRIBUTE: rowIndex,
width: width,
height: height,
fontSize: fontSize,
fontWeight: fontWeight,
value: value,
display: display,
alignment: alignment
};
});
_defineProperty(_assertThisInitialized(_this), "getInputProps", function (_ref2) {
var _objectSpread3;
var _ref2$refKey = _ref2.refKey,
refKey = _ref2$refKey === void 0 ? 'ref' : _ref2$refKey;
return _objectSpread({}, _this.getCommonProps(), (_objectSpread3 = {
onBlur: _this.leaveEditState
}, _defineProperty(_objectSpread3, refKey, _this.refHandler), _defineProperty(_objectSpread3, "onChange", _this.inputValueChanged), _defineProperty(_objectSpread3, "onKeyDown", _this.inputKeyDown), _defineProperty(_objectSpread3, 'data-testid', 'cell-editing-element'), _objectSpread3));
});
_defineProperty(_assertThisInitialized(_this), "dropdownValueChanged", function (_ref3) {
var value = _ref3.value;
// console.log('selected and committing', value)
_this.commitEdit(value);
});
_defineProperty(_assertThisInitialized(_this), "getDropdownProps", function (_ref4) {
var _objectSpread4;
var _ref4$refKey = _ref4.refKey,
refKey = _ref4$refKey === void 0 ? 'ref' : _ref4$refKey;
var choices = _this.props.header && _this.props.header.choices;
var acceptRawText = _this.props.header && _this.props.header.acceptRawText || false;
var virtualized = choices && choices.length > 10;
var onKeyDown = virtualized ? _this.dropdownInputKeyDown : _this.dropdownKeyDown;
return _objectSpread({}, _this.getCommonProps(), (_objectSpread4 = {
onBlur: _this.leaveEditState
}, _defineProperty(_objectSpread4, refKey, _this.refHandler), _defineProperty(_objectSpread4, "onChange", _this.dropdownValueChanged), _defineProperty(_objectSpread4, "onKeyDown", onKeyDown), _defineProperty(_objectSpread4, "choices", choices), _defineProperty(_objectSpread4, "virtualized", virtualized), _defineProperty(_objectSpread4, "acceptRawText", acceptRawText), _defineProperty(_objectSpread4, "valueChanged", _this.valueChanged), _defineProperty(_objectSpread4, 'data-testid', 'cell-editing-element'), _objectSpread4));
});
return _this;
}
_createClass(CellEditContainer, [{
key: "componentDidMount",
value: function componentDidMount() {
// console.log(this.node)
// console.log(this.node.focus)
if (this.node && this.node.focus) {
this.node.focus();
if (this.node.select) {
this.node.select(); // this.node.setSelectionRange(0, this.node.value.length)
}
}
}
}, {
key: "render",
value: function render() {
var _this$props5 = this.props,
render = _this$props5.render,
rest = _objectWithoutProperties(_this$props5, ["render"]);
return render(_objectSpread({}, rest, {
getInputProps: this.getInputProps,
getDropdownProps: this.getDropdownProps
}));
}
}]);
return CellEditContainer;
}(_react["default"].Component);
var _default = CellEditContainer;
exports["default"] = _default;
//# sourceMappingURL=CellEditContainer.js.map