1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports._getTriggerRegex = undefined;
|
7 |
|
8 | var _extends2 = require('babel-runtime/helpers/extends');
|
9 |
|
10 | var _extends3 = _interopRequireDefault(_extends2);
|
11 |
|
12 | var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
|
13 |
|
14 | var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
|
15 |
|
16 | var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
17 |
|
18 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
19 |
|
20 | var _createClass2 = require('babel-runtime/helpers/createClass');
|
21 |
|
22 | var _createClass3 = _interopRequireDefault(_createClass2);
|
23 |
|
24 | var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
|
25 |
|
26 | var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
|
27 |
|
28 | var _inherits2 = require('babel-runtime/helpers/inherits');
|
29 |
|
30 | var _inherits3 = _interopRequireDefault(_inherits2);
|
31 |
|
32 | var _react = require('react');
|
33 |
|
34 | var _react2 = _interopRequireDefault(_react);
|
35 |
|
36 | var _propTypes = require('prop-types');
|
37 |
|
38 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
39 |
|
40 | var _reactDom = require('react-dom');
|
41 |
|
42 | var _reactDom2 = _interopRequireDefault(_reactDom);
|
43 |
|
44 | var _keys = require('lodash/keys');
|
45 |
|
46 | var _keys2 = _interopRequireDefault(_keys);
|
47 |
|
48 | var _values = require('lodash/values');
|
49 |
|
50 | var _values2 = _interopRequireDefault(_values);
|
51 |
|
52 | var _omit = require('lodash/omit');
|
53 |
|
54 | var _omit2 = _interopRequireDefault(_omit);
|
55 |
|
56 | var _isEqual = require('lodash/isEqual');
|
57 |
|
58 | var _isEqual2 = _interopRequireDefault(_isEqual);
|
59 |
|
60 | var _substyle = require('substyle');
|
61 |
|
62 | var _utils = require('./utils');
|
63 |
|
64 | var _utils2 = _interopRequireDefault(_utils);
|
65 |
|
66 | var _SuggestionsOverlay = require('./SuggestionsOverlay');
|
67 |
|
68 | var _SuggestionsOverlay2 = _interopRequireDefault(_SuggestionsOverlay);
|
69 |
|
70 | var _Highlighter = require('./Highlighter');
|
71 |
|
72 | var _Highlighter2 = _interopRequireDefault(_Highlighter);
|
73 |
|
74 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
75 |
|
76 | var _getTriggerRegex = exports._getTriggerRegex = function _getTriggerRegex(trigger) {
|
77 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
78 |
|
79 | if (trigger instanceof RegExp) {
|
80 | return trigger;
|
81 | } else {
|
82 | var allowSpaceInQuery = options.allowSpaceInQuery;
|
83 |
|
84 | var escapedTriggerChar = _utils2.default.escapeRegex(trigger);
|
85 |
|
86 |
|
87 |
|
88 | return new RegExp('(?:^|\\s)(' + escapedTriggerChar + '([^' + (allowSpaceInQuery ? '' : '\\s') + escapedTriggerChar + ']*))$');
|
89 | }
|
90 | };
|
91 |
|
92 | var _getDataProvider = function _getDataProvider(data) {
|
93 | if (data instanceof Array) {
|
94 |
|
95 | return function (query, callback) {
|
96 | var results = [];
|
97 | for (var i = 0, l = data.length; i < l; ++i) {
|
98 | var display = data[i].display || data[i].id;
|
99 | if (display.toLowerCase().indexOf(query.toLowerCase()) >= 0) {
|
100 | results.push(data[i]);
|
101 | }
|
102 | }
|
103 | return results;
|
104 | };
|
105 | } else {
|
106 |
|
107 | return data;
|
108 | }
|
109 | };
|
110 |
|
111 | var KEY = { TAB: 9, RETURN: 13, ESC: 27, UP: 38, DOWN: 40 };
|
112 |
|
113 | var isComposing = false;
|
114 |
|
115 | var MentionsInput = function (_React$Component) {
|
116 | (0, _inherits3.default)(MentionsInput, _React$Component);
|
117 |
|
118 | function MentionsInput(props) {
|
119 | (0, _classCallCheck3.default)(this, MentionsInput);
|
120 |
|
121 | var _this = (0, _possibleConstructorReturn3.default)(this, (MentionsInput.__proto__ || (0, _getPrototypeOf2.default)(MentionsInput)).call(this, props));
|
122 |
|
123 | _initialiseProps.call(_this);
|
124 |
|
125 | _this.suggestions = {};
|
126 |
|
127 | _this.state = {
|
128 | focusIndex: 0,
|
129 |
|
130 | selectionStart: null,
|
131 | selectionEnd: null,
|
132 |
|
133 | suggestions: {},
|
134 |
|
135 | caretPosition: null,
|
136 | suggestionsPosition: null
|
137 | };
|
138 | return _this;
|
139 | }
|
140 |
|
141 | (0, _createClass3.default)(MentionsInput, [{
|
142 | key: 'render',
|
143 | value: function render() {
|
144 | return _react2.default.createElement(
|
145 | 'div',
|
146 | (0, _extends3.default)({ ref: 'container' }, this.props.style),
|
147 | this.renderControl(),
|
148 | this.renderSuggestionsOverlay()
|
149 | );
|
150 | }
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | }, {
|
161 | key: 'componentDidMount',
|
162 | value: function componentDidMount() {
|
163 | this.updateSuggestionsPosition();
|
164 | }
|
165 | }, {
|
166 | key: 'componentDidUpdate',
|
167 | value: function componentDidUpdate() {
|
168 | this.updateSuggestionsPosition();
|
169 |
|
170 |
|
171 |
|
172 | if (this.state.setSelectionAfterMentionChange) {
|
173 | this.setState({ setSelectionAfterMentionChange: false });
|
174 | this.setSelection(this.state.selectionStart, this.state.selectionEnd);
|
175 | }
|
176 | }
|
177 | }]);
|
178 | return MentionsInput;
|
179 | }(_react2.default.Component);
|
180 |
|
181 | MentionsInput.propTypes = {
|
182 | |
183 |
|
184 |
|
185 |
|
186 | singleLine: _propTypes2.default.bool,
|
187 |
|
188 | |
189 |
|
190 |
|
191 | allowSpaceInQuery: _propTypes2.default.bool,
|
192 |
|
193 | markup: _propTypes2.default.string,
|
194 | value: _propTypes2.default.string,
|
195 |
|
196 | displayTransform: _propTypes2.default.func,
|
197 | onKeyDown: _propTypes2.default.func,
|
198 | onSelect: _propTypes2.default.func,
|
199 | onBlur: _propTypes2.default.func,
|
200 | onChange: _propTypes2.default.func,
|
201 |
|
202 | children: _propTypes2.default.oneOfType([_propTypes2.default.element, _propTypes2.default.arrayOf(_propTypes2.default.element)]).isRequired
|
203 | };
|
204 | MentionsInput.defaultProps = {
|
205 | markup: "@[__display__](__id__)",
|
206 | singleLine: false,
|
207 | displayTransform: function displayTransform(id, display, type) {
|
208 | return display;
|
209 | },
|
210 | onKeyDown: function onKeyDown() {
|
211 | return null;
|
212 | },
|
213 | onSelect: function onSelect() {
|
214 | return null;
|
215 | },
|
216 | onBlur: function onBlur() {
|
217 | return null;
|
218 | }
|
219 | };
|
220 |
|
221 | var _initialiseProps = function _initialiseProps() {
|
222 | var _this2 = this;
|
223 |
|
224 | this.getInputProps = function (isTextarea) {
|
225 | var _props = _this2.props,
|
226 | readOnly = _props.readOnly,
|
227 | disabled = _props.disabled,
|
228 | style = _props.style;
|
229 |
|
230 |
|
231 |
|
232 | var props = (0, _omit2.default)(_this2.props, 'style', (0, _keys2.default)(MentionsInput.propTypes));
|
233 |
|
234 | return (0, _extends3.default)({}, props, style("input"), {
|
235 |
|
236 | value: _this2.getPlainText()
|
237 |
|
238 | }, !readOnly && !disabled && {
|
239 | onChange: _this2.handleChange,
|
240 | onSelect: _this2.handleSelect,
|
241 | onKeyDown: _this2.handleKeyDown,
|
242 | onBlur: _this2.handleBlur,
|
243 | onCompositionStart: _this2.handleCompositionStart,
|
244 | onCompositionEnd: _this2.handleCompositionEnd
|
245 | });
|
246 | };
|
247 |
|
248 | this.renderControl = function () {
|
249 | var _props2 = _this2.props,
|
250 | singleLine = _props2.singleLine,
|
251 | style = _props2.style;
|
252 |
|
253 | var inputProps = _this2.getInputProps(!singleLine);
|
254 |
|
255 | return _react2.default.createElement(
|
256 | 'div',
|
257 | style("control"),
|
258 | _this2.renderHighlighter(inputProps.style),
|
259 | singleLine ? _this2.renderInput(inputProps) : _this2.renderTextarea(inputProps)
|
260 | );
|
261 | };
|
262 |
|
263 | this.renderInput = function (props) {
|
264 | return _react2.default.createElement('input', (0, _extends3.default)({
|
265 | type: 'text',
|
266 | ref: 'input'
|
267 | }, props));
|
268 | };
|
269 |
|
270 | this.renderTextarea = function (props) {
|
271 | return _react2.default.createElement('textarea', (0, _extends3.default)({
|
272 | ref: 'input'
|
273 | }, props));
|
274 | };
|
275 |
|
276 | this.renderSuggestionsOverlay = function () {
|
277 | if (!_utils2.default.isNumber(_this2.state.selectionStart)) {
|
278 |
|
279 | return null;
|
280 | }
|
281 | return _react2.default.createElement(_SuggestionsOverlay2.default, {
|
282 | style: _this2.props.style("suggestions"),
|
283 | position: _this2.state.suggestionsPosition,
|
284 | focusIndex: _this2.state.focusIndex,
|
285 | scrollFocusedIntoView: _this2.state.scrollFocusedIntoView,
|
286 | ref: 'suggestions',
|
287 | suggestions: _this2.state.suggestions,
|
288 | onSelect: _this2.addMention,
|
289 | onMouseDown: _this2.handleSuggestionsMouseDown,
|
290 | onMouseEnter: function onMouseEnter(focusIndex) {
|
291 | return _this2.setState({
|
292 | focusIndex: focusIndex,
|
293 | scrollFocusedIntoView: false
|
294 | });
|
295 | },
|
296 | isLoading: _this2.isLoading() });
|
297 | };
|
298 |
|
299 | this.renderHighlighter = function (inputStyle) {
|
300 | var _state = _this2.state,
|
301 | selectionStart = _state.selectionStart,
|
302 | selectionEnd = _state.selectionEnd;
|
303 | var _props3 = _this2.props,
|
304 | markup = _props3.markup,
|
305 | displayTransform = _props3.displayTransform,
|
306 | singleLine = _props3.singleLine,
|
307 | children = _props3.children,
|
308 | value = _props3.value,
|
309 | style = _props3.style;
|
310 |
|
311 |
|
312 | return _react2.default.createElement(
|
313 | _Highlighter2.default,
|
314 | {
|
315 | ref: 'highlighter',
|
316 | style: style("highlighter"),
|
317 | inputStyle: inputStyle,
|
318 | value: value,
|
319 | markup: markup,
|
320 | displayTransform: displayTransform,
|
321 | singleLine: singleLine,
|
322 | selection: {
|
323 | start: selectionStart,
|
324 | end: selectionEnd
|
325 | },
|
326 | onCaretPositionChange: function onCaretPositionChange(position) {
|
327 | return _this2.setState({ caretPosition: position });
|
328 | } },
|
329 | children
|
330 | );
|
331 | };
|
332 |
|
333 | this.getPlainText = function () {
|
334 | return _utils2.default.getPlainText(_this2.props.value || "", _this2.props.markup, _this2.props.displayTransform);
|
335 | };
|
336 |
|
337 | this.executeOnChange = function (event) {
|
338 | for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
339 | args[_key - 1] = arguments[_key];
|
340 | }
|
341 |
|
342 | if (_this2.props.onChange) {
|
343 | var _props4;
|
344 |
|
345 | return (_props4 = _this2.props).onChange.apply(_props4, [event].concat(args));
|
346 | }
|
347 |
|
348 | if (_this2.props.valueLink) {
|
349 | var _props$valueLink;
|
350 |
|
351 | return (_props$valueLink = _this2.props.valueLink).requestChange.apply(_props$valueLink, [event.target.value].concat(args));
|
352 | }
|
353 | };
|
354 |
|
355 | this.handleChange = function (ev) {
|
356 |
|
357 | if (document.activeElement !== ev.target) {
|
358 |
|
359 | return;
|
360 | }
|
361 |
|
362 | var value = _this2.props.value || "";
|
363 | var newPlainTextValue = ev.target.value;
|
364 |
|
365 |
|
366 | var newValue = _utils2.default.applyChangeToValue(value, _this2.props.markup, newPlainTextValue, _this2.state.selectionStart, _this2.state.selectionEnd, ev.target.selectionEnd, _this2.props.displayTransform);
|
367 |
|
368 |
|
369 | newPlainTextValue = _utils2.default.getPlainText(newValue, _this2.props.markup, _this2.props.displayTransform);
|
370 |
|
371 |
|
372 | var selectionStart = ev.target.selectionStart;
|
373 | var selectionEnd = ev.target.selectionEnd;
|
374 | var setSelectionAfterMentionChange = false;
|
375 |
|
376 |
|
377 |
|
378 | var startOfMention = _utils2.default.findStartOfMentionInPlainText(value, _this2.props.markup, selectionStart, _this2.props.displayTransform);
|
379 |
|
380 | if (startOfMention !== undefined && _this2.state.selectionEnd > startOfMention) {
|
381 |
|
382 | selectionStart = startOfMention;
|
383 | selectionEnd = selectionStart;
|
384 | setSelectionAfterMentionChange = true;
|
385 | }
|
386 |
|
387 | _this2.setState({
|
388 | selectionStart: selectionStart,
|
389 | selectionEnd: selectionEnd,
|
390 | setSelectionAfterMentionChange: setSelectionAfterMentionChange
|
391 | });
|
392 |
|
393 | var mentions = _utils2.default.getMentions(newValue, _this2.props.markup);
|
394 |
|
395 |
|
396 |
|
397 | var eventMock = { target: { value: newValue } };
|
398 |
|
399 | _this2.executeOnChange(eventMock, newValue, newPlainTextValue, mentions);
|
400 | };
|
401 |
|
402 | this.handleSelect = function (ev) {
|
403 |
|
404 | if (isComposing) return;
|
405 |
|
406 |
|
407 | _this2.setState({
|
408 | selectionStart: ev.target.selectionStart,
|
409 | selectionEnd: ev.target.selectionEnd
|
410 | });
|
411 |
|
412 |
|
413 | var el = _this2.refs.input;
|
414 | if (ev.target.selectionStart === ev.target.selectionEnd) {
|
415 | _this2.updateMentionsQueries(el.value, ev.target.selectionStart);
|
416 | } else {
|
417 | _this2.clearSuggestions();
|
418 | }
|
419 |
|
420 |
|
421 | _this2.updateHighlighterScroll();
|
422 |
|
423 | _this2.props.onSelect(ev);
|
424 | };
|
425 |
|
426 | this.handleKeyDown = function (ev) {
|
427 |
|
428 | var suggestionsCount = _utils2.default.countSuggestions(_this2.state.suggestions);
|
429 |
|
430 | var suggestionsComp = _this2.refs.suggestions;
|
431 | if (suggestionsCount === 0 || !suggestionsComp) {
|
432 | _this2.props.onKeyDown(ev);
|
433 |
|
434 | return;
|
435 | }
|
436 |
|
437 | if ((0, _values2.default)(KEY).indexOf(ev.keyCode) >= 0) {
|
438 | ev.preventDefault();
|
439 | }
|
440 |
|
441 | switch (ev.keyCode) {
|
442 | case KEY.ESC:
|
443 | {
|
444 | _this2.clearSuggestions();
|
445 | return;
|
446 | }
|
447 | case KEY.DOWN:
|
448 | {
|
449 | _this2.shiftFocus(+1);
|
450 | return;
|
451 | }
|
452 | case KEY.UP:
|
453 | {
|
454 | _this2.shiftFocus(-1);
|
455 | return;
|
456 | }
|
457 | case KEY.RETURN:
|
458 | {
|
459 | _this2.selectFocused();
|
460 | return;
|
461 | }
|
462 | case KEY.TAB:
|
463 | {
|
464 | _this2.selectFocused();
|
465 | return;
|
466 | }
|
467 | }
|
468 | };
|
469 |
|
470 | this.shiftFocus = function (delta) {
|
471 | var suggestionsCount = _utils2.default.countSuggestions(_this2.state.suggestions);
|
472 |
|
473 | _this2.setState({
|
474 | focusIndex: (suggestionsCount + _this2.state.focusIndex + delta) % suggestionsCount,
|
475 | scrollFocusedIntoView: true
|
476 | });
|
477 | };
|
478 |
|
479 | this.selectFocused = function () {
|
480 | var _state2 = _this2.state,
|
481 | suggestions = _state2.suggestions,
|
482 | focusIndex = _state2.focusIndex;
|
483 |
|
484 | var _utils$getSuggestion = _utils2.default.getSuggestion(suggestions, focusIndex),
|
485 | suggestion = _utils$getSuggestion.suggestion,
|
486 | descriptor = _utils$getSuggestion.descriptor;
|
487 |
|
488 | _this2.addMention(suggestion, descriptor);
|
489 |
|
490 | _this2.setState({
|
491 | focusIndex: 0
|
492 | });
|
493 | };
|
494 |
|
495 | this.handleBlur = function (ev) {
|
496 | var clickedSuggestion = _this2._suggestionsMouseDown;
|
497 | _this2._suggestionsMouseDown = false;
|
498 |
|
499 |
|
500 |
|
501 | if (!clickedSuggestion) {
|
502 | _this2.setState({
|
503 | selectionStart: null,
|
504 | selectionEnd: null
|
505 | });
|
506 | };
|
507 |
|
508 | window.setTimeout(function () {
|
509 | _this2.updateHighlighterScroll();
|
510 | }, 1);
|
511 |
|
512 | _this2.props.onBlur(ev, clickedSuggestion);
|
513 | };
|
514 |
|
515 | this.handleSuggestionsMouseDown = function (ev) {
|
516 | _this2._suggestionsMouseDown = true;
|
517 | };
|
518 |
|
519 | this.updateSuggestionsPosition = function () {
|
520 | var caretPosition = _this2.state.caretPosition;
|
521 |
|
522 |
|
523 | if (!caretPosition || !_this2.refs.suggestions) {
|
524 | return;
|
525 | }
|
526 |
|
527 | var container = _this2.refs.container;
|
528 |
|
529 |
|
530 | var suggestions = _reactDom2.default.findDOMNode(_this2.refs.suggestions);
|
531 | var highlighter = _reactDom2.default.findDOMNode(_this2.refs.highlighter);
|
532 |
|
533 | if (!suggestions) {
|
534 | return;
|
535 | }
|
536 |
|
537 | var left = caretPosition.left - highlighter.scrollLeft;
|
538 | var position = {};
|
539 |
|
540 |
|
541 | if (left + suggestions.offsetWidth > container.offsetWidth) {
|
542 | position.right = 0;
|
543 | } else {
|
544 | position.left = left;
|
545 | }
|
546 |
|
547 | position.top = caretPosition.top - highlighter.scrollTop;
|
548 |
|
549 | if ((0, _isEqual2.default)(position, _this2.state.suggestionsPosition)) {
|
550 | return;
|
551 | }
|
552 |
|
553 | _this2.setState({
|
554 | suggestionsPosition: position
|
555 | });
|
556 | };
|
557 |
|
558 | this.updateHighlighterScroll = function () {
|
559 | if (!_this2.refs.input || !_this2.refs.highlighter) {
|
560 |
|
561 |
|
562 | return;
|
563 | }
|
564 | var input = _this2.refs.input;
|
565 | var highlighter = _reactDom2.default.findDOMNode(_this2.refs.highlighter);
|
566 | highlighter.scrollLeft = input.scrollLeft;
|
567 | };
|
568 |
|
569 | this.handleCompositionStart = function () {
|
570 | isComposing = true;
|
571 | };
|
572 |
|
573 | this.handleCompositionEnd = function () {
|
574 | isComposing = false;
|
575 | };
|
576 |
|
577 | this.setSelection = function (selectionStart, selectionEnd) {
|
578 | if (selectionStart === null || selectionEnd === null) return;
|
579 |
|
580 | var el = _this2.refs.input;
|
581 | if (el.setSelectionRange) {
|
582 | el.setSelectionRange(selectionStart, selectionEnd);
|
583 | } else if (el.createTextRange) {
|
584 | var range = el.createTextRange();
|
585 | range.collapse(true);
|
586 | range.moveEnd('character', selectionEnd);
|
587 | range.moveStart('character', selectionStart);
|
588 | range.select();
|
589 | }
|
590 | };
|
591 |
|
592 | this.updateMentionsQueries = function (plainTextValue, caretPosition) {
|
593 |
|
594 | _this2._queryId++;
|
595 | _this2.suggestions = {};
|
596 | _this2.setState({
|
597 | suggestions: {}
|
598 | });
|
599 |
|
600 |
|
601 | var value = _this2.props.value || "";
|
602 | if (_utils2.default.isInsideOfMention(value, _this2.props.markup, caretPosition, _this2.props.displayTransform) || _utils2.default.isInsideOfMention(value, _this2.props.markup, caretPosition - 1, _this2.props.displayTransform)) {
|
603 | return;
|
604 | }
|
605 |
|
606 |
|
607 |
|
608 | var substring = plainTextValue.substring(0, caretPosition);
|
609 |
|
610 | _react2.default.Children.forEach(_this2.props.children, function (child) {
|
611 | if (!child) {
|
612 | return;
|
613 | }
|
614 |
|
615 | var regex = _getTriggerRegex(child.props.trigger, _this2.props);
|
616 | var match = substring.match(regex);
|
617 | if (match) {
|
618 | var querySequenceStart = substring.indexOf(match[1], match.index);
|
619 | _this2.queryData(match[2], child, querySequenceStart, querySequenceStart + match[1].length, plainTextValue);
|
620 | }
|
621 | });
|
622 | };
|
623 |
|
624 | this.clearSuggestions = function () {
|
625 |
|
626 | _this2._queryId++;
|
627 | _this2.suggestions = {};
|
628 | _this2.setState({
|
629 | suggestions: {},
|
630 | focusIndex: 0
|
631 | });
|
632 | };
|
633 |
|
634 | this.queryData = function (query, mentionDescriptor, querySequenceStart, querySequenceEnd, plainTextValue) {
|
635 | var provideData = _getDataProvider(mentionDescriptor.props.data);
|
636 | var snycResult = provideData(query, _this2.updateSuggestions.bind(null, _this2._queryId, mentionDescriptor, query, querySequenceStart, querySequenceEnd, plainTextValue));
|
637 | if (snycResult instanceof Array) {
|
638 | _this2.updateSuggestions(_this2._queryId, mentionDescriptor, query, querySequenceStart, querySequenceEnd, plainTextValue, snycResult);
|
639 | }
|
640 | };
|
641 |
|
642 | this.updateSuggestions = function (queryId, mentionDescriptor, query, querySequenceStart, querySequenceEnd, plainTextValue, suggestions) {
|
643 |
|
644 | if (queryId !== _this2._queryId) return;
|
645 |
|
646 | var update = {};
|
647 | update[mentionDescriptor.props.type] = {
|
648 | query: query,
|
649 | mentionDescriptor: mentionDescriptor,
|
650 | querySequenceStart: querySequenceStart,
|
651 | querySequenceEnd: querySequenceEnd,
|
652 | results: suggestions,
|
653 | plainTextValue: plainTextValue
|
654 | };
|
655 |
|
656 |
|
657 |
|
658 | _this2.suggestions = _utils2.default.extend({}, _this2.suggestions, update);
|
659 |
|
660 | _this2.setState({
|
661 | suggestions: _this2.suggestions
|
662 | });
|
663 | };
|
664 |
|
665 | this.addMention = function (suggestion, _ref2) {
|
666 | var mentionDescriptor = _ref2.mentionDescriptor,
|
667 | querySequenceStart = _ref2.querySequenceStart,
|
668 | querySequenceEnd = _ref2.querySequenceEnd,
|
669 | plainTextValue = _ref2.plainTextValue;
|
670 |
|
671 |
|
672 | var value = _this2.props.value || "";
|
673 | var start = _utils2.default.mapPlainTextIndex(value, _this2.props.markup, querySequenceStart, 'START', _this2.props.displayTransform);
|
674 | var end = start + querySequenceEnd - querySequenceStart;
|
675 | var insert = _utils2.default.makeMentionsMarkup(_this2.props.markup, suggestion.id, suggestion.display, mentionDescriptor.props.type);
|
676 | if (mentionDescriptor.props.appendSpaceOnAdd) {
|
677 | insert = insert + ' ';
|
678 | }
|
679 | var newValue = _utils2.default.spliceString(value, start, end, insert);
|
680 |
|
681 |
|
682 | _this2.refs.input.focus();
|
683 |
|
684 | var displayValue = _this2.props.displayTransform(suggestion.id, suggestion.display, mentionDescriptor.props.type);
|
685 | if (mentionDescriptor.props.appendSpaceOnAdd) {
|
686 | displayValue = displayValue + ' ';
|
687 | }
|
688 | var newCaretPosition = querySequenceStart + displayValue.length;
|
689 | _this2.setState({
|
690 | selectionStart: newCaretPosition,
|
691 | selectionEnd: newCaretPosition,
|
692 | setSelectionAfterMentionChange: true
|
693 | });
|
694 |
|
695 |
|
696 | var eventMock = { target: { value: newValue } };
|
697 | var mentions = _utils2.default.getMentions(newValue, _this2.props.markup);
|
698 | var newPlainTextValue = _utils2.default.spliceString(plainTextValue, querySequenceStart, querySequenceEnd, displayValue);
|
699 |
|
700 | _this2.executeOnChange(eventMock, newValue, newPlainTextValue, mentions);
|
701 |
|
702 | var onAdd = mentionDescriptor.props.onAdd;
|
703 | if (onAdd) {
|
704 | onAdd(suggestion.id, suggestion.display);
|
705 | }
|
706 |
|
707 |
|
708 | _this2.clearSuggestions();
|
709 | };
|
710 |
|
711 | this.isLoading = function () {
|
712 | var isLoading = false;
|
713 | _react2.default.Children.forEach(_this2.props.children, function (child) {
|
714 | isLoading = isLoading || child && child.props.isLoading;
|
715 | });
|
716 | return isLoading;
|
717 | };
|
718 |
|
719 | this._queryId = 0;
|
720 | };
|
721 |
|
722 | var isMobileSafari = typeof navigator !== 'undefined' && /iPhone|iPad|iPod/i.test(navigator.userAgent);
|
723 |
|
724 | var styled = (0, _substyle.defaultStyle)({
|
725 | position: "relative",
|
726 | overflowY: "visible",
|
727 |
|
728 | input: {
|
729 | display: "block",
|
730 | position: "absolute",
|
731 |
|
732 | top: 0,
|
733 |
|
734 | boxSizing: "border-box",
|
735 |
|
736 | backgroundColor: "transparent",
|
737 |
|
738 | width: "inherit"
|
739 | },
|
740 |
|
741 | '&multiLine': {
|
742 | input: (0, _extends3.default)({
|
743 | width: "100%",
|
744 | height: "100%",
|
745 | bottom: 0,
|
746 | overflow: "hidden",
|
747 | resize: "none"
|
748 |
|
749 | }, isMobileSafari ? {
|
750 | marginTop: 1,
|
751 | marginLeft: -3
|
752 | } : null)
|
753 | }
|
754 | }, function (_ref) {
|
755 | var singleLine = _ref.singleLine;
|
756 | return {
|
757 | "&singleLine": singleLine,
|
758 | "&multiLine": !singleLine
|
759 | };
|
760 | });
|
761 |
|
762 | exports.default = styled(MentionsInput); |
\ | No newline at end of file |