UNPKG

@react-awesome-query-builder/antd

Version:
147 lines 6.22 kB
import _extends from "@babel/runtime/helpers/extends"; import _typeof from "@babel/runtime/helpers/typeof"; 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 PropTypes from "prop-types"; import { TreeSelect } from "antd"; import { calcTextWidth, SELECT_WIDTH_OFFSET_RIGHT } from "../../utils/domUtils"; import { defaultTreeDataMap } from "../../utils/stuff"; import { Utils } from "@react-awesome-query-builder/ui"; var useOnPropsChanged = Utils.ReactUtils.useOnPropsChanged; var _Utils$ListUtils = Utils.ListUtils, getTitleInListValues = _Utils$ListUtils.getTitleInListValues, mapListValues = _Utils$ListUtils.mapListValues; var TreeSelectWidget = /*#__PURE__*/function (_Component) { function TreeSelectWidget(props) { var _this; _classCallCheck(this, TreeSelectWidget); _this = _callSuper(this, TreeSelectWidget, [props]); _this.handleChange = function (val) { if (!_this.props.treeMultiple) { _this.props.setValue(val); return; } if (val && !val.length) { _this.props.setValue(undefined); //not allow [] return; } if (_typeof(val[0]) == "object" && val[0].value !== undefined) { //`treeCheckStrictly` is on val = val.map(function (v) { return v.value; }); } _this.props.setValue(val); }; _this.filterTreeNode = function (input, option) { var dataForFilter = option.title; return dataForFilter.toLowerCase().indexOf(input.toLowerCase()) >= 0; }; useOnPropsChanged(_this); _this.onPropsChanged(props); return _this; } _inherits(TreeSelectWidget, _Component); return _createClass(TreeSelectWidget, [{ key: "onPropsChanged", value: function onPropsChanged(nextProps) { var listValues = nextProps.listValues, treeValues = nextProps.treeValues, treeMultiple = nextProps.treeMultiple; var treeData = treeValues || listValues; var optionsMaxWidth = 0; var initialOffset = treeMultiple ? 24 + 22 : 24; // arrow + checkbox for leftmost item var offset = 20; var padding = 5 * 2; mapListValues(treeData, function (_ref) { var title = _ref.title, value = _ref.value, path = _ref.path; optionsMaxWidth = Math.max(optionsMaxWidth, calcTextWidth(title, null) + padding + (path ? path.length : 0) * offset + initialOffset); }); if (!isNaN(optionsMaxWidth) && optionsMaxWidth) { this.optionsMaxWidth = optionsMaxWidth; } } }, { key: "render", value: function render() { var _this$props = this.props, config = _this$props.config, placeholder = _this$props.placeholder, _this$props$customPro = _this$props.customProps, customProps = _this$props$customPro === void 0 ? {} : _this$props$customPro, value = _this$props.value, treeMultiple = _this$props.treeMultiple, listValues = _this$props.listValues, treeValues = _this$props.treeValues, treeExpandAll = _this$props.treeExpandAll, readonly = _this$props.readonly; var treeData = treeValues || listValues; var treeCheckStrictly = customProps.treeCheckStrictly || false; var renderSize = config.settings.renderSize; var placeholderWidth = calcTextWidth(placeholder); var aValue = value != undefined ? value : undefined; if (treeCheckStrictly && aValue !== undefined) { if (treeMultiple) { aValue = aValue.map(function (v) { return { value: v, label: getTitleInListValues(treeData, v) }; }); } } var width = aValue || !placeholderWidth ? null : placeholderWidth + SELECT_WIDTH_OFFSET_RIGHT + 6; 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)); return /*#__PURE__*/React.createElement(TreeSelect, _extends({ disabled: readonly, style: { minWidth: width, width: width }, dropdownStyle: { width: useAutoWidth ? "auto" : dropdownWidth + 10, paddingRight: "10px" }, multiple: treeMultiple, treeCheckable: treeMultiple, key: "widget-treeselect", popupMatchSelectWidth: false, placeholder: placeholder, size: renderSize, treeData: treeData, treeDataSimpleMode: defaultTreeDataMap, filterTreeNode: this.filterTreeNode, value: aValue, onChange: this.handleChange, treeDefaultExpandAll: treeExpandAll }, customProps)); } }]); }(Component); TreeSelectWidget.propTypes = { setValue: PropTypes.func.isRequired, config: PropTypes.object.isRequired, value: PropTypes.oneOfType([PropTypes.string, PropTypes.array]), field: PropTypes.any, placeholder: PropTypes.string, customProps: PropTypes.object, fieldDefinition: PropTypes.object, readonly: PropTypes.bool, treeMultiple: PropTypes.bool, // from fieldSettings: listValues: PropTypes.oneOfType([PropTypes.object, PropTypes.array]), // obsolete treeValues: PropTypes.oneOfType([PropTypes.object, PropTypes.array]) }; export { TreeSelectWidget as default };