UNPKG

@integec/grid-tools

Version:
290 lines (242 loc) 11.9 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _isNil = require('ramda/src/isNil'); var _isNil2 = _interopRequireDefault(_isNil); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; 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; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _utils = require('./utils'); var _constants = require('./constants.js'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } 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; } /* eslint-disable no-unused-vars */ /* eslint-enable no-unused-vars */ // import PropTypes from 'prop-types' // TODO: determine prop types later var getInitialState = function getInitialState(_ref) { var rowIndex = _ref.rowIndex, columnIndex = _ref.columnIndex, header = _ref.header, width = _ref.width, height = _ref.height, data = _ref.data, render = _ref.render; 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 = function (_React$Component) { _inherits(CellEditContainer, _React$Component); function CellEditContainer() { var _ref2; var _temp, _this, _ret; _classCallCheck(this, CellEditContainer); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref2 = CellEditContainer.__proto__ || Object.getPrototypeOf(CellEditContainer)).call.apply(_ref2, [this].concat(args))), _this), _this.state = getInitialState(_this.props), _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 }); }, _this.inputValueChanged = function (e) { _this.valueChanged(e.target.value); }, _this.refHandler = function (n) { return _this.node = n; }, _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 (commitRowEdit) { commitRowEdit({ currentRow: rowData, editedRow: _extends({}, 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'); } }, _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 }); } }, _this.leaveEditState = function () { if (_this.props.value !== _this.state.value) { _this.commitEdit(); return; } _this.cancelEdit(); }, _this.selectLeftCell = function () { var selectLeft = _this.props.selectLeft; _this.leaveEditState(); selectLeft && selectLeft(); }, _this.selectRightCell = function () { var selectRight = _this.props.selectRight; _this.leaveEditState(); selectRight && selectRight(); }, _this.selectTopCell = function () { var selectTop = _this.props.selectTop; _this.leaveEditState(); selectTop && selectTop(); }, _this.selectBottomCell = function () { var selectBottom = _this.props.selectBottom; _this.leaveEditState(); selectBottom && selectBottom(); }, _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(); }, _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(); }, _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(); } }, _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 }; }, _this.getInputProps = function (_ref3) { var _extends3; var _ref3$refKey = _ref3.refKey, refKey = _ref3$refKey === undefined ? 'ref' : _ref3$refKey; return _extends({}, _this.getCommonProps(), (_extends3 = { onBlur: _this.leaveEditState }, _defineProperty(_extends3, refKey, _this.refHandler), _defineProperty(_extends3, 'onChange', _this.inputValueChanged), _defineProperty(_extends3, 'onKeyDown', _this.inputKeyDown), _defineProperty(_extends3, 'data-testid', 'cell-editing-element'), _extends3)); }, _this.dropdownValueChanged = function (_ref4) { var value = _ref4.value; console.log('selected and committing', value); _this.commitEdit(value); }, _this.getDropdownProps = function (_ref5) { var _extends4; var _ref5$refKey = _ref5.refKey, refKey = _ref5$refKey === undefined ? 'ref' : _ref5$refKey; var choices = _this.props.header && _this.props.header.choices; var virtualized = choices && choices.length > 10; var onKeyDown = virtualized ? _this.dropdownInputKeyDown : _this.dropdownKeyDown; return _extends({}, _this.getCommonProps(), (_extends4 = { onBlur: _this.leaveEditState }, _defineProperty(_extends4, refKey, _this.refHandler), _defineProperty(_extends4, 'onChange', _this.dropdownValueChanged), _defineProperty(_extends4, 'onKeyDown', onKeyDown), _defineProperty(_extends4, 'choices', choices), _defineProperty(_extends4, 'virtualized', virtualized), _defineProperty(_extends4, 'data-testid', 'cell-editing-element'), _extends4)); }, _temp), _possibleConstructorReturn(_this, _ret); } _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.setSelectionRange) { this.node.setSelectionRange(0, this.node.value.length); } } } /** navigation props ***/ }, { key: 'render', value: function render() { var render = this.props.render; return render({ getInputProps: this.getInputProps, getDropdownProps: this.getDropdownProps }); } }]); return CellEditContainer; }(_react2.default.Component); exports.default = CellEditContainer; //# sourceMappingURL=CellEditContainer.js.map