1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');
|
8 |
|
9 | var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
|
10 |
|
11 | var _extends2 = require('babel-runtime/helpers/extends');
|
12 |
|
13 | var _extends3 = _interopRequireDefault(_extends2);
|
14 |
|
15 | var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
|
16 |
|
17 | var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
|
18 |
|
19 | var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
20 |
|
21 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
22 |
|
23 | var _createClass2 = require('babel-runtime/helpers/createClass');
|
24 |
|
25 | var _createClass3 = _interopRequireDefault(_createClass2);
|
26 |
|
27 | var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
|
28 |
|
29 | var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
|
30 |
|
31 | var _inherits2 = require('babel-runtime/helpers/inherits');
|
32 |
|
33 | var _inherits3 = _interopRequireDefault(_inherits2);
|
34 |
|
35 | var _react = require('react');
|
36 |
|
37 | var _react2 = _interopRequireDefault(_react);
|
38 |
|
39 | var _propTypes = require('prop-types');
|
40 |
|
41 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
42 |
|
43 | var _substyle = require('substyle');
|
44 |
|
45 | var _utils = require('./utils');
|
46 |
|
47 | var _utils2 = _interopRequireDefault(_utils);
|
48 |
|
49 | var _Suggestion = require('./Suggestion');
|
50 |
|
51 | var _Suggestion2 = _interopRequireDefault(_Suggestion);
|
52 |
|
53 | var _LoadingIndicator = require('./LoadingIndicator');
|
54 |
|
55 | var _LoadingIndicator2 = _interopRequireDefault(_LoadingIndicator);
|
56 |
|
57 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
58 |
|
59 | var SuggestionsOverlay = function (_Component) {
|
60 | (0, _inherits3.default)(SuggestionsOverlay, _Component);
|
61 |
|
62 | function SuggestionsOverlay() {
|
63 | (0, _classCallCheck3.default)(this, SuggestionsOverlay);
|
64 | return (0, _possibleConstructorReturn3.default)(this, (SuggestionsOverlay.__proto__ || (0, _getPrototypeOf2.default)(SuggestionsOverlay)).apply(this, arguments));
|
65 | }
|
66 |
|
67 | (0, _createClass3.default)(SuggestionsOverlay, [{
|
68 | key: 'componentDidUpdate',
|
69 | value: function componentDidUpdate() {
|
70 | var suggestions = this.refs.suggestions;
|
71 |
|
72 | if (!suggestions || suggestions.offsetHeight >= suggestions.scrollHeight || !this.props.scrollFocusedIntoView) {
|
73 | return;
|
74 | }
|
75 |
|
76 | var scrollTop = suggestions.scrollTop;
|
77 |
|
78 | var _suggestions$children = suggestions.children[this.props.focusIndex].getBoundingClientRect(),
|
79 | top = _suggestions$children.top,
|
80 | bottom = _suggestions$children.bottom;
|
81 |
|
82 | var _suggestions$getBound = suggestions.getBoundingClientRect(),
|
83 | topContainer = _suggestions$getBound.top;
|
84 |
|
85 | top = top - topContainer + scrollTop;
|
86 | bottom = bottom - topContainer + scrollTop;
|
87 |
|
88 | if (top < scrollTop) {
|
89 | suggestions.scrollTop = top;
|
90 | } else if (bottom > suggestions.offsetHeight) {
|
91 | suggestions.scrollTop = bottom - suggestions.offsetHeight;
|
92 | }
|
93 | }
|
94 | }, {
|
95 | key: 'render',
|
96 | value: function render() {
|
97 | var _props = this.props,
|
98 | suggestions = _props.suggestions,
|
99 | isLoading = _props.isLoading,
|
100 | style = _props.style,
|
101 | onMouseDown = _props.onMouseDown;
|
102 |
|
103 |
|
104 |
|
105 | if (_utils2.default.countSuggestions(suggestions) === 0 && !isLoading) {
|
106 | return null;
|
107 | }
|
108 |
|
109 | return _react2.default.createElement(
|
110 | 'div',
|
111 | (0, _extends3.default)({}, style, {
|
112 | onMouseDown: onMouseDown
|
113 | }),
|
114 | _react2.default.createElement(
|
115 | 'ul',
|
116 | (0, _extends3.default)({
|
117 | ref: 'suggestions'
|
118 | }, style("list")),
|
119 | this.renderSuggestions()
|
120 | ),
|
121 | this.renderLoadingIndicator()
|
122 | );
|
123 | }
|
124 | }, {
|
125 | key: 'renderSuggestions',
|
126 | value: function renderSuggestions() {
|
127 | var _this2 = this;
|
128 |
|
129 | return _utils2.default.getSuggestions(this.props.suggestions).reduce(function (result, _ref) {
|
130 | var suggestions = _ref.suggestions,
|
131 | descriptor = _ref.descriptor;
|
132 | return [].concat((0, _toConsumableArray3.default)(result), (0, _toConsumableArray3.default)(suggestions.map(function (suggestion, index) {
|
133 | return _this2.renderSuggestion(suggestion, descriptor, result.length + index);
|
134 | })));
|
135 | }, []);
|
136 | }
|
137 | }, {
|
138 | key: 'renderSuggestion',
|
139 | value: function renderSuggestion(suggestion, descriptor, index) {
|
140 | var _this3 = this;
|
141 |
|
142 | var id = this.getID(suggestion);
|
143 | var isFocused = index === this.props.focusIndex;
|
144 |
|
145 | var mentionDescriptor = descriptor.mentionDescriptor,
|
146 | query = descriptor.query;
|
147 |
|
148 |
|
149 | return _react2.default.createElement(_Suggestion2.default, {
|
150 | style: this.props.style("item"),
|
151 | key: id,
|
152 | id: id,
|
153 | ref: isFocused ? "focused" : null,
|
154 | query: query,
|
155 | index: index,
|
156 | descriptor: mentionDescriptor,
|
157 | suggestion: suggestion,
|
158 | focused: isFocused,
|
159 | onClick: function onClick() {
|
160 | return _this3.select(suggestion, descriptor);
|
161 | },
|
162 | onMouseEnter: function onMouseEnter() {
|
163 | return _this3.handleMouseEnter(index);
|
164 | } });
|
165 | }
|
166 | }, {
|
167 | key: 'getID',
|
168 | value: function getID(suggestion) {
|
169 | if (suggestion instanceof String) {
|
170 | return suggestion;
|
171 | }
|
172 |
|
173 | return suggestion.id;
|
174 | }
|
175 | }, {
|
176 | key: 'renderLoadingIndicator',
|
177 | value: function renderLoadingIndicator() {
|
178 | if (!this.props.isLoading) {
|
179 | return;
|
180 | }
|
181 |
|
182 | return _react2.default.createElement(_LoadingIndicator2.default, this.props.style("loadingIndicator"));
|
183 | }
|
184 | }, {
|
185 | key: 'handleMouseEnter',
|
186 | value: function handleMouseEnter(index, ev) {
|
187 | if (this.props.onMouseEnter) {
|
188 | this.props.onMouseEnter(index);
|
189 | }
|
190 | }
|
191 | }, {
|
192 | key: 'select',
|
193 | value: function select(suggestion, descriptor) {
|
194 | this.props.onSelect(suggestion, descriptor);
|
195 | }
|
196 | }]);
|
197 | return SuggestionsOverlay;
|
198 | }(_react.Component);
|
199 |
|
200 | SuggestionsOverlay.propTypes = {
|
201 | suggestions: _propTypes2.default.object.isRequired,
|
202 | focusIndex: _propTypes2.default.number,
|
203 | scrollFocusedIntoView: _propTypes2.default.bool,
|
204 | isLoading: _propTypes2.default.bool,
|
205 | onSelect: _propTypes2.default.func
|
206 | };
|
207 | SuggestionsOverlay.defaultProps = {
|
208 | suggestions: {},
|
209 | onSelect: function onSelect() {
|
210 | return null;
|
211 | }
|
212 | };
|
213 |
|
214 |
|
215 | var styled = (0, _substyle.defaultStyle)(function (_ref2) {
|
216 | var position = _ref2.position;
|
217 | return (0, _extends3.default)({
|
218 | position: "absolute",
|
219 | zIndex: 1,
|
220 | backgroundColor: "white",
|
221 | marginTop: 14,
|
222 | minWidth: 100
|
223 | }, position, {
|
224 |
|
225 | list: {
|
226 | margin: 0,
|
227 | padding: 0,
|
228 | listStyleType: "none"
|
229 | }
|
230 | });
|
231 | });
|
232 |
|
233 | exports.default = styled(SuggestionsOverlay); |
\ | No newline at end of file |