UNPKG

qwc2

Version:
195 lines (194 loc) 10.6 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), 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 _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /** * Copyright 2024 Sourcepole AG * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. */ import React from 'react'; import PropTypes from 'prop-types'; import { KeyValCache } from '../utils/EditingUtils'; import LocaleUtils from '../utils/LocaleUtils'; import './style/EditComboField.css'; var EditComboField = /*#__PURE__*/function (_React$Component) { function EditComboField() { var _this; _classCallCheck(this, EditComboField); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _callSuper(this, EditComboField, [].concat(args)); _defineProperty(_this, "state", { showPlaceholder: true, values: [] }); _defineProperty(_this, "hasEmptyValue", function (values) { for (var i = 0; i < values.length; ++i) { if (typeof values[i] === 'string') { if (values[i] === '') { return true; } } else if (values[i].value === '') { return true; } } return false; }); _defineProperty(_this, "renderMultiSelect", function () { var items = new Set(); try { items = new Set(JSON.parse('[' + _this.props.value.slice(1, -1) + ']')); } catch (e) { // pass } var serializeValue = function serializeValue(value, enabled) { if (enabled) { return '{' + JSON.stringify(_toConsumableArray(items).concat([value])).slice(1, -1) + '}'; } else { return '{' + JSON.stringify(_toConsumableArray(items).filter(function (x) { return x !== value; })).slice(1, -1) + '}'; } }; return /*#__PURE__*/React.createElement("div", { className: "edit-multi-select" }, _this.state.values.map(function (item, index) { var _this$itemValueLabel = _this.itemValueLabel(item), value = _this$itemValueLabel.value, label = _this$itemValueLabel.label; return /*#__PURE__*/React.createElement("div", { key: _this.props.fieldId + index }, /*#__PURE__*/React.createElement("label", null, /*#__PURE__*/React.createElement("input", { checked: items.has(value), disabled: _this.props.readOnly, onChange: function onChange(ev) { return _this.props.updateField(_this.props.fieldId, serializeValue(value, ev.target.checked)); }, type: "checkbox" }), label)); })); }); _defineProperty(_this, "renderComboSelect", function () { var _this$props$placehold; return /*#__PURE__*/React.createElement("select", { disabled: _this.props.readOnly, name: _this.props.name, onChange: function onChange(ev) { return _this.props.updateField(_this.props.fieldId, ev.target.selectedIndex === 0 && _this.state.showPlaceholder ? null : ev.target.value); }, required: _this.props.required, style: _this.props.style, value: String(_this.props.value) }, _this.state.showPlaceholder ? /*#__PURE__*/React.createElement("option", { disabled: _this.props.required, value: "" }, (_this$props$placehold = _this.props.placeholder) !== null && _this$props$placehold !== void 0 ? _this$props$placehold : LocaleUtils.tr("editing.select")) : null, _this.state.values.map(function (item, index) { var _this$itemValueLabel2 = _this.itemValueLabel(item), value = _this$itemValueLabel2.value, label = _this$itemValueLabel2.label; return /*#__PURE__*/React.createElement("option", { key: _this.props.fieldId + index, value: String(value) }, label); })); }); _defineProperty(_this, "itemValueLabel", function (item) { var value = ""; var label = ""; if (typeof item === 'string') { value = label = item; } else { value = item.value; label = item.label; } return { value: value, label: label }; }); return _this; } _inherits(EditComboField, _React$Component); return _createClass(EditComboField, [{ key: "componentDidMount", value: function componentDidMount() { var _this2 = this; if (this.props.values) { // eslint-disable-next-line this.setState({ values: this.props.values, showPlaceholder: !this.hasEmptyValue(this.props.values) }); } else if (this.props.keyvalrel) { var _this$props$filterExp; KeyValCache.get(this.props.editIface, this.props.keyvalrel, (_this$props$filterExp = this.props.filterExpr) !== null && _this$props$filterExp !== void 0 ? _this$props$filterExp : null).then(function (values) { // eslint-disable-next-line _this2.setState({ values: values, showPlaceholder: !_this2.hasEmptyValue(values) }); }); } } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { var _this3 = this; if (this.props.keyvalrel && this.props.filterExpr !== prevProps.filterExpr) { var _this$props$filterExp2; KeyValCache.get(this.props.editIface, this.props.keyvalrel, (_this$props$filterExp2 = this.props.filterExpr) !== null && _this$props$filterExp2 !== void 0 ? _this$props$filterExp2 : null).then(function (values) { // eslint-disable-next-line _this3.setState({ values: values, showPlaceholder: !_this3.hasEmptyValue(values) }); }); } } }, { key: "render", value: function render() { if (this.props.multiSelect) { return this.renderMultiSelect(); } else { return this.renderComboSelect(); } } }]); }(React.Component); _defineProperty(EditComboField, "propTypes", { editIface: PropTypes.object, fieldId: PropTypes.string, filterExpr: PropTypes.array, keyvalrel: PropTypes.string, multiSelect: PropTypes.bool, name: PropTypes.string, placeholder: PropTypes.string, readOnly: PropTypes.bool, required: PropTypes.bool, style: PropTypes.object, updateField: PropTypes.func, value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), values: PropTypes.array }); export { EditComboField as default };