UNPKG

@react-awesome-query-builder/ui

Version:
228 lines (227 loc) 10.8 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; 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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } 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 { Utils } from "@react-awesome-query-builder/core"; import PropTypes from "prop-types"; import { truncateString } from "../../utils/stuff"; import { useOnPropsChanged } from "../../utils/reactUtils"; import last from "lodash/last"; import keys from "lodash/keys"; var getFieldPathLabels = Utils.RuleUtils.getFieldPathLabels; var _Utils$ConfigUtils = Utils.ConfigUtils, getFieldConfig = _Utils$ConfigUtils.getFieldConfig, getFieldParts = _Utils$ConfigUtils.getFieldParts, getFieldPathParts = _Utils$ConfigUtils.getFieldPathParts; var Field = /*#__PURE__*/function (_Component) { function Field(props) { var _this; _classCallCheck(this, Field); _this = _callSuper(this, Field, [props]); _this.setField = function (field, asyncListValues) { var _meta = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; var id = _this.props.id; if (!_meta.widgetId) { var widgetId = [id, "L", -1].join(":"); _meta.widgetId = widgetId; } _this.props.setField(field, asyncListValues, _meta); }; useOnPropsChanged(_this); _this.onPropsChanged(props); return _this; } _inherits(Field, _Component); return _createClass(Field, [{ key: "onPropsChanged", value: function onPropsChanged(nextProps) { var prevProps = this.props; var keysForMeta = ["selectedField", "selectedFieldSrc", "selectedFieldType", "config", "parentField"]; var needUpdateMeta = !this.meta || keysForMeta.map(function (k) { return nextProps[k] !== prevProps[k]; }).filter(function (ch) { return ch; }).length > 0; if (needUpdateMeta) { this.meta = this.getMeta(nextProps); } } }, { key: "getMeta", value: function getMeta(_ref) { var selectedField = _ref.selectedField, selectedFieldType = _ref.selectedFieldType, config = _ref.config, parentField = _ref.parentField; var selectedKey = selectedField; var _config$settings = config.settings, maxLabelsLength = _config$settings.maxLabelsLength, fieldSeparatorDisplay = _config$settings.fieldSeparatorDisplay, fieldPlaceholder = _config$settings.fieldPlaceholder, fieldSeparator = _config$settings.fieldSeparator; var isFieldSelected = !!selectedField; var placeholder = !isFieldSelected ? truncateString(fieldPlaceholder, maxLabelsLength) : null; var currField = isFieldSelected ? getFieldConfig(config, selectedKey) : null; var selectedOpts = currField || {}; var selectedKeys = getFieldPathParts(selectedKey, config); var selectedPath = getFieldPathParts(selectedKey, config, true); var selectedLabel = this.getFieldLabel(currField, selectedKey, config); var partsLabels = getFieldPathLabels(selectedKey, config); var selectedFullLabel = partsLabels ? partsLabels.join(fieldSeparatorDisplay) : null; if (selectedFullLabel == selectedLabel || parentField) selectedFullLabel = null; var selectedAltLabel = selectedOpts.label2 || selectedOpts.tooltip; var parentFieldPath = getFieldParts(parentField, config); var parentFieldConfig = parentField ? getFieldConfig(config, parentField) : null; var sourceFields = parentField ? parentFieldConfig && parentFieldConfig.subfields : config.fields; var lookingForFieldType = !isFieldSelected && selectedFieldType; var items = this.buildOptions(parentFieldPath, config, sourceFields, lookingForFieldType, parentFieldPath); // Field source has been chnaged, no new field selected, but op & value remains var errorText = lookingForFieldType ? "Please select field" : null; if (selectedKey && _typeof(selectedKey) === "object") { // can happen due to incorrect rule state: field is Map{func, args} but fieldSrc is not "func" selectedKey = undefined; } return { placeholder: placeholder, items: items, parentField: parentField, selectedKey: selectedKey, selectedKeys: selectedKeys, selectedPath: selectedPath, selectedLabel: selectedLabel, selectedOpts: selectedOpts, selectedAltLabel: selectedAltLabel, selectedFullLabel: selectedFullLabel, errorText: errorText }; } }, { key: "getFieldLabel", value: function getFieldLabel(fieldOpts, fieldKey, config) { if (!fieldKey) return null; var maxLabelsLength = config.settings.maxLabelsLength; var fieldParts = getFieldParts(fieldKey, config); var label = (fieldOpts === null || fieldOpts === void 0 ? void 0 : fieldOpts.label) || last(fieldParts); label = truncateString(label, maxLabelsLength); return label; } }, { key: "buildOptions", value: function buildOptions(parentFieldPath, config, fields) { var _this2 = this; var fieldType = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : undefined; var path = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : null; var optGroup = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : null; if (!fields) return null; var _config$settings2 = config.settings, fieldSeparator = _config$settings2.fieldSeparator, fieldSeparatorDisplay = _config$settings2.fieldSeparatorDisplay; var prefix = path !== null && path !== void 0 && path.length ? path.join(fieldSeparator) + fieldSeparator : ""; var _countFieldsMatchesType = function countFieldsMatchesType(fields) { return Object.keys(fields || {}).reduce(function (acc, fieldKey) { var field = fields[fieldKey]; if (field.type === "!struct") { return acc + _countFieldsMatchesType(field.subfields); } else { return acc + (field.type === fieldType ? 1 : 0); } }, 0); }; return keys(fields).map(function (fieldKey) { var fullFieldPath = [].concat(_toConsumableArray(path !== null && path !== void 0 ? path : []), [fieldKey]); var field = fields[fieldKey]; var label = _this2.getFieldLabel(field, fullFieldPath, config); var partsLabels = getFieldPathLabels(fullFieldPath, config); var fullLabel = partsLabels.join(fieldSeparatorDisplay); if (fullLabel == label || parentFieldPath !== null && parentFieldPath !== void 0 && parentFieldPath.length) fullLabel = null; var altLabel = field.label2; var tooltip = field.tooltip; var disabled = field.disabled; if (field.hideForSelect) return undefined; if (field.type == "!struct") { var items = _this2.buildOptions(parentFieldPath, config, field.subfields, fieldType, fullFieldPath, { label: label, tooltip: tooltip }); var hasItemsMatchesType = _countFieldsMatchesType(field.subfields) > 0; return { disabled: disabled, key: fieldKey, path: prefix + fieldKey, label: label, fullLabel: fullLabel, altLabel: altLabel, tooltip: tooltip, items: items, matchesType: hasItemsMatchesType }; } else { var matchesType = fieldType !== undefined ? field.type === fieldType : undefined; return { disabled: disabled, key: fieldKey, path: prefix + fieldKey, label: label, fullLabel: fullLabel, altLabel: altLabel, tooltip: tooltip, grouplabel: optGroup === null || optGroup === void 0 ? void 0 : optGroup.label, group: optGroup, matchesType: matchesType }; } }).filter(function (o) { return !!o; }); } }, { key: "render", value: function render() { var _this$props = this.props, config = _this$props.config, customProps = _this$props.customProps, setFieldSrc = _this$props.setFieldSrc, readonly = _this$props.readonly, id = _this$props.id, groupId = _this$props.groupId; var renderField = config.settings.renderField; if (!this.meta.items) { return null; } var renderProps = _objectSpread({ id: id, groupId: groupId, config: config, customProps: customProps, readonly: readonly, setField: this.setField, setFieldSrc: setFieldSrc }, this.meta); return renderField(renderProps, config.ctx); } }]); }(Component); Field.propTypes = { id: PropTypes.string, groupId: PropTypes.string, config: PropTypes.object.isRequired, selectedField: PropTypes.any, selectedFieldSrc: PropTypes.string, selectedFieldType: PropTypes.string, parentField: PropTypes.string, customProps: PropTypes.object, readonly: PropTypes.bool, //actions setField: PropTypes.func.isRequired, setFieldSrc: PropTypes.func }; export { Field as default };