UNPKG

@integec/grid-tools

Version:
338 lines (284 loc) 13.5 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _find = require('ramda/src/find'); var _find2 = _interopRequireDefault(_find); var _findIndex = require('ramda/src/findIndex'); var _findIndex2 = _interopRequireDefault(_findIndex); var _isNil = require('ramda/src/isNil'); var _isNil2 = _interopRequireDefault(_isNil); 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 _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 _templateObject = _taggedTemplateLiteral(['\n ', ';\n'], ['\n ', ';\n']), _templateObject2 = _taggedTemplateLiteral(['\n ', ';\n min-height: 30px;\n'], ['\n ', ';\n min-height: 30px;\n']), _templateObject3 = _taggedTemplateLiteral(['\n font-weight: ', ';\n color: ', ';\n background-color: ', ';\n padding: 4px 0.3em;\n cursor: pointer;\n'], ['\n font-weight: ', ';\n color: ', ';\n background-color: ', ';\n padding: 4px 0.3em;\n cursor: pointer;\n']), _templateObject4 = _taggedTemplateLiteral(['\n background-color: white;\n border: 1px solid black;\n'], ['\n background-color: white;\n border: 1px solid black;\n']); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _downshift = require('downshift'); var _downshift2 = _interopRequireDefault(_downshift); var _PortaledPopper = require('../PortaledPopper'); var _PortaledPopper2 = _interopRequireDefault(_PortaledPopper); var _styledComponents = require('styled-components'); var _styledComponents2 = _interopRequireDefault(_styledComponents); var _reactVirtualized = require('react-virtualized'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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; } function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } var listContainerStyle = '\n border: solid 1px #ccc;\n border-radius: 3px;\n background-color: white;\n'; var VirtualizedList = (0, _styledComponents2.default)(_reactVirtualized.List)(_templateObject, listContainerStyle); var BasicList = _styledComponents2.default.div(_templateObject2, listContainerStyle); var Item = _styledComponents2.default.div(_templateObject3, function (props) { return props.selected ? 'bold' : 'normal'; }, function (props) { return props.selected ? 'brown' : 'initial'; }, function (props) { return props.hilighted ? '#eee' : 'initial'; }); var ComboSelector = _styledComponents2.default.button(_templateObject4); // TODO: think about values that may be other primitives var partialMatch = function partialMatch(a, b) { return !(0, _isNil2.default)(a) && !(0, _isNil2.default)(b) && typeof a === 'string' && typeof b === 'string' && a.toLowerCase().includes(b.toLowerCase()); }; var matchesInput = function matchesInput(inputValue) { return function (_ref) { var value = _ref.value, text = _ref.text; return !inputValue || inputValue.trim().length === 0 || partialMatch(value, inputValue) || partialMatch(text, inputValue); }; }; var renderListItem = function renderListItem(_ref2) { var getItemProps = _ref2.getItemProps, style = _ref2.style, item = _ref2.item, index = _ref2.index, selectedItem = _ref2.selectedItem, highlightedIndex = _ref2.highlightedIndex; return _react2.default.createElement( Item, _extends({}, getItemProps({ item: item, index: index }), { key: item.value, style: style, selected: selectedItem === item, hilighted: highlightedIndex === index, 'data-testid': 'grid-cell-dropdown-item' }), item.text || item.value ); }; var renderVirtualizedList = function renderVirtualizedList(_ref3) { var getItemProps = _ref3.getItemProps, inputValue = _ref3.inputValue, selectedItem = _ref3.selectedItem, highlightedIndex = _ref3.highlightedIndex, ref = _ref3.ref, style = _ref3.style, placement = _ref3.placement, arrowProps = _ref3.arrowProps, choices = _ref3.choices, minWidth = _ref3.minWidth, justOpened = _ref3.justOpened; if (process.env.NODE_ENV === 'development') console.log('rendering virtualized list here..'); console.log('virtualized style is ', style); var visibleChoices = justOpened ? choices : choices.filter(matchesInput(inputValue)); var rowRenderer = function rowRenderer(_ref4) { var key = _ref4.key, index = _ref4.index, isScrolling = _ref4.isScrolling, isVisible = _ref4.isVisible, style = _ref4.style; return renderListItem({ getItemProps: getItemProps, style: style, item: visibleChoices[index], index: index, selectedItem: selectedItem, highlightedIndex: highlightedIndex }); }; // console.log('min width is ', visibleChoices.length) var index = (0, _findIndex2.default)(function (a) { return a === selectedItem; }, visibleChoices); return _react2.default.createElement( 'div', { ref: ref, style: style }, _react2.default.createElement(VirtualizedList, { width: minWidth, height: 250, rowCount: visibleChoices.length, rowHeight: 30, rowRenderer: rowRenderer, scrollToIndex: justOpened && index >= 0 ? index : undefined, id: 'drop-down-vr-list' }) ); }; var renderBasicList = function renderBasicList(_ref5) { var getItemProps = _ref5.getItemProps, inputValue = _ref5.inputValue, selectedItem = _ref5.selectedItem, highlightedIndex = _ref5.highlightedIndex, ref = _ref5.ref, style = _ref5.style, placement = _ref5.placement, arrowProps = _ref5.arrowProps, choices = _ref5.choices, minWidth = _ref5.minWidth; console.log('basic list style is ', style); return _react2.default.createElement( BasicList, { innerRef: ref, style: _extends({}, style, { minWidth: minWidth + 'px' }) }, choices.map(function (item, index) { return renderListItem({ getItemProps: getItemProps, item: item, index: index, selectedItem: selectedItem, highlightedIndex: highlightedIndex }); }) ); }; var DropDownCellEditor = function (_React$Component) { _inherits(DropDownCellEditor, _React$Component); function DropDownCellEditor() { var _ref6; var _temp, _this, _ret; _classCallCheck(this, DropDownCellEditor); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref6 = DropDownCellEditor.__proto__ || Object.getPrototypeOf(DropDownCellEditor)).call.apply(_ref6, [this].concat(args))), _this), _this.handelListRef = function (node) { _this.list = node; console.log('list is', node); }, _this.handleBlur = function (e) { console.log('blur is ', e); var _this$props = _this.props, virtualized = _this$props.virtualized, onBlur = _this$props.onBlur; if (!virtualized) { onBlur && onBlur(e); } else { if (!e.relatedTarget || e.relatedTarget.getAttribute('id') !== 'drop-down-vr-list') { onBlur && onBlur(e); } } }, _this.state = { showSelection: true, justOpened: true }, _this.inputValueChanged = function (value) { console.log('value changed to ', value); _this.setState({ justOpened: false }); }, _this.renderInput = function (_ref7) { var getInputProps = _ref7.getInputProps, placeholder = _ref7.placeholder, onKeyDown = _ref7.onKeyDown, _ref7$style = _ref7.style, style = _ref7$style === undefined ? {} : _ref7$style, className = _ref7.className, _ref7$width = _ref7.width, width = _ref7$width === undefined ? 150 : _ref7$width, _ref7$height = _ref7.height, height = _ref7$height === undefined ? 25 : _ref7$height, handleRef = _ref7.handleRef; return _react2.default.createElement('input', _extends({}, getInputProps({ placeholder: placeholder, onKeyDown: onKeyDown }), { ref: handleRef, style: _extends({}, style, { width: width + 'px', height: height + 'px' }), onBlur: _this.handleBlur, className: className })); }, _this.renderComboBox = function (_ref8) { var getToggleButtonProps = _ref8.getToggleButtonProps, placeholder = _ref8.placeholder, onKeyDown = _ref8.onKeyDown, selectedItem = _ref8.selectedItem, _ref8$style = _ref8.style, style = _ref8$style === undefined ? {} : _ref8$style, className = _ref8.className, _ref8$width = _ref8.width, width = _ref8$width === undefined ? 150 : _ref8$width, _ref8$height = _ref8.height, height = _ref8$height === undefined ? 25 : _ref8$height, handleRef = _ref8.handleRef; return _react2.default.createElement( ComboSelector, _extends({ innerRef: handleRef }, getToggleButtonProps({ onKeyDown: onKeyDown }), { style: _extends({}, style, { width: width + 'px', height: height + 'px' }), onBlur: _this.handleBlur, className: className }), selectedItem ? selectedItem.text || selectedItem.value : placeholder || 'Select a value' ); }, _temp), _possibleConstructorReturn(_this, _ret); } // innerRef={ref} _createClass(DropDownCellEditor, [{ key: 'render', value: function render() { var _this2 = this; var _props = this.props, choices = _props.choices, value = _props.value, onChange = _props.onChange, virtualized = _props.virtualized; var _state = this.state, showSelection = _state.showSelection, justOpened = _state.justOpened; var renderList = virtualized ? renderVirtualizedList : renderBasicList; var renderSelector = virtualized ? this.renderInput : this.renderComboBox; var selectedItem = (0, _find2.default)(function (c) { return value === c.value; }, choices); var hilightedIndex = (0, _findIndex2.default)(function (c) { return value === c.value; }, choices); return _react2.default.createElement( _downshift2.default, { defaultSelectedItem: selectedItem, defaultHighlightedIndex: hilightedIndex, onInputValueChange: this.inputValueChanged, onChange: onChange, itemToString: function itemToString(_ref9) { var vallue = _ref9.vallue, text = _ref9.text; return text || value + ''; } }, function (downshiftProps) { return _react2.default.createElement( 'div', null, _react2.default.createElement( _PortaledPopper2.default, { popperVisible: true, referenceInnerRef: _this2.props.innerRef, popperRender: function popperRender(popperProps) { return renderList(_extends({}, downshiftProps, popperProps, { choices: choices, minWidth: _this2.props.width == null ? 150 : _this2.props.width, justOpened: justOpened, ref: popperProps.ref })); } }, function (_ref10) { var ref = _ref10.ref; return renderSelector(_extends({}, _this2.props, downshiftProps, { handleRef: ref })); } ) ); } ); } }]); return DropDownCellEditor; }(_react2.default.Component); exports.default = DropDownCellEditor; //# sourceMappingURL=DropdownCellEditor.js.map