1 | 'use strict';
|
2 |
|
3 | exports.__esModule = true;
|
4 |
|
5 | var _extends2 = require('babel-runtime/helpers/extends');
|
6 |
|
7 | var _extends3 = _interopRequireDefault(_extends2);
|
8 |
|
9 | var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
10 |
|
11 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
12 |
|
13 | var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
|
14 |
|
15 | var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
|
16 |
|
17 | var _inherits2 = require('babel-runtime/helpers/inherits');
|
18 |
|
19 | var _inherits3 = _interopRequireDefault(_inherits2);
|
20 |
|
21 | var _class, _temp;
|
22 |
|
23 | var _react = require('react');
|
24 |
|
25 | var _react2 = _interopRequireDefault(_react);
|
26 |
|
27 | var _propTypes = require('prop-types');
|
28 |
|
29 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
30 |
|
31 | var _reactLifecyclesCompat = require('react-lifecycles-compat');
|
32 |
|
33 | var _classnames = require('classnames');
|
34 |
|
35 | var _classnames2 = _interopRequireDefault(_classnames);
|
36 |
|
37 | var _select = require('../select');
|
38 |
|
39 | var _select2 = _interopRequireDefault(_select);
|
40 |
|
41 | var _cascader = require('../cascader');
|
42 |
|
43 | var _cascader2 = _interopRequireDefault(_cascader);
|
44 |
|
45 | var _menu = require('../menu');
|
46 |
|
47 | var _menu2 = _interopRequireDefault(_menu);
|
48 |
|
49 | var _util = require('../util');
|
50 |
|
51 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
52 |
|
53 | var bindCtx = _util.func.bindCtx;
|
54 | var pickOthers = _util.obj.pickOthers;
|
55 | var getStyle = _util.dom.getStyle;
|
56 |
|
57 |
|
58 | var normalizeValue = function normalizeValue(value) {
|
59 | if (value) {
|
60 | if (Array.isArray(value)) {
|
61 | return value;
|
62 | }
|
63 |
|
64 | return [value];
|
65 | }
|
66 |
|
67 | return [];
|
68 | };
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | var CascaderSelect = (_temp = _class = function (_Component) {
|
74 | (0, _inherits3.default)(CascaderSelect, _Component);
|
75 |
|
76 | function CascaderSelect(props) {
|
77 | (0, _classCallCheck3.default)(this, CascaderSelect);
|
78 |
|
79 | var _this = (0, _possibleConstructorReturn3.default)(this, _Component.call(this, props));
|
80 |
|
81 | _this.refreshValueDataCache = function (curValue) {
|
82 | if (curValue) {
|
83 | var valueArr = Array.isArray(curValue) ? curValue : [curValue];
|
84 |
|
85 | valueArr.length && Object.keys(_this._valueDataCache).forEach(function (v) {
|
86 | if (!valueArr.includes(v)) {
|
87 | delete _this._valueDataCache[v];
|
88 | }
|
89 | });
|
90 | } else {
|
91 | _this._valueDataCache = {};
|
92 | }
|
93 | };
|
94 |
|
95 | _this.state = {
|
96 | value: normalizeValue('value' in props ? props.value : props.defaultValue),
|
97 | searchValue: '',
|
98 | visible: typeof props.visible === 'undefined' ? props.defaultVisible : props.visible
|
99 | };
|
100 |
|
101 |
|
102 | _this._valueDataCache = {};
|
103 |
|
104 | bindCtx(_this, ['handleVisibleChange', 'handleAfterOpen', 'handleSelect', 'handleChange', 'handleClear', 'handleRemove', 'handleSearch', 'getPopup', 'saveSelectRef', 'saveCascaderRef', 'handleKeyDown']);
|
105 | return _this;
|
106 | }
|
107 |
|
108 | CascaderSelect.getDerivedStateFromProps = function getDerivedStateFromProps(props) {
|
109 | var st = {};
|
110 |
|
111 | if ('value' in props) {
|
112 | st.value = normalizeValue(props.value);
|
113 | }
|
114 | if ('visible' in props) {
|
115 | st.visible = props.visible;
|
116 | }
|
117 |
|
118 | return st;
|
119 | };
|
120 |
|
121 | CascaderSelect.prototype.updateCache = function updateCache(dataSource) {
|
122 | var _this2 = this;
|
123 |
|
124 | this._v2n = {};
|
125 | this._p2n = {};
|
126 | var loop = function loop(data) {
|
127 | var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '0';
|
128 | return data.forEach(function (item, index) {
|
129 | var value = item.value,
|
130 | children = item.children;
|
131 |
|
132 | var pos = prefix + '-' + index;
|
133 | _this2._v2n[value] = _this2._p2n[pos] = (0, _extends3.default)({}, item, { pos: pos });
|
134 |
|
135 | if (children && children.length) {
|
136 | loop(children, pos);
|
137 | }
|
138 | });
|
139 | };
|
140 |
|
141 | loop(dataSource);
|
142 | };
|
143 |
|
144 | CascaderSelect.prototype.flatValue = function flatValue(value) {
|
145 | var _this3 = this;
|
146 |
|
147 | var getDepth = function getDepth(v) {
|
148 | var pos = _this3.getPos(v);
|
149 | if (!pos) {
|
150 | return 0;
|
151 | }
|
152 | return pos.split('-').length;
|
153 | };
|
154 | var newValue = value.slice(0).sort(function (prev, next) {
|
155 | return getDepth(prev) - getDepth(next);
|
156 | });
|
157 |
|
158 | for (var i = 0; i < newValue.length; i++) {
|
159 | for (var j = 0; j < newValue.length; j++) {
|
160 | if (i !== j && this.isDescendantOrSelf(this.getPos(newValue[i]), this.getPos(newValue[j]))) {
|
161 | newValue.splice(j, 1);
|
162 | j--;
|
163 | }
|
164 | }
|
165 | }
|
166 |
|
167 | return newValue;
|
168 | };
|
169 |
|
170 | CascaderSelect.prototype.isDescendantOrSelf = function isDescendantOrSelf(currentPos, targetPos) {
|
171 | if (!currentPos || !targetPos) {
|
172 | return false;
|
173 | }
|
174 |
|
175 | var currentNums = currentPos.split('-');
|
176 | var targetNums = targetPos.split('-');
|
177 |
|
178 | return currentNums.length <= targetNums.length && currentNums.every(function (num, index) {
|
179 | return num === targetNums[index];
|
180 | });
|
181 | };
|
182 |
|
183 | CascaderSelect.prototype.getValue = function getValue(pos) {
|
184 | return this._p2n[pos] ? this._p2n[pos].value : null;
|
185 | };
|
186 |
|
187 | CascaderSelect.prototype.getPos = function getPos(value) {
|
188 | return this._v2n[value] ? this._v2n[value].pos : null;
|
189 | };
|
190 |
|
191 | CascaderSelect.prototype.getData = function getData(value) {
|
192 | var _this4 = this;
|
193 |
|
194 | return value.map(function (v) {
|
195 | return _this4._v2n[v] || _this4._valueDataCache[v];
|
196 | });
|
197 | };
|
198 |
|
199 | CascaderSelect.prototype.getLabelPath = function getLabelPath(data) {
|
200 | var _this5 = this;
|
201 |
|
202 | var nums = data.pos.split('-');
|
203 | return nums.slice(1).reduce(function (ret, num, index) {
|
204 | var p = nums.slice(0, index + 2).join('-');
|
205 | ret.push(_this5._p2n[p].label);
|
206 | return ret;
|
207 | }, []);
|
208 | };
|
209 |
|
210 | CascaderSelect.prototype.getSingleData = function getSingleData(value) {
|
211 | if (!value.length) {
|
212 | return null;
|
213 | }
|
214 |
|
215 | if (Array.isArray(value)) value = value[0];
|
216 |
|
217 | var data = this._v2n[value];
|
218 |
|
219 | if (data) {
|
220 | var labelPath = this.getLabelPath(data);
|
221 | var displayRender = this.props.displayRender || function (labels) {
|
222 | return labels.join(' / ');
|
223 | };
|
224 |
|
225 | data = (0, _extends3.default)({}, data, {
|
226 | label: displayRender(labelPath, data)
|
227 | });
|
228 |
|
229 | this._valueDataCache[value] = data;
|
230 | this.refreshValueDataCache(value);
|
231 | } else {
|
232 | data = this._valueDataCache[value];
|
233 | }
|
234 |
|
235 | return data || {
|
236 | value: value
|
237 | };
|
238 | };
|
239 |
|
240 | CascaderSelect.prototype.getMultipleData = function getMultipleData(value) {
|
241 | var _this6 = this;
|
242 |
|
243 | if (!value.length) {
|
244 | return null;
|
245 | }
|
246 |
|
247 | var _props = this.props,
|
248 | checkStrictly = _props.checkStrictly,
|
249 | canOnlyCheckLeaf = _props.canOnlyCheckLeaf,
|
250 | displayRender = _props.displayRender;
|
251 |
|
252 | var flatValue = checkStrictly || canOnlyCheckLeaf ? value : this.flatValue(value);
|
253 | var data = flatValue.map(function (v) {
|
254 | var item = _this6._v2n[v];
|
255 |
|
256 | if (item) {
|
257 | _this6._valueDataCache[v] = item;
|
258 | } else {
|
259 | item = _this6._valueDataCache[v];
|
260 | }
|
261 |
|
262 | return item || { value: v };
|
263 | });
|
264 |
|
265 | if (displayRender) {
|
266 | data = data.map(function (item) {
|
267 | if (!item.pos || !(item.value in _this6._v2n)) {
|
268 | return item;
|
269 | }
|
270 |
|
271 | var labelPath = _this6.getLabelPath(item);
|
272 | var newItem = (0, _extends3.default)({}, item, {
|
273 | label: displayRender(labelPath, item)
|
274 | });
|
275 |
|
276 | _this6._valueDataCache[item.value] = newItem;
|
277 |
|
278 | return newItem;
|
279 | });
|
280 | }
|
281 |
|
282 | return data;
|
283 | };
|
284 |
|
285 | CascaderSelect.prototype.getIndeterminate = function getIndeterminate(value) {
|
286 | var _this7 = this;
|
287 |
|
288 | var indeterminate = [];
|
289 |
|
290 | var positions = value.map(this.getPos.bind(this));
|
291 | positions.forEach(function (pos) {
|
292 | if (!pos) {
|
293 | return false;
|
294 | }
|
295 | var nums = pos.split('-');
|
296 | for (var i = nums.length; i > 2; i--) {
|
297 | var parentPos = nums.slice(0, i - 1).join('-');
|
298 | var parentValue = _this7.getValue(parentPos);
|
299 | if (indeterminate.indexOf(parentValue) === -1) {
|
300 | indeterminate.push(parentValue);
|
301 | }
|
302 | }
|
303 | });
|
304 |
|
305 | return indeterminate;
|
306 | };
|
307 |
|
308 | CascaderSelect.prototype.saveSelectRef = function saveSelectRef(ref) {
|
309 | this.select = ref;
|
310 | };
|
311 |
|
312 | CascaderSelect.prototype.saveCascaderRef = function saveCascaderRef(ref) {
|
313 | this.cascader = ref;
|
314 | };
|
315 |
|
316 | CascaderSelect.prototype.completeValue = function completeValue(value) {
|
317 | var newValue = [];
|
318 |
|
319 | var flatValue = this.flatValue(value).reverse();
|
320 | var ps = Object.keys(this._p2n);
|
321 | for (var i = 0; i < ps.length; i++) {
|
322 | for (var j = 0; j < flatValue.length; j++) {
|
323 | var v = flatValue[j];
|
324 | if (this.isDescendantOrSelf(this.getPos(v), ps[i])) {
|
325 | newValue.push(this.getValue(ps[i]));
|
326 | ps.splice(i, 1);
|
327 | i--;
|
328 | break;
|
329 | }
|
330 | }
|
331 | }
|
332 |
|
333 | return newValue;
|
334 | };
|
335 |
|
336 | CascaderSelect.prototype.isLeaf = function isLeaf(data) {
|
337 | return !(data.children && data.children.length || !!this.props.loadData && !data.isLeaf);
|
338 | };
|
339 |
|
340 | CascaderSelect.prototype.handleVisibleChange = function handleVisibleChange(visible, type) {
|
341 | var _this8 = this;
|
342 |
|
343 | var searchValue = this.state.searchValue;
|
344 |
|
345 | if (!('visible' in this.props)) {
|
346 | this.setState({
|
347 | visible: visible
|
348 | });
|
349 | }
|
350 |
|
351 | if (!visible && searchValue) {
|
352 | this.setState({
|
353 | searchValue: ''
|
354 | });
|
355 | }
|
356 |
|
357 | if (['fromCascader', 'keyboard'].indexOf(type) !== -1 && !visible) {
|
358 |
|
359 | setTimeout(function () {
|
360 | return _this8.select.focusInput();
|
361 | }, 0);
|
362 | }
|
363 |
|
364 | this.props.onVisibleChange(visible, type);
|
365 | };
|
366 |
|
367 | CascaderSelect.prototype.handleKeyDown = function handleKeyDown(e) {
|
368 | var onKeyDown = this.props.onKeyDown;
|
369 | var visible = this.state.visible;
|
370 |
|
371 |
|
372 | if (onKeyDown) {
|
373 | onKeyDown(e);
|
374 | }
|
375 |
|
376 | if (!visible) {
|
377 | return;
|
378 | }
|
379 |
|
380 | switch (e.keyCode) {
|
381 | case _util.KEYCODE.UP:
|
382 | case _util.KEYCODE.DOWN:
|
383 | this.cascader.setFocusValue();
|
384 | e.preventDefault();
|
385 | break;
|
386 | default:
|
387 | break;
|
388 | }
|
389 | };
|
390 |
|
391 | CascaderSelect.prototype.getPopup = function getPopup(ref) {
|
392 | this.popup = ref;
|
393 | if (typeof this.props.popupProps.ref === 'function') {
|
394 | this.props.popupProps.ref(ref);
|
395 | }
|
396 | };
|
397 |
|
398 | CascaderSelect.prototype.handleAfterOpen = function handleAfterOpen() {
|
399 | if (!this.popup) {
|
400 | return;
|
401 | }
|
402 |
|
403 | var _props2 = this.props,
|
404 | prefix = _props2.prefix,
|
405 | popupProps = _props2.popupProps;
|
406 |
|
407 | var dropDownNode = this.popup.getInstance().overlay.getInstance().getContentNode();
|
408 | var cascaderNode = dropDownNode.querySelector('.' + prefix + 'cascader');
|
409 | if (cascaderNode) {
|
410 | this.cascaderHeight = getStyle(cascaderNode, 'height');
|
411 | }
|
412 |
|
413 | if (typeof popupProps.afterOpen === 'function') {
|
414 | popupProps.afterOpen();
|
415 | }
|
416 | };
|
417 |
|
418 | CascaderSelect.prototype.handleSelect = function handleSelect(value, data) {
|
419 | var _props3 = this.props,
|
420 | multiple = _props3.multiple,
|
421 | changeOnSelect = _props3.changeOnSelect;
|
422 | var _state = this.state,
|
423 | visible = _state.visible,
|
424 | searchValue = _state.searchValue;
|
425 |
|
426 |
|
427 | if (!multiple && (!changeOnSelect || this.isLeaf(data) || !!searchValue)) {
|
428 | this.handleVisibleChange(!visible, 'fromCascader');
|
429 | }
|
430 | };
|
431 |
|
432 | |
433 |
|
434 |
|
435 |
|
436 |
|
437 |
|
438 | CascaderSelect.prototype.handleChange = function handleChange(value, data, extra) {
|
439 | var _this9 = this;
|
440 |
|
441 | var _props4 = this.props,
|
442 | multiple = _props4.multiple,
|
443 | onChange = _props4.onChange;
|
444 | var _state2 = this.state,
|
445 | searchValue = _state2.searchValue,
|
446 | stateValue = _state2.value;
|
447 |
|
448 |
|
449 | var st = {};
|
450 |
|
451 | if (multiple && stateValue && Array.isArray(stateValue)) {
|
452 | var noExistedValues = stateValue.filter(function (v) {
|
453 | return !_this9._v2n[v];
|
454 | });
|
455 |
|
456 | value = [].concat(noExistedValues, value);
|
457 |
|
458 |
|
459 | data = [].concat(noExistedValues.map(function (v) {
|
460 | return _this9._valueDataCache[v];
|
461 | }).filter(function (v) {
|
462 | return v;
|
463 | }), data).filter(function (current, index, arr) {
|
464 | return index === arr.indexOf(current);
|
465 | });
|
466 |
|
467 | this.refreshValueDataCache(value);
|
468 | }
|
469 |
|
470 | if (!('value' in this.props)) {
|
471 | st.value = value;
|
472 | }
|
473 | if (!multiple && searchValue) {
|
474 | st.searchValue = '';
|
475 | }
|
476 | if (Object.keys(st).length) {
|
477 | this.setState(st);
|
478 | }
|
479 |
|
480 | if (onChange) {
|
481 | onChange(value, data, extra);
|
482 | }
|
483 |
|
484 | if (searchValue && this.select) {
|
485 | this.select.handleSearchClear();
|
486 | }
|
487 | };
|
488 |
|
489 | CascaderSelect.prototype.handleClear = function handleClear() {
|
490 |
|
491 | var _props5 = this.props,
|
492 | hasClear = _props5.hasClear,
|
493 | multiple = _props5.multiple,
|
494 | treeCheckable = _props5.treeCheckable;
|
495 |
|
496 | if (hasClear && (!multiple || !treeCheckable)) {
|
497 | if (!('value' in this.props)) {
|
498 | this.setState({
|
499 | value: []
|
500 | });
|
501 | }
|
502 |
|
503 | this.props.onChange(null, null);
|
504 | }
|
505 | };
|
506 |
|
507 | CascaderSelect.prototype.handleRemove = function handleRemove(currentData) {
|
508 | var currentValue = currentData.value;
|
509 |
|
510 | var value = void 0;
|
511 |
|
512 | var _props6 = this.props,
|
513 | multiple = _props6.multiple,
|
514 | checkStrictly = _props6.checkStrictly,
|
515 | onChange = _props6.onChange;
|
516 |
|
517 | if (multiple) {
|
518 | value = [].concat(this.state.value);
|
519 | value.splice(value.indexOf(currentValue), 1);
|
520 |
|
521 | if (this.props.onChange) {
|
522 | var data = this.getData(value);
|
523 | var checked = false;
|
524 |
|
525 | if (checkStrictly) {
|
526 | this.props.onChange(value, data, {
|
527 | checked: checked,
|
528 | currentData: currentData,
|
529 | checkedData: data
|
530 | });
|
531 | } else {
|
532 | var checkedValue = this.completeValue(value);
|
533 | var checkedData = this.getData(checkedValue);
|
534 | var indeterminateValue = this.getIndeterminate(value);
|
535 | var indeterminateData = this.getData(indeterminateValue);
|
536 | this.props.onChange(value, data, {
|
537 | checked: checked,
|
538 | currentData: currentData,
|
539 | checkedData: checkedData,
|
540 | indeterminateData: indeterminateData
|
541 | });
|
542 | }
|
543 | }
|
544 | } else {
|
545 | value = [];
|
546 | onChange(null, null);
|
547 | }
|
548 |
|
549 | if (!('value' in this.props)) {
|
550 | this.setState({
|
551 | value: value
|
552 | });
|
553 | }
|
554 |
|
555 | this.refreshValueDataCache(value);
|
556 | };
|
557 |
|
558 | CascaderSelect.prototype.handleSearch = function handleSearch(searchValue) {
|
559 | this.setState({
|
560 | searchValue: searchValue
|
561 | });
|
562 |
|
563 | this.props.onSearch && this.props.onSearch(searchValue);
|
564 | };
|
565 |
|
566 | CascaderSelect.prototype.getPath = function getPath(pos) {
|
567 | var items = [];
|
568 |
|
569 | var nums = pos.split('-');
|
570 | if (nums === 2) {
|
571 | items.push(this._p2n[pos]);
|
572 | } else {
|
573 | for (var i = 1; i < nums.length; i++) {
|
574 | var p = nums.slice(0, i + 1).join('-');
|
575 | items.push(this._p2n[p]);
|
576 | }
|
577 | }
|
578 |
|
579 | return items;
|
580 | };
|
581 |
|
582 | CascaderSelect.prototype.filterItems = function filterItems() {
|
583 | var _this10 = this;
|
584 |
|
585 | var _props7 = this.props,
|
586 | multiple = _props7.multiple,
|
587 | changeOnSelect = _props7.changeOnSelect,
|
588 | canOnlyCheckLeaf = _props7.canOnlyCheckLeaf,
|
589 | filter = _props7.filter;
|
590 | var searchValue = this.state.searchValue;
|
591 |
|
592 | var items = Object.keys(this._p2n).map(function (p) {
|
593 | return _this10._p2n[p];
|
594 | });
|
595 | if (!multiple && !changeOnSelect || multiple && canOnlyCheckLeaf) {
|
596 | items = items.filter(function (item) {
|
597 | return !item.children || !item.children.length;
|
598 | });
|
599 | }
|
600 |
|
601 | return items.map(function (item) {
|
602 | return _this10.getPath(item.pos);
|
603 | }).filter(function (path) {
|
604 | return filter(searchValue, path);
|
605 | });
|
606 | };
|
607 |
|
608 | CascaderSelect.prototype.renderNotFound = function renderNotFound() {
|
609 | var _props8 = this.props,
|
610 | prefix = _props8.prefix,
|
611 | notFoundContent = _props8.notFoundContent;
|
612 |
|
613 |
|
614 | return _react2.default.createElement(
|
615 | _menu2.default,
|
616 | { className: prefix + 'cascader-select-not-found' },
|
617 | _react2.default.createElement(
|
618 | _menu2.default.Item,
|
619 | null,
|
620 | notFoundContent
|
621 | )
|
622 | );
|
623 | };
|
624 |
|
625 | CascaderSelect.prototype.renderCascader = function renderCascader() {
|
626 | var dataSource = this.props.dataSource;
|
627 |
|
628 | if (dataSource.length === 0) {
|
629 | return this.renderNotFound();
|
630 | }
|
631 |
|
632 | var searchValue = this.state.searchValue;
|
633 |
|
634 | var filteredPaths = [];
|
635 |
|
636 | if (searchValue) {
|
637 | filteredPaths = this.filterItems();
|
638 | if (filteredPaths.length === 0) {
|
639 | return this.renderNotFound();
|
640 | }
|
641 | }
|
642 |
|
643 | var _props9 = this.props,
|
644 | multiple = _props9.multiple,
|
645 | useVirtual = _props9.useVirtual,
|
646 | changeOnSelect = _props9.changeOnSelect,
|
647 | checkStrictly = _props9.checkStrictly,
|
648 | canOnlyCheckLeaf = _props9.canOnlyCheckLeaf,
|
649 | defaultExpandedValue = _props9.defaultExpandedValue,
|
650 | expandTriggerType = _props9.expandTriggerType,
|
651 | onExpand = _props9.onExpand,
|
652 | listStyle = _props9.listStyle,
|
653 | listClassName = _props9.listClassName,
|
654 | loadData = _props9.loadData,
|
655 | showSearch = _props9.showSearch,
|
656 | resultRender = _props9.resultRender,
|
657 | readOnly = _props9.readOnly,
|
658 | itemRender = _props9.itemRender,
|
659 | immutable = _props9.immutable,
|
660 | _props9$menuProps = _props9.menuProps,
|
661 | menuProps = _props9$menuProps === undefined ? {} : _props9$menuProps;
|
662 | var value = this.state.value;
|
663 |
|
664 |
|
665 | var props = {
|
666 | dataSource: dataSource,
|
667 | value: value,
|
668 | multiple: multiple,
|
669 | useVirtual: useVirtual,
|
670 | canOnlySelectLeaf: !changeOnSelect,
|
671 | checkStrictly: checkStrictly,
|
672 | canOnlyCheckLeaf: canOnlyCheckLeaf,
|
673 | defaultExpandedValue: defaultExpandedValue,
|
674 | expandTriggerType: expandTriggerType,
|
675 | ref: this.saveCascaderRef,
|
676 | onExpand: onExpand,
|
677 | listStyle: listStyle,
|
678 | listClassName: listClassName,
|
679 | loadData: loadData,
|
680 | itemRender: itemRender,
|
681 | immutable: immutable
|
682 | };
|
683 |
|
684 | if ('expandedValue' in this.props) {
|
685 | props.expandedValue = this.props.expandedValue;
|
686 | }
|
687 |
|
688 | if (!readOnly) {
|
689 | props.onChange = this.handleChange;
|
690 | props.onSelect = this.handleSelect;
|
691 | }
|
692 | if (showSearch) {
|
693 | props.searchValue = searchValue;
|
694 | props.filteredPaths = filteredPaths;
|
695 | props.resultRender = resultRender;
|
696 | props.filteredListStyle = { height: this.cascaderHeight };
|
697 | }
|
698 |
|
699 | return _react2.default.createElement(_cascader2.default, (0, _extends3.default)({}, props, menuProps));
|
700 | };
|
701 |
|
702 | CascaderSelect.prototype.renderPopupContent = function renderPopupContent() {
|
703 | var _props10 = this.props,
|
704 | prefix = _props10.prefix,
|
705 | header = _props10.header,
|
706 | footer = _props10.footer;
|
707 |
|
708 | return _react2.default.createElement(
|
709 | 'div',
|
710 | { className: prefix + 'cascader-select-dropdown' },
|
711 | header,
|
712 | this.renderCascader(),
|
713 | footer
|
714 | );
|
715 | };
|
716 |
|
717 | CascaderSelect.prototype.renderPreview = function renderPreview(others) {
|
718 | var _props11 = this.props,
|
719 | prefix = _props11.prefix,
|
720 | multiple = _props11.multiple,
|
721 | className = _props11.className,
|
722 | renderPreview = _props11.renderPreview;
|
723 | var value = this.state.value;
|
724 |
|
725 | var previewCls = (0, _classnames2.default)(className, prefix + 'form-preview');
|
726 | var items = (multiple ? this.getMultipleData(value) : this.getSingleData(value)) || [];
|
727 |
|
728 | if (!Array.isArray(items)) {
|
729 | items = [items];
|
730 | }
|
731 |
|
732 | if (typeof renderPreview === 'function') {
|
733 | return _react2.default.createElement(
|
734 | 'div',
|
735 | (0, _extends3.default)({}, others, { className: previewCls }),
|
736 | renderPreview(items, this.props)
|
737 | );
|
738 | }
|
739 |
|
740 | return _react2.default.createElement(
|
741 | 'p',
|
742 | (0, _extends3.default)({}, others, { className: previewCls }),
|
743 | items.map(function (_ref) {
|
744 | var label = _ref.label;
|
745 | return label;
|
746 | }).join(', ')
|
747 | );
|
748 | };
|
749 |
|
750 | CascaderSelect.prototype.render = function render() {
|
751 | var _props12 = this.props,
|
752 | prefix = _props12.prefix,
|
753 | size = _props12.size,
|
754 | hasArrow = _props12.hasArrow,
|
755 | hasBorder = _props12.hasBorder,
|
756 | hasClear = _props12.hasClear,
|
757 | label = _props12.label,
|
758 | readOnly = _props12.readOnly,
|
759 | placeholder = _props12.placeholder,
|
760 | dataSource = _props12.dataSource,
|
761 | disabled = _props12.disabled,
|
762 | multiple = _props12.multiple,
|
763 | className = _props12.className,
|
764 | showSearch = _props12.showSearch,
|
765 | popupStyle = _props12.popupStyle,
|
766 | popupClassName = _props12.popupClassName,
|
767 | popupContainer = _props12.popupContainer,
|
768 | popupProps = _props12.popupProps,
|
769 | followTrigger = _props12.followTrigger,
|
770 | isPreview = _props12.isPreview,
|
771 | resultAutoWidth = _props12.resultAutoWidth;
|
772 | var _state3 = this.state,
|
773 | value = _state3.value,
|
774 | searchValue = _state3.searchValue,
|
775 | visible = _state3.visible;
|
776 |
|
777 | var others = pickOthers(Object.keys(CascaderSelect.propTypes), this.props);
|
778 |
|
779 | this.updateCache(dataSource);
|
780 |
|
781 | if (isPreview) {
|
782 | return this.renderPreview(others);
|
783 | }
|
784 |
|
785 | var popupContent = this.renderPopupContent();
|
786 |
|
787 | var props = {
|
788 | prefix: prefix,
|
789 | className: className,
|
790 | size: size,
|
791 | placeholder: placeholder,
|
792 | disabled: disabled,
|
793 | hasArrow: hasArrow,
|
794 | hasBorder: hasBorder,
|
795 | hasClear: hasClear,
|
796 | label: label,
|
797 | readOnly: readOnly,
|
798 | ref: this.saveSelectRef,
|
799 | autoWidth: false,
|
800 | mode: multiple ? 'multiple' : 'single',
|
801 | value: multiple ? this.getMultipleData(value) : this.getSingleData(value),
|
802 | onChange: this.handleClear,
|
803 | onRemove: this.handleRemove,
|
804 | visible: visible,
|
805 | onVisibleChange: this.handleVisibleChange,
|
806 | showSearch: showSearch,
|
807 | onSearch: this.handleSearch,
|
808 | onKeyDown: this.handleKeyDown,
|
809 | popupContent: popupContent,
|
810 | popupStyle: popupStyle,
|
811 | popupClassName: popupClassName,
|
812 | popupContainer: popupContainer,
|
813 | popupProps: popupProps,
|
814 | followTrigger: followTrigger
|
815 | };
|
816 |
|
817 | if (showSearch) {
|
818 | props.popupProps = (0, _extends3.default)({}, popupProps, {
|
819 | ref: this.getPopup,
|
820 | afterOpen: this.handleAfterOpen
|
821 | });
|
822 | props.autoWidth = resultAutoWidth && !!searchValue;
|
823 | }
|
824 |
|
825 | return _react2.default.createElement(_select2.default, (0, _extends3.default)({}, props, others));
|
826 | };
|
827 |
|
828 | return CascaderSelect;
|
829 | }(_react.Component), _class.propTypes = {
|
830 | prefix: _propTypes2.default.string,
|
831 | pure: _propTypes2.default.bool,
|
832 | className: _propTypes2.default.string,
|
833 | |
834 |
|
835 |
|
836 | size: _propTypes2.default.oneOf(['small', 'medium', 'large']),
|
837 | |
838 |
|
839 |
|
840 | placeholder: _propTypes2.default.string,
|
841 | |
842 |
|
843 |
|
844 | disabled: _propTypes2.default.bool,
|
845 | |
846 |
|
847 |
|
848 | hasArrow: _propTypes2.default.bool,
|
849 | |
850 |
|
851 |
|
852 | hasBorder: _propTypes2.default.bool,
|
853 | |
854 |
|
855 |
|
856 | hasClear: _propTypes2.default.bool,
|
857 | |
858 |
|
859 |
|
860 | label: _propTypes2.default.node,
|
861 | |
862 |
|
863 |
|
864 | readOnly: _propTypes2.default.bool,
|
865 | |
866 |
|
867 |
|
868 | dataSource: _propTypes2.default.arrayOf(_propTypes2.default.object),
|
869 | |
870 |
|
871 |
|
872 | defaultValue: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.arrayOf(_propTypes2.default.string)]),
|
873 | |
874 |
|
875 |
|
876 | value: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.arrayOf(_propTypes2.default.string)]),
|
877 | |
878 |
|
879 |
|
880 |
|
881 |
|
882 |
|
883 |
|
884 |
|
885 |
|
886 |
|
887 |
|
888 | onChange: _propTypes2.default.func,
|
889 | |
890 |
|
891 |
|
892 | defaultExpandedValue: _propTypes2.default.arrayOf(_propTypes2.default.string),
|
893 | |
894 |
|
895 |
|
896 | expandedValue: _propTypes2.default.arrayOf(_propTypes2.default.string),
|
897 | |
898 |
|
899 |
|
900 | expandTriggerType: _propTypes2.default.oneOf(['click', 'hover']),
|
901 | onExpand: _propTypes2.default.func,
|
902 | |
903 |
|
904 |
|
905 | useVirtual: _propTypes2.default.bool,
|
906 | |
907 |
|
908 |
|
909 | multiple: _propTypes2.default.bool,
|
910 | |
911 |
|
912 |
|
913 | changeOnSelect: _propTypes2.default.bool,
|
914 | |
915 |
|
916 |
|
917 | canOnlyCheckLeaf: _propTypes2.default.bool,
|
918 | |
919 |
|
920 |
|
921 | checkStrictly: _propTypes2.default.bool,
|
922 | |
923 |
|
924 |
|
925 | listStyle: _propTypes2.default.object,
|
926 | |
927 |
|
928 |
|
929 | listClassName: _propTypes2.default.string,
|
930 | |
931 |
|
932 |
|
933 |
|
934 |
|
935 |
|
936 | displayRender: _propTypes2.default.func,
|
937 | |
938 |
|
939 |
|
940 |
|
941 |
|
942 | itemRender: _propTypes2.default.func,
|
943 | |
944 |
|
945 |
|
946 | showSearch: _propTypes2.default.bool,
|
947 | |
948 |
|
949 |
|
950 |
|
951 |
|
952 |
|
953 |
|
954 | filter: _propTypes2.default.func,
|
955 | |
956 |
|
957 |
|
958 |
|
959 |
|
960 | onSearch: _propTypes2.default.func,
|
961 | |
962 |
|
963 |
|
964 |
|
965 |
|
966 |
|
967 |
|
968 | resultRender: _propTypes2.default.func,
|
969 | |
970 |
|
971 |
|
972 | resultAutoWidth: _propTypes2.default.bool,
|
973 | |
974 |
|
975 |
|
976 | notFoundContent: _propTypes2.default.node,
|
977 | |
978 |
|
979 |
|
980 |
|
981 | loadData: _propTypes2.default.func,
|
982 | |
983 |
|
984 |
|
985 | header: _propTypes2.default.node,
|
986 | |
987 |
|
988 |
|
989 | footer: _propTypes2.default.node,
|
990 | |
991 |
|
992 |
|
993 | defaultVisible: _propTypes2.default.bool,
|
994 | |
995 |
|
996 |
|
997 | visible: _propTypes2.default.bool,
|
998 | |
999 |
|
1000 |
|
1001 |
|
1002 |
|
1003 | onVisibleChange: _propTypes2.default.func,
|
1004 | |
1005 |
|
1006 |
|
1007 | popupStyle: _propTypes2.default.object,
|
1008 | |
1009 |
|
1010 |
|
1011 | popupClassName: _propTypes2.default.string,
|
1012 | |
1013 |
|
1014 |
|
1015 | popupContainer: _propTypes2.default.any,
|
1016 | |
1017 |
|
1018 |
|
1019 | popupProps: _propTypes2.default.object,
|
1020 | |
1021 |
|
1022 |
|
1023 | followTrigger: _propTypes2.default.bool,
|
1024 | |
1025 |
|
1026 |
|
1027 | isPreview: _propTypes2.default.bool,
|
1028 | |
1029 |
|
1030 |
|
1031 |
|
1032 | renderPreview: _propTypes2.default.func,
|
1033 | |
1034 |
|
1035 |
|
1036 |
|
1037 | immutable: _propTypes2.default.bool
|
1038 | }, _class.defaultProps = {
|
1039 | prefix: 'next-',
|
1040 | pure: false,
|
1041 | size: 'medium',
|
1042 | disabled: false,
|
1043 | hasArrow: true,
|
1044 | hasBorder: true,
|
1045 | hasClear: false,
|
1046 | dataSource: [],
|
1047 | defaultValue: null,
|
1048 | expandTriggerType: 'click',
|
1049 | onExpand: function onExpand() {},
|
1050 | useVirtual: false,
|
1051 | multiple: false,
|
1052 | changeOnSelect: false,
|
1053 | canOnlyCheckLeaf: false,
|
1054 | checkStrictly: false,
|
1055 | showSearch: false,
|
1056 | filter: function filter(searchValue, path) {
|
1057 | return path.some(function (item) {
|
1058 | return String(item.label).toLowerCase().indexOf(String(searchValue).toLowerCase()) > -1;
|
1059 | });
|
1060 | },
|
1061 | resultRender: function resultRender(searchValue, path) {
|
1062 | var parts = [];
|
1063 | path.forEach(function (item, i) {
|
1064 | var reExp = searchValue.replace(/[-.+*?^$()[\]{}|\\]/g, function (v) {
|
1065 | return '\\' + v;
|
1066 | });
|
1067 |
|
1068 | var re = new RegExp(reExp, 'gi');
|
1069 | var others = item.label.split(re);
|
1070 | var matches = item.label.match(re);
|
1071 |
|
1072 | others.forEach(function (other, j) {
|
1073 | if (other) {
|
1074 | parts.push(other);
|
1075 | }
|
1076 | if (j < others.length - 1) {
|
1077 | parts.push(_react2.default.createElement(
|
1078 | 'em',
|
1079 | { key: i + '-' + j },
|
1080 | matches[j]
|
1081 | ));
|
1082 | }
|
1083 | });
|
1084 | if (i < path.length - 1) {
|
1085 | parts.push(' / ');
|
1086 | }
|
1087 | });
|
1088 | return _react2.default.createElement(
|
1089 | 'span',
|
1090 | null,
|
1091 | parts
|
1092 | );
|
1093 | },
|
1094 | resultAutoWidth: true,
|
1095 | notFoundContent: 'Not Found',
|
1096 | defaultVisible: false,
|
1097 | onVisibleChange: function onVisibleChange() {},
|
1098 | popupProps: {},
|
1099 | immutable: false
|
1100 | }, _temp);
|
1101 | CascaderSelect.displayName = 'CascaderSelect';
|
1102 | exports.default = (0, _reactLifecyclesCompat.polyfill)(CascaderSelect);
|
1103 | module.exports = exports['default']; |
\ | No newline at end of file |