1 | "use strict";
|
2 |
|
3 | var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
|
4 |
|
5 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
6 |
|
7 | Object.defineProperty(exports, "__esModule", {
|
8 | value: true
|
9 | });
|
10 | exports.default = void 0;
|
11 |
|
12 | var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
13 |
|
14 | var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
15 |
|
16 | var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
17 |
|
18 | var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
19 |
|
20 | var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
21 |
|
22 | var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
|
23 |
|
24 | var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
|
25 |
|
26 | var _classnames = _interopRequireDefault(require("classnames"));
|
27 |
|
28 | var _toArray = _interopRequireDefault(require("rc-util/lib/Children/toArray"));
|
29 |
|
30 | var _KeyCode = _interopRequireDefault(require("rc-util/lib/KeyCode"));
|
31 |
|
32 | var React = _interopRequireWildcard(require("react"));
|
33 |
|
34 | var _rcTextarea = _interopRequireDefault(require("rc-textarea"));
|
35 |
|
36 | var _KeywordTrigger = _interopRequireDefault(require("./KeywordTrigger"));
|
37 |
|
38 | var _MentionsContext = require("./MentionsContext");
|
39 |
|
40 | var _Option = _interopRequireDefault(require("./Option"));
|
41 |
|
42 | var _util = require("./util");
|
43 |
|
44 | var Mentions = function (_React$Component) {
|
45 | (0, _inherits2.default)(Mentions, _React$Component);
|
46 |
|
47 | var _super = (0, _createSuper2.default)(Mentions);
|
48 |
|
49 | function Mentions(props) {
|
50 | var _this;
|
51 |
|
52 | (0, _classCallCheck2.default)(this, Mentions);
|
53 | _this = _super.call(this, props);
|
54 | _this.focusId = undefined;
|
55 |
|
56 | _this.triggerChange = function (value) {
|
57 | var onChange = _this.props.onChange;
|
58 |
|
59 | if (!('value' in _this.props)) {
|
60 | _this.setState({
|
61 | value: value
|
62 | });
|
63 | }
|
64 |
|
65 | if (onChange) {
|
66 | onChange(value);
|
67 | }
|
68 | };
|
69 |
|
70 | _this.onChange = function (_ref) {
|
71 | var value = _ref.target.value;
|
72 |
|
73 | _this.triggerChange(value);
|
74 | };
|
75 |
|
76 |
|
77 | _this.onKeyDown = function (event) {
|
78 | var which = event.which;
|
79 | var _this$state = _this.state,
|
80 | activeIndex = _this$state.activeIndex,
|
81 | measuring = _this$state.measuring;
|
82 |
|
83 | if (!measuring) {
|
84 | return;
|
85 | }
|
86 |
|
87 | if (which === _KeyCode.default.UP || which === _KeyCode.default.DOWN) {
|
88 |
|
89 | var optionLen = _this.getOptions().length;
|
90 |
|
91 | var offset = which === _KeyCode.default.UP ? -1 : 1;
|
92 | var newActiveIndex = (activeIndex + offset + optionLen) % optionLen;
|
93 |
|
94 | _this.setState({
|
95 | activeIndex: newActiveIndex
|
96 | });
|
97 |
|
98 | event.preventDefault();
|
99 | } else if (which === _KeyCode.default.ESC) {
|
100 | _this.stopMeasure();
|
101 | } else if (which === _KeyCode.default.ENTER) {
|
102 |
|
103 | event.preventDefault();
|
104 |
|
105 | var options = _this.getOptions();
|
106 |
|
107 | if (!options.length) {
|
108 | _this.stopMeasure();
|
109 |
|
110 | return;
|
111 | }
|
112 |
|
113 | var option = options[activeIndex];
|
114 |
|
115 | _this.selectOption(option);
|
116 | }
|
117 | };
|
118 | |
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | _this.onKeyUp = function (event) {
|
133 | var key = event.key,
|
134 | which = event.which;
|
135 | var _this$state2 = _this.state,
|
136 | prevMeasureText = _this$state2.measureText,
|
137 | measuring = _this$state2.measuring;
|
138 | var _this$props = _this.props,
|
139 | _this$props$prefix = _this$props.prefix,
|
140 | prefix = _this$props$prefix === void 0 ? '' : _this$props$prefix,
|
141 | onSearch = _this$props.onSearch,
|
142 | validateSearch = _this$props.validateSearch;
|
143 | var target = event.target;
|
144 | var selectionStartText = (0, _util.getBeforeSelectionText)(target);
|
145 |
|
146 | var _getLastMeasureIndex = (0, _util.getLastMeasureIndex)(selectionStartText, prefix),
|
147 | measureIndex = _getLastMeasureIndex.location,
|
148 | measurePrefix = _getLastMeasureIndex.prefix;
|
149 |
|
150 |
|
151 | if ([_KeyCode.default.ESC, _KeyCode.default.UP, _KeyCode.default.DOWN, _KeyCode.default.ENTER].indexOf(which) !== -1) {
|
152 | return;
|
153 | }
|
154 |
|
155 | if (measureIndex !== -1) {
|
156 | var measureText = selectionStartText.slice(measureIndex + measurePrefix.length);
|
157 | var validateMeasure = validateSearch(measureText, _this.props);
|
158 | var matchOption = !!_this.getOptions(measureText).length;
|
159 |
|
160 | if (validateMeasure) {
|
161 | if (key === measurePrefix || key === 'Shift' || measuring || measureText !== prevMeasureText && matchOption) {
|
162 | _this.startMeasure(measureText, measurePrefix, measureIndex);
|
163 | }
|
164 | } else if (measuring) {
|
165 |
|
166 | _this.stopMeasure();
|
167 | }
|
168 | |
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | if (onSearch && validateMeasure) {
|
175 | onSearch(measureText, measurePrefix);
|
176 | }
|
177 | } else if (measuring) {
|
178 | _this.stopMeasure();
|
179 | }
|
180 | };
|
181 |
|
182 | _this.onPressEnter = function (event) {
|
183 | var measuring = _this.state.measuring;
|
184 | var onPressEnter = _this.props.onPressEnter;
|
185 |
|
186 | if (!measuring && onPressEnter) {
|
187 | onPressEnter(event);
|
188 | }
|
189 | };
|
190 |
|
191 | _this.onInputFocus = function (event) {
|
192 | _this.onFocus(event);
|
193 | };
|
194 |
|
195 | _this.onInputBlur = function (event) {
|
196 | _this.onBlur(event);
|
197 | };
|
198 |
|
199 | _this.onDropdownFocus = function () {
|
200 | _this.onFocus();
|
201 | };
|
202 |
|
203 | _this.onDropdownBlur = function () {
|
204 | _this.onBlur();
|
205 | };
|
206 |
|
207 | _this.onFocus = function (event) {
|
208 | window.clearTimeout(_this.focusId);
|
209 | var isFocus = _this.state.isFocus;
|
210 | var onFocus = _this.props.onFocus;
|
211 |
|
212 | if (!isFocus && event && onFocus) {
|
213 | onFocus(event);
|
214 | }
|
215 |
|
216 | _this.setState({
|
217 | isFocus: true
|
218 | });
|
219 | };
|
220 |
|
221 | _this.onBlur = function (event) {
|
222 | _this.focusId = window.setTimeout(function () {
|
223 | var onBlur = _this.props.onBlur;
|
224 |
|
225 | _this.setState({
|
226 | isFocus: false
|
227 | });
|
228 |
|
229 | _this.stopMeasure();
|
230 |
|
231 | if (onBlur) {
|
232 | onBlur(event);
|
233 | }
|
234 | }, 0);
|
235 | };
|
236 |
|
237 | _this.selectOption = function (option) {
|
238 | var _this$state3 = _this.state,
|
239 | value = _this$state3.value,
|
240 | measureLocation = _this$state3.measureLocation,
|
241 | measurePrefix = _this$state3.measurePrefix;
|
242 | var _this$props2 = _this.props,
|
243 | split = _this$props2.split,
|
244 | onSelect = _this$props2.onSelect;
|
245 | var _option$value = option.value,
|
246 | mentionValue = _option$value === void 0 ? '' : _option$value;
|
247 |
|
248 | var _replaceWithMeasure = (0, _util.replaceWithMeasure)(value, {
|
249 | measureLocation: measureLocation,
|
250 | targetText: mentionValue,
|
251 | prefix: measurePrefix,
|
252 | selectionStart: _this.textarea.selectionStart,
|
253 | split: split
|
254 | }),
|
255 | text = _replaceWithMeasure.text,
|
256 | selectionLocation = _replaceWithMeasure.selectionLocation;
|
257 |
|
258 | _this.triggerChange(text);
|
259 |
|
260 | _this.stopMeasure(function () {
|
261 |
|
262 | (0, _util.setInputSelection)(_this.textarea, selectionLocation);
|
263 | });
|
264 |
|
265 | if (onSelect) {
|
266 | onSelect(option, measurePrefix);
|
267 | }
|
268 | };
|
269 |
|
270 | _this.setActiveIndex = function (activeIndex) {
|
271 | _this.setState({
|
272 | activeIndex: activeIndex
|
273 | });
|
274 | };
|
275 |
|
276 | _this.setTextAreaRef = function (element) {
|
277 | var _element$resizableTex;
|
278 |
|
279 | _this.textarea = element === null || element === void 0 ? void 0 : (_element$resizableTex = element.resizableTextArea) === null || _element$resizableTex === void 0 ? void 0 : _element$resizableTex.textArea;
|
280 | };
|
281 |
|
282 | _this.setMeasureRef = function (element) {
|
283 | _this.measure = element;
|
284 | };
|
285 |
|
286 | _this.getOptions = function (measureText) {
|
287 | var targetMeasureText = measureText || _this.state.measureText || '';
|
288 | var _this$props3 = _this.props,
|
289 | children = _this$props3.children,
|
290 | filterOption = _this$props3.filterOption;
|
291 | var list = (0, _toArray.default)(children).map(function (_ref2) {
|
292 | var props = _ref2.props,
|
293 | key = _ref2.key;
|
294 | return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, {
|
295 | key: key || props.value
|
296 | });
|
297 | }).filter(function (option) {
|
298 |
|
299 | if (filterOption === false) {
|
300 | return true;
|
301 | }
|
302 |
|
303 | return filterOption(targetMeasureText, option);
|
304 | });
|
305 | return list;
|
306 | };
|
307 |
|
308 | _this.state = {
|
309 | value: props.defaultValue || props.value || '',
|
310 | measuring: false,
|
311 | measureLocation: 0,
|
312 | measureText: null,
|
313 | measurePrefix: '',
|
314 | activeIndex: 0,
|
315 | isFocus: false
|
316 | };
|
317 | return _this;
|
318 | }
|
319 |
|
320 | (0, _createClass2.default)(Mentions, [{
|
321 | key: "componentDidUpdate",
|
322 | value: function componentDidUpdate() {
|
323 | var measuring = this.state.measuring;
|
324 |
|
325 | if (measuring) {
|
326 | this.measure.scrollTop = this.textarea.scrollTop;
|
327 | }
|
328 | }
|
329 | }, {
|
330 | key: "startMeasure",
|
331 | value: function startMeasure(measureText, measurePrefix, measureLocation) {
|
332 | this.setState({
|
333 | measuring: true,
|
334 | measureText: measureText,
|
335 | measurePrefix: measurePrefix,
|
336 | measureLocation: measureLocation,
|
337 | activeIndex: 0
|
338 | });
|
339 | }
|
340 | }, {
|
341 | key: "stopMeasure",
|
342 | value: function stopMeasure(callback) {
|
343 | this.setState({
|
344 | measuring: false,
|
345 | measureLocation: 0,
|
346 | measureText: null
|
347 | }, callback);
|
348 | }
|
349 | }, {
|
350 | key: "focus",
|
351 | value: function focus() {
|
352 | this.textarea.focus();
|
353 | }
|
354 | }, {
|
355 | key: "blur",
|
356 | value: function blur() {
|
357 | this.textarea.blur();
|
358 | }
|
359 | }, {
|
360 | key: "render",
|
361 | value: function render() {
|
362 | var _this$state4 = this.state,
|
363 | value = _this$state4.value,
|
364 | measureLocation = _this$state4.measureLocation,
|
365 | measurePrefix = _this$state4.measurePrefix,
|
366 | measuring = _this$state4.measuring,
|
367 | activeIndex = _this$state4.activeIndex;
|
368 | var _this$props4 = this.props,
|
369 | prefixCls = _this$props4.prefixCls,
|
370 | placement = _this$props4.placement,
|
371 | direction = _this$props4.direction,
|
372 | transitionName = _this$props4.transitionName,
|
373 | className = _this$props4.className,
|
374 | style = _this$props4.style,
|
375 | autoFocus = _this$props4.autoFocus,
|
376 | notFoundContent = _this$props4.notFoundContent,
|
377 | getPopupContainer = _this$props4.getPopupContainer,
|
378 | restProps = (0, _objectWithoutProperties2.default)(_this$props4, ["prefixCls", "placement", "direction", "transitionName", "className", "style", "autoFocus", "notFoundContent", "getPopupContainer"]);
|
379 | var inputProps = (0, _util.omit)(restProps, 'value', 'defaultValue', 'prefix', 'split', 'children', 'validateSearch', 'filterOption', 'onSelect', 'onSearch');
|
380 | var options = measuring ? this.getOptions() : [];
|
381 | return React.createElement("div", {
|
382 | className: (0, _classnames.default)(prefixCls, className),
|
383 | style: style
|
384 | }, React.createElement(_rcTextarea.default, (0, _extends2.default)({
|
385 | autoFocus: autoFocus,
|
386 | ref: this.setTextAreaRef,
|
387 | value: value
|
388 | }, inputProps, {
|
389 | onChange: this.onChange,
|
390 | onKeyDown: this.onKeyDown,
|
391 | onKeyUp: this.onKeyUp,
|
392 | onPressEnter: this.onPressEnter,
|
393 | onFocus: this.onInputFocus,
|
394 | onBlur: this.onInputBlur
|
395 | })), measuring && React.createElement("div", {
|
396 | ref: this.setMeasureRef,
|
397 | className: "".concat(prefixCls, "-measure")
|
398 | }, value.slice(0, measureLocation), React.createElement(_MentionsContext.MentionsContextProvider, {
|
399 | value: {
|
400 | notFoundContent: notFoundContent,
|
401 | activeIndex: activeIndex,
|
402 | setActiveIndex: this.setActiveIndex,
|
403 | selectOption: this.selectOption,
|
404 | onFocus: this.onDropdownFocus,
|
405 | onBlur: this.onDropdownBlur
|
406 | }
|
407 | }, React.createElement(_KeywordTrigger.default, {
|
408 | prefixCls: prefixCls,
|
409 | transitionName: transitionName,
|
410 | placement: placement,
|
411 | direction: direction,
|
412 | options: options,
|
413 | visible: true,
|
414 | getPopupContainer: getPopupContainer
|
415 | }, React.createElement("span", null, measurePrefix))), value.slice(measureLocation + measurePrefix.length)));
|
416 | }
|
417 | }], [{
|
418 | key: "getDerivedStateFromProps",
|
419 | value: function getDerivedStateFromProps(props, prevState) {
|
420 | var newState = {};
|
421 |
|
422 | if ('value' in props && props.value !== prevState.value) {
|
423 | newState.value = props.value || '';
|
424 | }
|
425 |
|
426 | return newState;
|
427 | }
|
428 | }]);
|
429 | return Mentions;
|
430 | }(React.Component);
|
431 |
|
432 | Mentions.Option = _Option.default;
|
433 | Mentions.defaultProps = {
|
434 | prefixCls: 'rc-mentions',
|
435 | prefix: '@',
|
436 | split: ' ',
|
437 | validateSearch: _util.validateSearch,
|
438 | filterOption: _util.filterOption,
|
439 | notFoundContent: 'Not Found',
|
440 | rows: 1
|
441 | };
|
442 | var _default = Mentions;
|
443 | exports.default = _default; |
\ | No newline at end of file |