UNPKG

@integec/grid-tools

Version:
385 lines (315 loc) 15.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _find2 = _interopRequireDefault(require("ramda/src/find")); var _findIndex2 = _interopRequireDefault(require("ramda/src/findIndex")); var _isNil2 = _interopRequireDefault(require("ramda/src/isNil")); var _react = _interopRequireDefault(require("react")); var _downshift = _interopRequireDefault(require("downshift")); var _PortaledPopper = _interopRequireDefault(require("../PortaledPopper")); var _styledComponents = _interopRequireDefault(require("styled-components")); var _reactVirtualized = require("react-virtualized"); 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 _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 _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 _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 _extends() { _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; }; return _extends.apply(this, arguments); } function _templateObject4() { var data = _taggedTemplateLiteral(["\n background-color: white;\n border: 1px solid black;\n"]); _templateObject4 = function _templateObject4() { return data; }; return data; } function _templateObject3() { var data = _taggedTemplateLiteral(["\n font-weight: ", ";\n color: ", ";\n background-color: ", ";\n padding: 4px 0.3em;\n cursor: pointer;\n"]); _templateObject3 = function _templateObject3() { return data; }; return data; } function _templateObject2() { var data = _taggedTemplateLiteral(["\n ", ";\n min-height: 30px;\n"]); _templateObject2 = function _templateObject2() { return data; }; return data; } function _templateObject() { var data = _taggedTemplateLiteral(["\n ", ";\n"]); _templateObject = function _templateObject() { return data; }; return data; } function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } 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, _styledComponents["default"])(_reactVirtualized.List)(_templateObject(), listContainerStyle); var BasicList = _styledComponents["default"].div(_templateObject2(), listContainerStyle); var Item = _styledComponents["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 = _styledComponents["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 _react["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, 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 index = _ref4.index, 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 _react["default"].createElement("div", { ref: ref, style: _objectSpread({}, style, { zIndex: 100000 }) }, _react["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, selectedItem = _ref5.selectedItem, highlightedIndex = _ref5.highlightedIndex, ref = _ref5.ref, style = _ref5.style, choices = _ref5.choices, minWidth = _ref5.minWidth; // console.log('basic list style is ', style) return _react["default"].createElement(BasicList, { innerRef: ref, style: _objectSpread({}, style, { minWidth: minWidth + 'px', zIndex: 100000 }) }, choices.map(function (item, index) { return renderListItem({ getItemProps: getItemProps, item: item, index: index, selectedItem: selectedItem, highlightedIndex: highlightedIndex }); })); }; var DropDownCellEditor = /*#__PURE__*/ function (_React$Component) { _inherits(DropDownCellEditor, _React$Component); function DropDownCellEditor() { var _getPrototypeOf2; var _this; _classCallCheck(this, DropDownCellEditor); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(DropDownCellEditor)).call.apply(_getPrototypeOf2, [this].concat(args))); _defineProperty(_assertThisInitialized(_this), "handelListRef", function (node) { _this.list = node; // console.log('list is', node) }); _defineProperty(_assertThisInitialized(_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); } } }); _defineProperty(_assertThisInitialized(_this), "state", { showSelection: true, justOpened: true }); _defineProperty(_assertThisInitialized(_this), "inputValueChanged", function (value) { // console.log('value changed to ', value) var _this$props2 = _this.props, acceptRawText = _this$props2.acceptRawText, valueChanged = _this$props2.valueChanged; if (acceptRawText) { valueChanged(value); } _this.setState({ justOpened: false }); }); _defineProperty(_assertThisInitialized(_this), "renderInput", function (_ref6) { var getInputProps = _ref6.getInputProps, placeholder = _ref6.placeholder, onKeyDown = _ref6.onKeyDown, _ref6$style = _ref6.style, style = _ref6$style === void 0 ? {} : _ref6$style, className = _ref6.className, _ref6$width = _ref6.width, width = _ref6$width === void 0 ? 150 : _ref6$width, _ref6$height = _ref6.height, height = _ref6$height === void 0 ? 25 : _ref6$height, handleRef = _ref6.handleRef; return _react["default"].createElement("input", _extends({}, getInputProps({ placeholder: placeholder, onKeyDown: onKeyDown }), { ref: handleRef, style: _objectSpread({}, style, { width: width + 'px', height: height + 'px' }), onBlur: _this.handleBlur, className: className })); }); _defineProperty(_assertThisInitialized(_this), "renderComboBox", function (_ref7) { var getToggleButtonProps = _ref7.getToggleButtonProps, placeholder = _ref7.placeholder, onKeyDown = _ref7.onKeyDown, selectedItem = _ref7.selectedItem, _ref7$style = _ref7.style, style = _ref7$style === void 0 ? {} : _ref7$style, className = _ref7.className, _ref7$width = _ref7.width, width = _ref7$width === void 0 ? 150 : _ref7$width, _ref7$height = _ref7.height, height = _ref7$height === void 0 ? 25 : _ref7$height, handleRef = _ref7.handleRef; return _react["default"].createElement(ComboSelector, _extends({ innerRef: handleRef }, getToggleButtonProps({ onKeyDown: onKeyDown }), { style: _objectSpread({}, style, { width: width + 'px', height: height + 'px' }), onBlur: _this.handleBlur, className: className }), selectedItem ? selectedItem.text || selectedItem.value : placeholder || 'Select a value'); }); return _this; } _createClass(DropDownCellEditor, [{ key: "render", value: function render() { var _this2 = this; var _this$props3 = this.props, choices = _this$props3.choices, value = _this$props3.value, onChange = _this$props3.onChange, virtualized = _this$props3.virtualized, zIndex = _this$props3.zIndex, acceptRawText = _this$props3.acceptRawText; // eslint-disable-next-line standard/object-curly-even-spacing var justOpened = this.state.justOpened; var renderList = virtualized ? renderVirtualizedList : renderBasicList; var renderSelector = virtualized || acceptRawText ? 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 _react["default"].createElement(_downshift["default"], { defaultSelectedItem: selectedItem, defaultHighlightedIndex: hilightedIndex, onInputValueChange: this.inputValueChanged, onChange: onChange // eslint-disable-next-line standard/object-curly-even-spacing , itemToString: function itemToString(_ref8) { var text = _ref8.text; return text || value + ''; } }, function (downshiftProps) { return _react["default"].createElement("div", null, _react["default"].createElement(_PortaledPopper["default"], { popperVisible: true, referenceInnerRef: _this2.props.innerRef, popperRender: function popperRender(popperProps) { return renderList(_objectSpread({}, downshiftProps, {}, popperProps, { choices: choices, minWidth: _this2.props.width == null ? 150 : _this2.props.width, justOpened: justOpened, ref: popperProps.ref, zIndex: zIndex })); } }, function (_ref9) { var ref = _ref9.ref; return renderSelector(_objectSpread({}, _this2.props, {}, downshiftProps, { handleRef: ref })); })); }); } }]); return DropDownCellEditor; }(_react["default"].Component); var _default = DropDownCellEditor; exports["default"] = _default; //# sourceMappingURL=DropdownCellEditor.js.map