UNPKG

zarm-web

Version:
271 lines (220 loc) 11.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireDefault(require("react")); var _classnames = _interopRequireDefault(require("classnames")); var _debounce = _interopRequireDefault(require("../utils/debounce")); var _tag = _interopRequireDefault(require("../tag")); var _icon = _interopRequireDefault(require("../icon")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } var sizeValue = { xs: 24, sm: 28, lg: 36, xl: 40 }; var Style = { tagStyle: { maxWidth: 100 }, iconStyle: { fontSize: 'initial' } }; var InputWithTags = /*#__PURE__*/ function (_React$Component) { _inherits(InputWithTags, _React$Component); function InputWithTags(props) { var _this; _classCallCheck(this, InputWithTags); _this = _possibleConstructorReturn(this, _getPrototypeOf(InputWithTags).call(this, props)); _this.inputDiv = void 0; _this.tagListBox = void 0; _this.isComposition = void 0; _this.debouncedOnInputChange = void 0; _this.state = { isFocus: false, compositionData: null }; _this.onInput = function (value) { if (_this.props.disabled || _this.isComposition) { return; } if (typeof _this.props.onSearchChange === 'function') { _this.props.onSearchChange(value); } }; _this.onFocus = function () { _this.setState({ isFocus: true }); }; _this.onBlur = function () { _this.setState({ isFocus: false }); }; _this.tagListBoxref = function (e) { _this.tagListBox = e; }; _this.onTagBoxClick = function () { var _this$props = _this.props, active = _this$props.active, search = _this$props.search, remoteSearch = _this$props.remoteSearch, value = _this$props.value; if (active && (search || remoteSearch) && Array.isArray(value)) { _this.inputDiv.focus(); } }; _this.onCompositionStart = function () { _this.isComposition = true; }; _this.onCompositionUpdate = function (e) { _this.setState({ compositionData: e.data }); }; _this.onCompositionEnd = function (value) { _this.isComposition = false; _this.setState({ compositionData: null }); _this.onInput(value); }; _this.debouncedOnInputChange = (0, _debounce.default)(_this.onInput, 300, false); return _this; } _createClass(InputWithTags, [{ key: "componentWillReceiveProps", value: function componentWillReceiveProps(nextProps) { var active = this.props.active; if (nextProps.active !== active) { // work without disabled and search prop; if (!nextProps.disabled && (nextProps.search || nextProps.remoteSearch)) { if (nextProps.active) { this.inputDiv.focus(); } else { this.inputDiv.innerText = ''; } } } } }, { key: "render", value: function render() { var _this2 = this; var _this$props2 = this.props, search = _this$props2.search, remoteSearch = _this$props2.remoteSearch, value = _this$props2.value, searchValue = _this$props2.searchValue, placeholder = _this$props2.placeholder, active = _this$props2.active, onDeleteTag = _this$props2.onDeleteTag, onSearchChange = _this$props2.onSearchChange, size = _this$props2.size, radius = _this$props2.radius, disabled = _this$props2.disabled, others = _objectWithoutProperties(_this$props2, ["search", "remoteSearch", "value", "searchValue", "placeholder", "active", "onDeleteTag", "onSearchChange", "size", "radius", "disabled"]); var _this$state = this.state, compositionData = _this$state.compositionData, isFocus = _this$state.isFocus; var showPlaceHolder = false; if ((search || remoteSearch) && !isFocus && value === null || typeof value === 'string' && value.length === 0 || // eslint-disable-next-line no-mixed-operators !value // eslint-disable-next-line no-mixed-operators && !compositionData) { showPlaceHolder = true; } var searchValueStyle = { display: isFocus && searchValue ? 'none' : 'inline-block' }; var tagSizeHeight = (size ? sizeValue[size] : 32) - 10; var tagList; // if value is array, make a Tag list; if (Array.isArray(value)) { tagList = value.map(function (elem, index) { return _react.default.createElement("div", { className: "za-tag-list-box", key: elem.key, ref: _this2.tagListBoxref }, _react.default.createElement(_tag.default, { style: _objectSpread({}, Style.tagStyle, { height: tagSizeHeight, lineHeight: "".concat(tagSizeHeight, "px") }), key: elem.key, closable: true, onClick: function onClick() {}, onClose: function onClose(e) { e.stopPropagation(); if (typeof onDeleteTag === 'function') { setTimeout(function () { onDeleteTag(e, elem.key, elem.value, index); }); } } }, elem.value)); }); } else { tagList = _react.default.createElement("div", { style: searchValueStyle, className: "value-text" }, compositionData || value); } var boxCls = (0, _classnames.default)(_defineProperty({ 'za-tag-input-box': true, 'is-radius': radius, 'za-tag-input-box--active': active, 'is-disabled': disabled }, "size-".concat(size), !!size)); return _react.default.createElement("div", _extends({ className: boxCls, onClick: this.onTagBoxClick }, others), tagList, (search || remoteSearch) && _react.default.createElement("div", { className: "za-tag-input__div", contentEditable: !disabled && (search || remoteSearch), onInput: function onInput(e) { _this2.debouncedOnInputChange(e.target.textContent); }, onFocus: this.onFocus, onBlur: this.onBlur, onCompositionStart: this.onCompositionStart, onCompositionUpdate: this.onCompositionUpdate, onCompositionEnd: function onCompositionEnd(e) { _this2.onCompositionEnd(e.target.textContent); }, ref: function ref(e) { _this2.inputDiv = e; } }), showPlaceHolder && _react.default.createElement("span", { style: searchValueStyle, className: "za-tag-input__div-placeholder" }, placeholder), _react.default.createElement(_icon.default, { style: Style.iconStyle, className: "arrow-bottom", type: "arrow-bottom" })); } }]); return InputWithTags; }(_react.default.Component); var _default = InputWithTags; exports.default = _default;