client/entity-editor/common/merge-field.js

"use strict";

require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/es.weak-map.js");
require("core-js/modules/web.dom-collections.iterator.js");
require("core-js/modules/es.object.get-own-property-descriptor.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.object.to-string.js");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _isEqual2 = _interopRequireDefault(require("lodash/isEqual"));
var _has2 = _interopRequireDefault(require("lodash/has"));
var _get3 = _interopRequireDefault(require("lodash/get"));
var React = _interopRequireWildcard(require("react"));
var _reactBootstrap = require("react-bootstrap");
var _reactFontawesome = require("@fortawesome/react-fontawesome");
var _reactSelect = _interopRequireDefault(require("react-select"));
var _validationLabel = _interopRequireDefault(require("../common/validation-label"));
var _freeSolidSvgIcons = require("@fortawesome/free-solid-svg-icons");
var _excluded = ["error", "options", "label", "currentValue", "tooltipText", "onChange", "valueProperty"];
/*
 * Copyright (C) 2019  Nicolas Pelletier
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/**
 * Presentational component. This component renders a plain text input and a
 * ValidationLabel for a field labelled 'Name'.
 *
 * @param {Object} props - The properties passed to the component.
 * @param {boolean} props.error - Passed to the ValidationLabel within the
 *        component to indicate a validation error.
 * @param {Array} props.options - Options to select from for this field
 * @param {string} props.label - The text for the ValidationLabel component
 * @param {any} props.currentValue - The currently selected value bubbled up from the state
 * @param {Function} props.onChange - Function to be called when the value in
 *        the wrapped input changes.
 * @returns {Object} a React component containing the rendered input
 */
function MergeField(_ref) {
  var error = _ref.error,
    options = _ref.options,
    label = _ref.label,
    currentValue = _ref.currentValue,
    tooltipText = _ref.tooltipText,
    onChange = _ref.onChange,
    valueProperty = _ref.valueProperty,
    rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var labelComponent = /*#__PURE__*/React.createElement(_validationLabel.default, {
    error: error
  }, label);
  var helpIconElement = tooltipText && /*#__PURE__*/React.createElement(_reactBootstrap.OverlayTrigger, {
    delay: 50,
    overlay: /*#__PURE__*/React.createElement(_reactBootstrap.Tooltip, null, tooltipText)
  }, /*#__PURE__*/React.createElement(_reactFontawesome.FontAwesomeIcon, {
    className: "margin-left-0-5",
    icon: _freeSolidSvgIcons.faQuestionCircle
  }));
  if (options.length <= 1) {
    var value = (0, _get3.default)(options, "[0][".concat(valueProperty, "]"), '');
    return /*#__PURE__*/React.createElement(_reactBootstrap.Form.Group, null, /*#__PURE__*/React.createElement(_reactBootstrap.Form.Label, null, labelComponent, helpIconElement), /*#__PURE__*/React.createElement(_reactBootstrap.Form.Control, (0, _extends2.default)({
      readOnly: true,
      value: value
    }, rest)));
  }
  var onChangeReturnValue = function onChangeReturnValue(selectObject) {
    var value;
    if ((0, _has2.default)(selectObject, 'value') && (0, _has2.default)(selectObject, 'label')) {
      value = selectObject.value;
    } else {
      value = selectObject;
    }
    return onChange(value);
  };
  var currentOption = options.filter(function (el) {
    return (0, _isEqual2.default)(el.value, currentValue);
  });
  return /*#__PURE__*/React.createElement(_reactBootstrap.Form.Group, null, /*#__PURE__*/React.createElement(_reactBootstrap.Form.Label, null, labelComponent, helpIconElement), /*#__PURE__*/React.createElement(_reactSelect.default, (0, _extends2.default)({}, rest, {
    classNamePrefix: "react-select",
    instanceId: label,
    isClearable: false,
    options: options,
    value: currentOption
    // eslint-disable-next-line react/jsx-no-bind
    ,
    onChange: onChangeReturnValue
  })));
}
MergeField.displayName = 'MergeField';
MergeField.defaultProps = {
  error: false,
  tooltipText: null,
  valueProperty: 'label'
};
var _default = MergeField;
exports.default = _default;
//# sourceMappingURL=merge-field.js.map