UNPKG

@bigfishtv/cockpit

Version:

131 lines (116 loc) 5.14 kB
var _class, _temp; 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; } import React, { Component } from 'react'; import PropTypes from 'prop-types'; import _get from 'lodash/get'; var SelectFolderItemInput = (_temp = _class = function (_Component) { _inherits(SelectFolderItemInput, _Component); function SelectFolderItemInput() { _classCallCheck(this, SelectFolderItemInput); return _possibleConstructorReturn(this, _Component.apply(this, arguments)); } SelectFolderItemInput.prototype.renderOptions = function renderOptions(folder) { var index = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; var total = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1; var sameDepthItemTotal = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0; var depth = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0; var options = []; var _props = this.props, items = _props.items, folderKey = _props.folderKey, folderTitleKey = _props.folderTitleKey, folderChildrenKey = _props.folderChildrenKey, itemKey = _props.itemKey, itemTitleKey = _props.itemTitleKey, itemFolderKey = _props.itemFolderKey, folderIcon = _props.folderIcon, itemIcon = _props.itemIcon; var childFolders = _get(folder, folderChildrenKey, []); var childItems = items.filter(function (item) { return _get(item, itemFolderKey) === _get(folder, folderKey); }); var isFirstFolder = index === 0; var isLastFolder = index == total - 1; var isActualFirstFolder = isFirstFolder && depth === 1; var isLastFolderWithNoSiblingItems = isLastFolder && sameDepthItemTotal === 0; options.push(React.createElement( 'option', { value: null, key: 'folder' + folder.id }, Array(depth).join('│'), depth > 0 && (isActualFirstFolder ? '┌' : isLastFolderWithNoSiblingItems ? '└' : '├'), (depth > 0 ? folderIcon + ' ' : '') + _get(folder, folderTitleKey) )); if (childFolders.length > 0) { for (var f = 0; f < childFolders.length; f++) { options = [].concat(options, [this.renderOptions(childFolders[f], f, childFolders.length, childItems.length, depth + 1)]); } } for (var i = 0; i < childItems.length; i++) { var item = childItems[i]; var isLast = i === childItems.length - 1; options.push(React.createElement( 'option', { value: _get(item, itemKey), key: 'email' + item.id }, Array(depth).join('│'), depth > 0 && (isLastFolderWithNoSiblingItems ? ' ' : '│'), isLast ? '└' : '├', itemIcon + ' ' + _get(item, itemTitleKey) )); } return options; }; SelectFolderItemInput.prototype.render = function render() { var _renderOptions; var _props2 = this.props, value = _props2.value, _onChange = _props2.onChange, folders = _props2.folders, folderKey = _props2.folderKey, folderTitleKey = _props2.folderTitleKey, folderChildrenKey = _props2.folderChildrenKey; var options = this.renderOptions((_renderOptions = {}, _renderOptions[folderKey] = null, _renderOptions[folderTitleKey] = 'Please select', _renderOptions[folderChildrenKey] = folders, _renderOptions)); return React.createElement( 'label', { className: 'select' }, React.createElement( 'select', { value: value, onChange: function onChange(e) { return _onChange(e.target.value); } }, options ) ); }; return SelectFolderItemInput; }(Component), _class.propTypes = { folders: PropTypes.array.isRequired, items: PropTypes.array.isRequired, onChange: PropTypes.func, value: PropTypes.any, folderKey: PropTypes.string, folderTitleKey: PropTypes.string, folderParentKey: PropTypes.string, folderChildrenKey: PropTypes.string, itemKey: PropTypes.string, itemTitleKey: PropTypes.string, itemFolderKey: PropTypes.string, folderIcon: PropTypes.string, itemIcon: PropTypes.string }, _class.defaultProps = { folders: [], items: [], onChange: function onChange() {}, value: null, folderKey: 'id', folderTitleKey: 'title', folderParentKey: 'parent_id', folderChildrenKey: 'children', itemKey: 'id', itemTitleKey: 'title', itemFolderKey: 'folder_id', folderIcon: '📂', itemIcon: '📄' }, _temp); export { SelectFolderItemInput as default };