UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

282 lines (249 loc) 9.18 kB
import _extends from "@babel/runtime/helpers/extends"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _createSuper(Derived) { function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } return function () { var Super = _getPrototypeOf(Derived), result; if (isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } import React, { Component } from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; import { EditorCore } from 'rc-editor-core'; import { CompositeDecorator, ContentState, EditorState, SelectionState } from 'draft-js'; import createMention from '../utils/createMention'; import exportContent from '../utils/exportContent'; var Mention = /*#__PURE__*/ function (_Component) { _inherits(Mention, _Component); var _super = _createSuper(Mention); function Mention(props) { var _this; _classCallCheck(this, Mention); _this = _super.call(this, props); _defineProperty(_assertThisInitialized(_this), "onEditorChange", function (editorState) { var selection = editorState.getSelection(); _this._decorator = editorState.getDecorator(); var content = editorState.getCurrentContent(); if (_this.props.onChange) { _this.setState({ selection: selection }, function () { _this.props.onChange(content, exportContent(content)); }); } else { _this.setState({ editorState: editorState, selection: selection }); } }); _defineProperty(_assertThisInitialized(_this), "onFocus", function (e) { if (_this.props.onFocus) { _this.props.onFocus(e); } _this.setState({ focused: true }); }); _defineProperty(_assertThisInitialized(_this), "onBlur", function (e) { if (_this.props.onBlur) { _this.props.onBlur(e); } _this.setState({ focused: false }); }); _defineProperty(_assertThisInitialized(_this), "reset", function () { /*eslint-disable*/ _this._editor.Reset(); /*eslint-enable*/ }); _this.mention = createMention({ prefix: _this.getPrefix(props), tag: props.tag, mode: props.mode, mentionStyle: props.mentionStyle }); _this.Suggestions = _this.mention.Suggestions; _this.plugins = [_this.mention]; _this.state = { suggestions: props.suggestions, value: props.value && EditorState.createWithContent(props.value, new CompositeDecorator(_this.mention.decorators)), selection: SelectionState.createEmpty(), focused: false }; if (typeof props.defaultValue === 'string') { // eslint-disable-next-line console.warn('The property `defaultValue` now allow `EditorState` only, see http://react-component.github.io/editor-mention/examples/defaultValue.html '); } if (props.value !== undefined) { _this.controlledMode = true; } return _this; } _createClass(Mention, [{ key: "componentWillReceiveProps", value: function componentWillReceiveProps(nextProps) { var suggestions = nextProps.suggestions; var selection = this.state.selection; var value = nextProps.value; if (value && selection) { value = EditorState.acceptSelection(EditorState.createWithContent(value, this._decorator), selection); } this.setState({ suggestions: suggestions, value: value }); } }, { key: "getPrefix", value: function getPrefix() { var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.props; return Array.isArray(props.prefix) ? props.prefix : [props.prefix]; } }, { key: "getUnderLine", value: function getUnderLine() { var prefixCls = this.props.prefixCls; return React.createElement("div", { className: "".concat(prefixCls, "-editor-underline") }, React.createElement("span", { className: "".concat(prefixCls, "-editor-ripple") })); } }, { key: "render", value: function render() { var _classnames, _this2 = this; var _this$props = this.props, prefixCls = _this$props.prefixCls, style = _this$props.style, tag = _this$props.tag, multiLines = _this$props.multiLines, suggestionStyle = _this$props.suggestionStyle, placeholder = _this$props.placeholder, defaultValue = _this$props.defaultValue, className = _this$props.className, notFoundContent = _this$props.notFoundContent, getSuggestionContainer = _this$props.getSuggestionContainer, readOnly = _this$props.readOnly, disabled = _this$props.disabled, placement = _this$props.placement; var _this$state = this.state, suggestions = _this$state.suggestions, focused = _this$state.focused; var Suggestions = this.Suggestions; var editorClass = classnames(className, (_classnames = {}, _defineProperty(_classnames, "".concat(prefixCls, "-wrapper"), true), _defineProperty(_classnames, "".concat(prefixCls, "-editor-focus"), focused), _defineProperty(_classnames, "readonly", readOnly), _defineProperty(_classnames, "disabled", disabled), _defineProperty(_classnames, "multilines", multiLines), _classnames)); var editorProps = this.controlledMode ? { value: this.state.value } : {}; var defaultValueState = defaultValue && EditorState.createWithContent(typeof defaultValue === 'string' ? ContentState.createFromText(defaultValue) : defaultValue, this._decorator); return React.createElement("div", { className: editorClass, style: style, ref: function ref(wrapper) { return _this2._wrapper = wrapper; } }, React.createElement(EditorCore, _extends({ ref: function ref(editor) { return _this2._editor = editor; }, prefixCls: prefixCls, style: style, multiLines: multiLines, plugins: this.plugins, defaultValue: defaultValueState, placeholder: placeholder, onFocus: this.onFocus, onBlur: this.onBlur, onChange: this.onEditorChange }, editorProps, { readOnly: readOnly || disabled }), React.createElement(Suggestions, { mode: tag ? 'immutable' : 'mutable', prefix: this.getPrefix(), prefixCls: prefixCls, style: suggestionStyle, placement: placement, notFoundContent: notFoundContent, suggestions: suggestions, getSuggestionContainer: getSuggestionContainer ? function () { return getSuggestionContainer(_this2._wrapper); } : null, onSearchChange: this.props.onSearchChange, onSelect: this.props.onSelect, noRedup: this.props.noRedup })), this.getUnderLine()); } }]); return Mention; }(Component); _defineProperty(Mention, "propTypes", { value: PropTypes.object, suggestions: PropTypes.array, prefix: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]), prefixCls: PropTypes.string, tag: PropTypes.element, style: PropTypes.object, className: PropTypes.string, onSearchChange: PropTypes.func, onChange: PropTypes.func, mode: PropTypes.string, multiLines: PropTypes.bool, suggestionStyle: PropTypes.object, placeholder: PropTypes.string, defaultValue: PropTypes.object, notFoundContent: PropTypes.any, position: PropTypes.string, onFocus: PropTypes.func, onBlur: PropTypes.func, onSelect: PropTypes.func, getSuggestionContainer: PropTypes.func, noRedup: PropTypes.bool, mentionStyle: PropTypes.object, placement: PropTypes.string }); _defineProperty(Mention, "controlledMode", false); Mention.defaultProps = { prefixCls: 'rc-editor-mention', prefix: '@', mode: 'immutable', suggestions: [], multiLines: false, className: '', suggestionStyle: {}, notFoundContent: '无法找到', position: 'absolute', placement: 'bottom', // top, bottom mentionStyle: {} }; export default Mention; //# sourceMappingURL=Mention.react.js.map