UNPKG

cspace-ui

Version:
348 lines (344 loc) 12.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _reactIntl = require("react-intl"); var _immutable = _interopRequireDefault(require("immutable")); var _cspaceInput = require("cspace-input"); var _OptionPickerInput = _interopRequireDefault(require("../../record/OptionPickerInput")); var _RemoveConditionButton = _interopRequireDefault(require("../RemoveConditionButton")); var _searchOperators = require("../../../constants/searchOperators"); var _BooleanConditionInput = _interopRequireDefault(require("../../../../styles/cspace-ui/BooleanConditionInput.css")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } const propTypes = { condition: _propTypes.default.instanceOf(_immutable.default.Map), config: _propTypes.default.shape({ recordTypes: _propTypes.default.object }), hasChildGroups: _propTypes.default.bool, inline: _propTypes.default.bool, name: _propTypes.default.string, readOnly: _propTypes.default.bool, recordType: _propTypes.default.string, rootPath: _propTypes.default.string, showInlineParens: _propTypes.default.bool, showMiniButtonFooter: _propTypes.default.bool, showRemoveButton: _propTypes.default.bool, isNewSearchForm: _propTypes.default.bool, getSearchConditionInputComponent: _propTypes.default.func.isRequired, onCommit: _propTypes.default.func, onRemove: _propTypes.default.func }; const defaultProps = { showInlineParens: true, showRemoveButton: true, showMiniButtonFooter: true }; const { MiniButton } = _cspaceInput.baseComponents; const messages = { [_searchOperators.OP_AND]: (0, _reactIntl.defineMessages)({ label: { "id": "booleanConditionInput.and.label", "defaultMessage": "and" }, opSelectorLabel: { "id": "booleanConditionInput.and.opSelectorLabel", "defaultMessage": "All" } }), [_searchOperators.OP_OR]: (0, _reactIntl.defineMessages)({ label: { "id": "booleanConditionInput.or.label", "defaultMessage": "or" }, opSelectorLabel: { "id": "booleanConditionInput.or.opSelectorLabel", "defaultMessage": "Any" } }), opSelector: (0, _reactIntl.defineMessages)({ label: { "id": "booleanConditionInput.opSelector.label", "defaultMessage": "{opSelectorInput} of the following conditions {operator, select, and {must} or {may}} be satisfied:" } }), addBoolean: (0, _reactIntl.defineMessages)({ label: { "id": "booleanConditionInput.addBoolean.label", "defaultMessage": "+ Any/All" } }), addField: (0, _reactIntl.defineMessages)({ label: { "id": "booleanConditionInput.addField.label", "defaultMessage": "+ Field" } }), addGroup: (0, _reactIntl.defineMessages)({ label: { "id": "booleanConditionInput.addGroup.label", "defaultMessage": "+ Group" } }) }; class BooleanConditionInput extends _react.Component { constructor() { super(); this.handleAddBooleanButtonClick = this.handleAddBooleanButtonClick.bind(this); this.handleAddFieldButtonClick = this.handleAddFieldButtonClick.bind(this); this.handleAddGroupButtonClick = this.handleAddGroupButtonClick.bind(this); this.handleChildConditionCommit = this.handleChildConditionCommit.bind(this); this.handleChildConditionRemove = this.handleChildConditionRemove.bind(this); this.handleOpSelectorCommit = this.handleOpSelectorCommit.bind(this); this.handleRef = this.handleRef.bind(this); this.handleRemoveButtonClick = this.handleRemoveButtonClick.bind(this); } componentDidMount() { const { condition } = this.props; const value = condition.get('value'); if (value === null) { // The condition was just added, and the operator needs to be selected. Focus it. if (this.domNode) { const input = this.domNode.querySelector('input[data-name="booleanSearchOp"]'); if (input) { input.focus(); } } } } handleAddBooleanButtonClick() { const { condition, name, onCommit } = this.props; if (onCommit) { const op = condition.get('op'); const value = condition.get('value') || _immutable.default.List(); // For the new nested boolean, set the operator to the opposite of the current operator, // since this is likely what the user wants. const nestedOp = op === _searchOperators.OP_AND ? _searchOperators.OP_OR : _searchOperators.OP_AND; const newCondition = _immutable.default.Map({ op: nestedOp, path: null, value: null }); onCommit(name, condition.set('value', value.push(newCondition))); } } handleAddFieldButtonClick() { const { condition, name, onCommit } = this.props; if (onCommit) { const value = condition.get('value') || _immutable.default.List(); onCommit(name, condition.set('value', value.push(_immutable.default.Map({ path: null })))); } } handleAddGroupButtonClick() { const { condition, name, onCommit } = this.props; if (onCommit) { const value = condition.get('value') || _immutable.default.List(); onCommit(name, condition.set('value', value.push(_immutable.default.Map({ op: _searchOperators.OP_GROUP, path: null })))); } } handleOpSelectorCommit(path, value) { const { condition, name, onCommit } = this.props; if (onCommit) { onCommit(name, condition.set('op', value)); } } handleChildConditionCommit(childName, childCondition) { const { condition, name, onCommit } = this.props; if (onCommit) { const index = parseInt(childName, 10); onCommit(name, condition.setIn(['value', index], childCondition)); } } handleChildConditionRemove(childName) { const { condition, name, onCommit } = this.props; if (onCommit) { const index = parseInt(childName, 10); onCommit(name, condition.deleteIn(['value', index])); } } handleRef(ref) { this.domNode = ref; } handleRemoveButtonClick() { const { name, onRemove } = this.props; if (onRemove) { onRemove(name); } } renderRemoveButton() { const { readOnly, showRemoveButton } = this.props; if (readOnly || !showRemoveButton) { return null; } return /*#__PURE__*/_react.default.createElement(_RemoveConditionButton.default, { onClick: this.handleRemoveButtonClick }); } renderHeader() { const { condition, readOnly } = this.props; if (readOnly) { return null; } const operator = condition.get('op'); const opSelectorInput = /*#__PURE__*/_react.default.createElement(_OptionPickerInput.default, { blankable: false, name: "booleanSearchOp", options: [{ value: _searchOperators.OP_OR, message: messages[_searchOperators.OP_OR].opSelectorLabel }, { value: _searchOperators.OP_AND, message: messages[_searchOperators.OP_AND].opSelectorLabel }], value: operator, onCommit: this.handleOpSelectorCommit }); return /*#__PURE__*/_react.default.createElement("header", null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.opSelector.label, { tagName: "div", values: { opSelectorInput, operator } })), this.renderRemoveButton()); } renderChildConditions() { const { condition, config, hasChildGroups, inline, readOnly, recordType, rootPath, getSearchConditionInputComponent, isNewSearchForm } = this.props; const operator = condition.get('op'); const childConditions = condition.get('value'); if (!childConditions || childConditions.size === 0) { return null; } const inputs = childConditions.map((childCondition, index) => { const SearchConditionInputComponent = getSearchConditionInputComponent(childCondition); const operatorLabel = index > 0 ? /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages[operator].label) : /*#__PURE__*/_react.default.createElement("span", null); return ( /*#__PURE__*/ // eslint-disable-next-line react/no-array-index-key _react.default.createElement("li", { key: index }, operatorLabel, inline ? ' ' : null, /*#__PURE__*/_react.default.createElement(SearchConditionInputComponent, { condition: childCondition, config: config, hasChildGroups: hasChildGroups, index: index, inline: inline, name: `${index}`, readOnly: readOnly, recordType: recordType, rootPath: rootPath, getSearchConditionInputComponent: getSearchConditionInputComponent, isNewSearchForm: isNewSearchForm, onCommit: this.handleChildConditionCommit, onRemove: this.handleChildConditionRemove }), inline ? ' ' : null) ); }); return /*#__PURE__*/_react.default.createElement("ul", null, inputs); } renderFooter() { const { condition, hasChildGroups, readOnly } = this.props; if (readOnly) { return null; } const operator = condition.get('op'); let addGroupButton; if (hasChildGroups) { addGroupButton = /*#__PURE__*/_react.default.createElement(MiniButton, { autoWidth: true, name: "addGroup", onClick: this.handleAddGroupButtonClick }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.addGroup.label)); } return /*#__PURE__*/_react.default.createElement("footer", null, /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages[operator].label)), /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(MiniButton, { autoWidth: true, name: "addField", onClick: this.handleAddFieldButtonClick }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.addField.label)), addGroupButton, /*#__PURE__*/_react.default.createElement(MiniButton, { autoWidth: true, name: "addBoolean", onClick: this.handleAddBooleanButtonClick }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.addBoolean.label)))); } render() { const { inline, showInlineParens, showMiniButtonFooter } = this.props; let openParen; let closeParen; if (inline && showInlineParens) { openParen = /*#__PURE__*/_react.default.createElement("div", null, "("); closeParen = /*#__PURE__*/_react.default.createElement("div", null, ")"); } const classNames = `${inline ? _BooleanConditionInput.default.inline : _BooleanConditionInput.default.normal} ${!showMiniButtonFooter ? _BooleanConditionInput.default.withoutMiniButtonFooter : null}`; return /*#__PURE__*/_react.default.createElement("div", { className: classNames, ref: this.handleRef }, this.renderHeader(), openParen, this.renderChildConditions(), closeParen, showMiniButtonFooter && this.renderFooter()); } } exports.default = BooleanConditionInput; BooleanConditionInput.propTypes = propTypes; BooleanConditionInput.defaultProps = defaultProps;