client/entity-editor/common/language-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.slice.js");
require("core-js/modules/es.array.sort.js");
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.promise.js");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _isNumber2 = _interopRequireDefault(require("lodash/isNumber"));
var React = _interopRequireWildcard(require("react"));
var _reactBootstrap = require("react-bootstrap");
var _async = _interopRequireDefault(require("react-select/async"));
var _reactFontawesome = require("@fortawesome/react-fontawesome");
var _validationLabel = _interopRequireDefault(require("./validation-label"));
var _reactSelect = require("react-select");
var _utils = require("../../helpers/utils");
var _reactSelectFastFilterOptions = _interopRequireDefault(require("react-select-fast-filter-options"));
var _freeSolidSvgIcons = require("@fortawesome/free-solid-svg-icons");
var _freezedObjects = require("../../unified-form/common/freezed-objects");
var _excluded = ["empty", "error", "tooltipText"];
/*
 * Copyright (C) 2016  Ben Ockmore
 *
 * 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; }
function OptimizedOption(props) {
  delete props.innerProps.onMouseMove;
  delete props.innerProps.onMouseOver;
  return /*#__PURE__*/React.createElement(_reactSelect.components.Option, props, props.children);
}
/**
 * Presentational component. This component renders a dropdown selection box
 * allowing the user to select from a list of provided language options. The
 * input is labelled with a ValidationLabel containing the text 'Language'.
 *
 * @param {Object} props - The properties passed to the component, which are
 *        then passed to the underlying VirtualizedSelect component.
 * @param {boolean} props.error - Passed to the ValidationLabel within the
 *        component to indicate a validation error.
 * @param {boolean} props.empty - Passed to the ValidationLabel within the
 *        component to indicate that the field is empty.
 * @returns {Object} A React component containing the rendered input.
 */
function LanguageField(_ref) {
  var _freezeObjects$filter;
  var empty = _ref.empty,
    error = _ref.error,
    tooltipText = _ref.tooltipText,
    rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var label = /*#__PURE__*/React.createElement(_validationLabel.default, {
    empty: empty,
    error: error
  }, "Language");
  var MAX_DROPDOWN_OPTIONS = 20;
  var MAX_F1_OPTIONS = 400;
  var tooltip = /*#__PURE__*/React.createElement(_reactBootstrap.Tooltip, {
    id: "language-tooltip"
  }, tooltipText);
  rest.options = (0, _utils.convertMapToObject)(rest.options);
  var value = rest.value,
    options = rest.options;
  var filterOptions = (_freezeObjects$filter = _freezedObjects.freezeObjects.filterOptions) !== null && _freezeObjects$filter !== void 0 ? _freezeObjects$filter : React.useMemo(function () {
    return (0, _reactSelectFastFilterOptions.default)({
      options: options
    });
  }, []);
  var sortFilterOptions = function sortFilterOptions(opts, input, selectOptions) {
    var newOptions = filterOptions(opts, input, selectOptions).slice(0, MAX_DROPDOWN_OPTIONS);
    var sortLang = function sortLang(a, b) {
      if ((0, _isNumber2.default)(a.frequency) && (0, _isNumber2.default)(b.frequency) && a.frequency !== b.frequency) {
        return b.frequency - a.frequency;
      }
      return a.label.localeCompare(b.label);
    };
    newOptions.sort(sortLang);
    return newOptions;
  };
  var f2Languages = options.filter(function (lang) {
    return lang.frequency === 2;
  });
  var f1Languages = options.filter(function (lang) {
    return lang.frequency === 1;
  }).slice(0, MAX_F1_OPTIONS);
  var defaultOptions = [{
    label: 'Frequently Used',
    options: f2Languages
  }, {
    label: 'Other',
    options: f1Languages
  }];
  var fetchOptions = React.useCallback(function (input) {
    return Promise.resolve(sortFilterOptions(options, input, value));
  }, []);
  return /*#__PURE__*/React.createElement(_reactBootstrap.Form.Group, null, /*#__PURE__*/React.createElement(_reactBootstrap.Form.Label, null, label, /*#__PURE__*/React.createElement(_reactBootstrap.OverlayTrigger, {
    delay: 50,
    overlay: tooltip
  }, /*#__PURE__*/React.createElement(_reactFontawesome.FontAwesomeIcon, {
    className: "margin-left-0-5",
    icon: _freeSolidSvgIcons.faQuestionCircle
  }))), /*#__PURE__*/React.createElement(_async.default, (0, _extends2.default)({
    cacheOptions: true,
    isClearable: true,
    className: "Select",
    classNamePrefix: "react-select",
    components: {
      Option: OptimizedOption
    },
    defaultOptions: defaultOptions,
    loadOptions: fetchOptions,
    placeholder: "Search language"
  }, rest)));
}
LanguageField.displayName = 'LanguageField';
LanguageField.defaultProps = {
  empty: false,
  error: false,
  tooltipText: null
};
function areEqual(prevProps, nextProps) {
  return JSON.stringify(prevProps) === JSON.stringify(nextProps);
}
var _default = /*#__PURE__*/React.memo(LanguageField, areEqual);
exports.default = _default;
//# sourceMappingURL=language-field.js.map