@react-awesome-query-builder/ui
Version:
User-friendly query builder for React. Core React UI
270 lines (269 loc) • 13.3 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
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 clone = Utils.OtherUtils.clone;
var _Utils$ConfigUtils = Utils.ConfigUtils,
getFieldConfig = _Utils$ConfigUtils.getFieldConfig,
getFieldParts = _Utils$ConfigUtils.getFieldParts,
getFieldPathParts = _Utils$ConfigUtils.getFieldPathParts,
getWidgetForFieldOp = _Utils$ConfigUtils.getWidgetForFieldOp;
var getFieldPathLabels = Utils.RuleUtils.getFieldPathLabels;
//tip: this.props.value - right value, this.props.field - left value
var ValueField = /*#__PURE__*/function (_Component) {
function ValueField(props) {
var _this;
_classCallCheck(this, ValueField);
_this = _callSuper(this, ValueField, [props]);
useOnPropsChanged(_this);
_this.onPropsChanged(props);
return _this;
}
_inherits(ValueField, _Component);
return _createClass(ValueField, [{
key: "onPropsChanged",
value: function onPropsChanged(nextProps) {
var prevProps = this.props;
var keysForItems = ["config", "field", "fieldSrc", "fieldType", "operator", "isFuncArg", "parentField"];
var keysForMeta = ["config", "field", "fieldSrc", "fieldType", "operator", "value", "placeholder", "isFuncArg", "parentField"];
var needUpdateItems = !this.items || keysForItems.map(function (k) {
return nextProps[k] !== prevProps[k];
}).filter(function (ch) {
return ch;
}).length > 0;
var needUpdateMeta = !this.meta || keysForMeta.map(function (k) {
return nextProps[k] !== prevProps[k];
}).filter(function (ch) {
return ch;
}).length > 0;
if (needUpdateItems) {
this.items = this.getItems(nextProps);
}
if (needUpdateMeta) {
this.meta = this.getMeta(nextProps);
}
}
}, {
key: "getItems",
value: function getItems(_ref) {
var config = _ref.config,
field = _ref.field,
fieldType = _ref.fieldType,
operator = _ref.operator,
parentField = _ref.parentField,
isFuncArg = _ref.isFuncArg,
fieldDefinition = _ref.fieldDefinition;
var canCompareFieldWithField = config.settings.canCompareFieldWithField;
// const fieldSeparator = config.settings.fieldSeparator;
var parentFieldPath = getFieldParts(parentField, config);
var parentFieldConfig = parentField ? getFieldConfig(config, parentField) : null;
var sourceFields = parentField ? parentFieldConfig === null || parentFieldConfig === void 0 ? void 0 : parentFieldConfig.subfields : config.fields;
var filteredFields = this.filterFields(config, sourceFields, field, parentField, parentFieldPath, operator, canCompareFieldWithField, isFuncArg, fieldDefinition, fieldType);
var items = this.buildOptions(parentFieldPath, config, filteredFields, parentFieldPath);
return items;
}
}, {
key: "getMeta",
value: function getMeta(_ref2) {
var _leftFieldConfig$widg;
var config = _ref2.config,
field = _ref2.field,
fieldType = _ref2.fieldType,
operator = _ref2.operator,
value = _ref2.value,
customPlaceholder = _ref2.placeholder,
isFuncArg = _ref2.isFuncArg,
parentField = _ref2.parentField;
var _config$settings = config.settings,
fieldPlaceholder = _config$settings.fieldPlaceholder,
fieldSeparatorDisplay = _config$settings.fieldSeparatorDisplay;
var selectedKey = value;
var isFieldSelected = !!value;
var leftFieldConfig = field ? getFieldConfig(config, field) : {};
var leftFieldWidgetField = leftFieldConfig === null || leftFieldConfig === void 0 || (_leftFieldConfig$widg = leftFieldConfig.widgets) === null || _leftFieldConfig$widg === void 0 ? void 0 : _leftFieldConfig$widg.field;
var leftFieldWidgetFieldProps = (leftFieldWidgetField === null || leftFieldWidgetField === void 0 ? void 0 : leftFieldWidgetField.widgetProps) || {};
var placeholder = isFieldSelected ? null : isFuncArg && customPlaceholder || leftFieldWidgetFieldProps.valuePlaceholder || fieldPlaceholder;
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;
return {
placeholder: placeholder,
selectedKey: selectedKey,
selectedKeys: selectedKeys,
selectedPath: selectedPath,
selectedLabel: selectedLabel,
selectedOpts: selectedOpts,
selectedAltLabel: selectedAltLabel,
selectedFullLabel: selectedFullLabel
};
}
}, {
key: "filterFields",
value: function filterFields(config, fields, leftFieldFullkey, parentField, parentFieldPath, operator, canCompareFieldWithField, isFuncArg, fieldDefinition, fieldType) {
fields = clone(fields);
var fieldSeparator = config.settings.fieldSeparator;
var leftFieldConfig = getFieldConfig(config, leftFieldFullkey);
var _relyOnWidgetType = false; //TODO: remove this, see issue #758
var widget = getWidgetForFieldOp(config, leftFieldFullkey, operator, "value");
var widgetConfig = config.widgets[widget];
var opConfig = config.operators[operator];
var expectedType;
if (isFuncArg) {
expectedType = fieldDefinition === null || fieldDefinition === void 0 ? void 0 : fieldDefinition.type;
} else if (opConfig !== null && opConfig !== void 0 && opConfig.valueTypes) {
expectedType = opConfig === null || opConfig === void 0 ? void 0 : opConfig.valueTypes[0];
} else if (_relyOnWidgetType && widgetConfig) {
expectedType = widgetConfig.type;
} else if (leftFieldConfig) {
expectedType = leftFieldConfig.type;
} else {
// no field at LHS, but can use type from "memory effect"
expectedType = fieldType;
}
function _filter(list, path) {
for (var rightFieldKey in list) {
var subfields = list[rightFieldKey].subfields;
var subpath = (path ? path : []).concat(rightFieldKey);
var rightFieldFullkey = subpath.join(fieldSeparator);
var rightFieldConfig = getFieldConfig(config, rightFieldFullkey);
if (!rightFieldConfig) {
delete list[rightFieldKey];
} else if (rightFieldConfig.type == "!struct" || rightFieldConfig.type == "!group") {
if (_filter(subfields, subpath) == 0) delete list[rightFieldKey];
} else {
// tip: LHS field can be used as arg in RHS function
var canUse = (!expectedType || rightFieldConfig.type == expectedType) && (isFuncArg ? true : rightFieldFullkey != leftFieldFullkey);
var fn = canCompareFieldWithField || config.settings.canCompareFieldWithField;
if (fn) canUse = canUse && fn(leftFieldFullkey, leftFieldConfig, rightFieldFullkey, rightFieldConfig, operator);
if (!canUse) delete list[rightFieldKey];
}
}
return keys(list).length;
}
_filter(fields, parentFieldPath || []);
return fields;
}
}, {
key: "buildOptions",
value: function buildOptions(parentFieldPath, config, fields) {
var _this2 = this;
var path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
var optGroup = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 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 : "";
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) fullLabel = null;
var altLabel = field.label2;
var tooltip = field.tooltip;
if (field.hideForCompare) return undefined;
if (field.type == "!struct" || field.type == "!group") {
return {
key: fieldKey,
path: prefix + fieldKey,
label: label,
fullLabel: fullLabel,
altLabel: altLabel,
tooltip: tooltip,
items: _this2.buildOptions(parentFieldPath, config, field.subfields, fullFieldPath, {
label: label,
tooltip: tooltip
}),
grouplabel: optGroup === null || optGroup === void 0 ? void 0 : optGroup.label,
group: optGroup
};
} else {
return {
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
};
}
}).filter(function (o) {
return !!o;
});
}
}, {
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: "render",
value: function render() {
var _this$props = this.props,
config = _this$props.config,
customProps = _this$props.customProps,
setValue = _this$props.setValue,
readonly = _this$props.readonly,
id = _this$props.id,
groupId = _this$props.groupId;
var renderField = config.settings.renderField;
var renderProps = _objectSpread({
isValueField: true,
config: config,
customProps: customProps,
setField: setValue,
readonly: readonly,
items: this.items,
id: id,
groupId: groupId
}, this.meta);
return renderField(renderProps, config.ctx);
}
}]);
}(Component);
ValueField.propTypes = {
id: PropTypes.string,
groupId: PropTypes.string,
setValue: PropTypes.func.isRequired,
config: PropTypes.object.isRequired,
field: PropTypes.any,
fieldSrc: PropTypes.string,
fieldType: PropTypes.string,
value: PropTypes.string,
operator: PropTypes.string,
customProps: PropTypes.object,
readonly: PropTypes.bool,
parentField: PropTypes.string,
fieldDefinition: PropTypes.object,
isFuncArg: PropTypes.bool
};
export { ValueField as default };