UNPKG

@react-awesome-query-builder/antd

Version:
208 lines 8.26 kB
import _extends from "@babel/runtime/helpers/extends"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/inherits"; function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } import React, { Component } from "react"; import { Tooltip, TreeSelect } from "antd"; import { BUILT_IN_PLACEMENTS, SELECT_WIDTH_OFFSET_RIGHT, calcTextWidth } from "../../utils/domUtils"; import PropTypes from "prop-types"; import { Utils } from "@react-awesome-query-builder/ui"; var useOnPropsChanged = Utils.ReactUtils.useOnPropsChanged; var mapFieldItemToOptionKeys = { key: "_value2", path: "value", label: "label", altLabel: "altLabel", tooltip: "_tooltip", grouplabel: "_grouplabel", fullLabel: "fullLabel" }; var FieldTreeSelect = /*#__PURE__*/function (_Component) { function FieldTreeSelect(props) { var _this; _classCallCheck(this, FieldTreeSelect); _this = _callSuper(this, FieldTreeSelect, [props]); _this.setOpen = function (newOpen) { _this.setState({ open: newOpen }); }; _this.onChange = function (key) { _this.props.setField(key); }; _this.filterTreeNode = function (input, option) { var config = _this.props.config; var keysForFilter = config.settings.fieldItemKeysForSearch.map(function (k) { return mapFieldItemToOptionKeys[k]; }); var valueForFilter = keysForFilter.map(function (k) { return typeof option[k] == "string" ? option[k] : ""; }).join("\0"); var matches = valueForFilter.toLowerCase().indexOf(input.toLowerCase()) >= 0; return matches; }; useOnPropsChanged(_this); _this.onPropsChanged(props); _this.state = { open: false }; return _this; } _inherits(FieldTreeSelect, _Component); return _createClass(FieldTreeSelect, [{ key: "onPropsChanged", value: function onPropsChanged(nextProps) { var items = nextProps.items, fieldSeparator = nextProps.config.settings.fieldSeparator; var optionsMaxWidth = 0; var initialOffset = 24; // arrow + checkbox for leftmost item var offset = 20; var padding = 5 * 2; this.treeData = this.getTreeData(items, function (_ref) { var label = _ref.label, path = _ref.path; optionsMaxWidth = Math.max(optionsMaxWidth, calcTextWidth(label, null) + padding + (path.split(fieldSeparator).length - 1) * offset + initialOffset); }); if (!isNaN(optionsMaxWidth) && optionsMaxWidth) { this.optionsMaxWidth = optionsMaxWidth; } } }, { key: "getTreeData", value: function getTreeData(fields) { var _this2 = this; var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; return fields.map(function (field) { var items = field.items, key = field.key, path = field.path, label = field.label, fullLabel = field.fullLabel, altLabel = field.altLabel, tooltip = field.tooltip, disabled = field.disabled, grouplabel = field.grouplabel, matchesType = field.matchesType; if (fn) fn(field); var pathKey = path || key; var optionText = matchesType ? /*#__PURE__*/React.createElement("b", null, label) : label; var option = tooltip ? /*#__PURE__*/React.createElement(Tooltip, { title: tooltip }, optionText) : optionText; if (items) { return { value: pathKey, title: option, children: _this2.getTreeData(items, fn), selectable: false, altLabel: altLabel, fullLabel: fullLabel, label: label, disabled: disabled, _value2: key, _tooltip: tooltip, _grouplabel: grouplabel }; } else { return { value: pathKey, title: option, altLabel: altLabel, fullLabel: fullLabel, label: label, disabled: disabled, _value2: key, _tooltip: tooltip, _grouplabel: grouplabel }; } }); } }, { key: "render", value: function render() { var _this$props = this.props, config = _this$props.config, _this$props$customPro = _this$props.customProps, customProps = _this$props$customPro === void 0 ? {} : _this$props$customPro, placeholder = _this$props.placeholder, errorText = _this$props.errorText, selectedKey = _this$props.selectedKey, selectedLabel = _this$props.selectedLabel, selectedOpts = _this$props.selectedOpts, selectedAltLabel = _this$props.selectedAltLabel, selectedFullLabel = _this$props.selectedFullLabel, readonly = _this$props.readonly; var open = this.state.open; var _config$settings = config.settings, renderSize = _config$settings.renderSize, fieldSeparator = _config$settings.fieldSeparator; var tooltipText = selectedAltLabel || selectedFullLabel; if (tooltipText == selectedLabel) tooltipText = null; var selectedPath = selectedKey ? selectedKey.split(fieldSeparator) : null; var treeDefaultExpandedKeys = selectedPath && selectedPath.length > 1 ? selectedPath.slice(0, -1).map(function (_key, i) { return selectedPath.slice(0, i + 1).join(fieldSeparator); }) : null; var placeholderWidth = calcTextWidth(placeholder); var isFieldSelected = !!selectedKey; var minWidth = placeholderWidth ? placeholderWidth + SELECT_WIDTH_OFFSET_RIGHT + 6 : null; var dropdownMinWidth = 100; var dropdownMaxWidth = 800; var useAutoWidth = true || !this.optionsMaxWidth; //tip: "auto" is good, but width will jump on expand/collapse var dropdownWidth = Math.max(dropdownMinWidth, Math.min(dropdownMaxWidth, this.optionsMaxWidth)); var res = /*#__PURE__*/React.createElement(TreeSelect, _extends({ open: open, onDropdownVisibleChange: this.setOpen, status: errorText && "error", onChange: this.onChange, value: selectedKey || undefined, style: { minWidth: minWidth, width: isFieldSelected ? null : minWidth }, dropdownStyle: { width: useAutoWidth ? "auto" : dropdownWidth + 20, paddingRight: "10px" }, multiple: false, treeCheckable: false, treeDataSimpleMode: false, treeData: this.treeData, size: renderSize, placeholder: placeholder, filterTreeNode: this.filterTreeNode, treeDefaultExpandedKeys: treeDefaultExpandedKeys, popupMatchSelectWidth: false, disabled: readonly }, customProps)); if (tooltipText && !selectedOpts.tooltip) { res = /*#__PURE__*/React.createElement(Tooltip, { title: !open ? tooltipText : null }, res); } return res; } }]); }(Component); FieldTreeSelect.propTypes = { config: PropTypes.object.isRequired, customProps: PropTypes.object, errorText: PropTypes.string, items: PropTypes.array.isRequired, placeholder: PropTypes.string, selectedKey: PropTypes.string, selectedKeys: PropTypes.array, selectedPath: PropTypes.array, selectedLabel: PropTypes.string, selectedAltLabel: PropTypes.string, selectedFullLabel: PropTypes.string, selectedOpts: PropTypes.object, readonly: PropTypes.bool, //actions setField: PropTypes.func.isRequired }; export { FieldTreeSelect as default };