UNPKG

38.3 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
6
7var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
8
9var _typeof2 = require('babel-runtime/helpers/typeof');
10
11var _typeof3 = _interopRequireDefault(_typeof2);
12
13var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
14
15var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
16
17var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
18
19var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
20
21var _inherits2 = require('babel-runtime/helpers/inherits');
22
23var _inherits3 = _interopRequireDefault(_inherits2);
24
25var _extends2 = require('babel-runtime/helpers/extends');
26
27var _extends3 = _interopRequireDefault(_extends2);
28
29var _class, _temp;
30
31var _react = require('react');
32
33var _react2 = _interopRequireDefault(_react);
34
35var _reactLifecyclesCompat = require('react-lifecycles-compat');
36
37var _propTypes = require('prop-types');
38
39var _propTypes2 = _interopRequireDefault(_propTypes);
40
41var _classnames = require('classnames');
42
43var _classnames2 = _interopRequireDefault(_classnames);
44
45var _select = require('../select');
46
47var _select2 = _interopRequireDefault(_select);
48
49var _tree = require('../tree');
50
51var _tree2 = _interopRequireDefault(_tree);
52
53var _util = require('../tree/view/util');
54
55var _util2 = require('../util');
56
57var _zhCn = require('../locale/zh-cn');
58
59var _zhCn2 = _interopRequireDefault(_zhCn);
60
61var _util3 = require('../select/util');
62
63function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
64
65var noop = function noop() {};
66var TreeNode = _tree2.default.Node;
67var bindCtx = _util2.func.bindCtx;
68
69
70var flatDataSource = function flatDataSource(props) {
71 var _k2n = {};
72 var _p2n = {};
73 var _v2n = {};
74
75 if ('dataSource' in props) {
76 var loop = function loop(data) {
77 var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '0';
78 return data.map(function (item, index) {
79 var value = item.value,
80 children = item.children;
81
82 var pos = prefix + '-' + index;
83 var key = item.key || pos;
84
85 var newItem = (0, _extends3.default)({}, item, { key: key, pos: pos });
86 if (children && children.length) {
87 newItem.children = loop(children, pos);
88 }
89
90 _k2n[key] = _p2n[pos] = _v2n[value] = newItem;
91 return newItem;
92 });
93 };
94
95 loop(props.dataSource);
96 } else if ('children' in props) {
97 var _loop = function _loop(children) {
98 var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '0';
99 return _react.Children.map(children, function (node, index) {
100 if (!_react2.default.isValidElement(node)) {
101 return;
102 }
103
104 var _node$props = node.props,
105 value = _node$props.value,
106 children = _node$props.children;
107
108 var pos = prefix + '-' + index;
109 var key = node.key || pos;
110 var newItem = (0, _extends3.default)({}, node.props, { key: key, pos: pos });
111 if (children && _react.Children.count(children)) {
112 newItem.children = _loop(children, pos);
113 }
114
115 _k2n[key] = _p2n[pos] = _v2n[value] = newItem;
116 return newItem;
117 });
118 };
119 _loop(props.children);
120 }
121
122 return { _k2n: _k2n, _p2n: _p2n, _v2n: _v2n };
123};
124
125var isSearched = function isSearched(label, searchedValue) {
126 var labelString = '';
127
128 searchedValue = String(searchedValue);
129
130 var loop = function loop(arg) {
131 if ((0, _react.isValidElement)(arg) && arg.props.children) {
132 _react.Children.forEach(arg.props.children, loop);
133 } else {
134 labelString += arg;
135 }
136 };
137 loop(label);
138
139 if (labelString.length >= searchedValue.length && labelString.toLowerCase().indexOf(searchedValue.toLowerCase()) > -1) {
140 return true;
141 }
142
143 return false;
144};
145
146var getSearchKeys = function getSearchKeys(searchedValue, _k2n, _p2n) {
147 var searchedKeys = [];
148 var retainedKeys = [];
149 Object.keys(_k2n).forEach(function (k) {
150 var _k2n$k = _k2n[k],
151 label = _k2n$k.label,
152 pos = _k2n$k.pos;
153
154
155 if (isSearched(label, searchedValue)) {
156 searchedKeys.push(k);
157 var posArr = pos.split('-');
158 posArr.forEach(function (n, i) {
159 if (i > 0) {
160 var p = posArr.slice(0, i + 1).join('-');
161 var kk = _p2n[p].key;
162 if (retainedKeys.indexOf(kk) === -1) {
163 retainedKeys.push(kk);
164 }
165 }
166 });
167 }
168 });
169
170 return { searchedKeys: searchedKeys, retainedKeys: retainedKeys };
171};
172
173/**
174 * TreeSelect
175 */
176var TreeSelect = (_temp = _class = function (_Component) {
177 (0, _inherits3.default)(TreeSelect, _Component);
178
179 function TreeSelect(props, context) {
180 (0, _classCallCheck3.default)(this, TreeSelect);
181
182 var _this = (0, _possibleConstructorReturn3.default)(this, _Component.call(this, props, context));
183
184 var defaultVisible = props.defaultVisible,
185 visible = props.visible,
186 defaultValue = props.defaultValue,
187 value = props.value;
188
189
190 _this.state = (0, _extends3.default)({
191 visible: typeof visible === 'undefined' ? defaultVisible : visible,
192 value: (0, _util.normalizeToArray)(typeof value === 'undefined' ? defaultValue : value),
193 searchedValue: '',
194 expandedKeys: [],
195 searchedKeys: [],
196 retainedKeys: [],
197 autoExpandParent: false,
198 // map of value => item, includes value not exist in dataSource
199 mapValueDS: {}
200 }, flatDataSource(props));
201
202 // init value/mapValueDS when defaultValue is not undefined
203 if (_this.state.value !== undefined) {
204 _this.state.mapValueDS = (0, _util3.getValueDataSource)(_this.state.value, _this.state.mapValueDS).mapValueDS;
205 _this.state.value = _this.state.value.map(function (v) {
206 return (0, _util3.valueToSelectKey)(v);
207 });
208 }
209
210 bindCtx(_this, ['handleSelect', 'handleCheck', 'handleSearch', 'handleSearchClear', 'handleVisibleChange', 'handleChange', 'handleRemove', 'handleExpand', 'handleKeyDown', 'saveTreeRef', 'saveSelectRef', 'renderMaxTagPlaceholder']);
211 return _this;
212 }
213
214 TreeSelect.getDerivedStateFromProps = function getDerivedStateFromProps(props, state) {
215 var st = {};
216
217 if ('value' in props) {
218 var valueArray = (0, _util.normalizeToArray)(props.value);
219 // convert value to string[]
220 st.value = valueArray.map(function (v) {
221 return (0, _util3.valueToSelectKey)(v);
222 });
223 // re-calculate map
224 st.mapValueDS = (0, _util3.getValueDataSource)(props.value, state.mapValueDS).mapValueDS;
225 }
226 if ('visible' in props) {
227 st.visible = props.visible;
228 }
229
230 var _flatDataSource = flatDataSource(props),
231 _k2n = _flatDataSource._k2n,
232 _p2n = _flatDataSource._p2n,
233 _v2n = _flatDataSource._v2n;
234
235 if (props.showSearch && state.searchedValue) {
236 var _getSearchKeys = getSearchKeys(state.searchedValue, _k2n, _p2n),
237 searchedKeys = _getSearchKeys.searchedKeys,
238 retainedKeys = _getSearchKeys.retainedKeys;
239
240 st.searchedKeys = searchedKeys;
241 st.retainedKeys = retainedKeys;
242 }
243
244 return (0, _extends3.default)({}, st, {
245 _k2n: _k2n,
246 _p2n: _p2n,
247 _v2n: _v2n
248 });
249 };
250
251 TreeSelect.prototype.getKeysByValue = function getKeysByValue(value) {
252 var _this2 = this;
253
254 return value.reduce(function (ret, v) {
255 var k = _this2.state._v2n[v] && _this2.state._v2n[v].key;
256 if (k) {
257 ret.push(k);
258 }
259 return ret;
260 }, []);
261 };
262
263 TreeSelect.prototype.getValueByKeys = function getValueByKeys(keys) {
264 var _this3 = this;
265
266 return keys.map(function (k) {
267 return _this3.state._k2n[k].value;
268 });
269 };
270
271 TreeSelect.prototype.getValueForSelect = function getValueForSelect(value) {
272 var treeCheckedStrategy = this.props.treeCheckedStrategy;
273
274 var nonExistentValueKeys = this.getNonExistentValueKeys();
275
276 var keys = this.getKeysByValue(value);
277
278 if (treeCheckedStrategy !== 'child') {
279 // there's no need to calculate all checked value when treeCheckedStrategy=child, close #3936
280 keys = (0, _util.getAllCheckedKeys)(keys, this.state._k2n, this.state._p2n);
281 }
282
283 switch (treeCheckedStrategy) {
284 case 'parent':
285 keys = (0, _util.filterChildKey)(keys, this.state._k2n, this.state._p2n);
286 break;
287 case 'child':
288 keys = (0, _util.filterParentKey)(keys, this.state._k2n, this.state._p2n);
289 break;
290 default:
291 break;
292 }
293
294 var values = this.getValueByKeys(keys);
295
296 return [].concat(values, nonExistentValueKeys);
297 };
298
299 TreeSelect.prototype.getData = function getData(value, forSelect) {
300 var _this4 = this;
301
302 var preserveNonExistentValue = this.props.preserveNonExistentValue;
303 var mapValueDS = this.state.mapValueDS;
304
305
306 var ret = value.reduce(function (ret, v) {
307 var k = _this4.state._v2n[v] && _this4.state._v2n[v].key;
308 if (k) {
309 var _state$_k2n$k = _this4.state._k2n[k],
310 label = _state$_k2n$k.label,
311 pos = _state$_k2n$k.pos,
312 disabled = _state$_k2n$k.disabled,
313 checkboxDisabled = _state$_k2n$k.checkboxDisabled;
314
315 var d = {
316 value: v,
317 label: label,
318 pos: pos
319 };
320 if (forSelect) {
321 d.disabled = disabled || checkboxDisabled;
322 } else {
323 d.key = k;
324 }
325 ret.push(d);
326 } else if (preserveNonExistentValue) {
327 // 需要保留 dataSource 中不存在的 value
328 var item = mapValueDS[v];
329 if (item) {
330 ret.push(item);
331 }
332 }
333 return ret;
334 }, []);
335
336 return ret;
337 };
338
339 TreeSelect.prototype.getNonExistentValues = function getNonExistentValues() {
340 var _this5 = this;
341
342 var preserveNonExistentValue = this.props.preserveNonExistentValue;
343 var value = this.state.value;
344
345
346 if (!preserveNonExistentValue) {
347 return [];
348 }
349 var nonExistentValues = value.filter(function (v) {
350 return !_this5.state._v2n[v];
351 });
352 return nonExistentValues;
353 };
354
355 TreeSelect.prototype.getNonExistentValueKeys = function getNonExistentValueKeys() {
356 var nonExistentValues = this.getNonExistentValues();
357 return nonExistentValues.map(function (v) {
358 if ((typeof v === 'undefined' ? 'undefined' : (0, _typeof3.default)(v)) === 'object' && v.hasOwnProperty('value')) {
359 return v.value;
360 }
361 return v;
362 });
363 };
364
365 TreeSelect.prototype.saveTreeRef = function saveTreeRef(ref) {
366 this.tree = ref;
367 };
368
369 TreeSelect.prototype.saveSelectRef = function saveSelectRef(ref) {
370 this.select = ref;
371 };
372
373 TreeSelect.prototype.handleVisibleChange = function handleVisibleChange(visible, type) {
374 if (!('visible' in this.props)) {
375 this.setState({
376 visible: visible
377 });
378 }
379
380 if (['fromTree', 'keyboard'].indexOf(type) !== -1 && !visible) {
381 this.select.focusInput();
382 }
383
384 this.props.onVisibleChange(visible, type);
385 };
386
387 TreeSelect.prototype.handleSelect = function handleSelect(selectedKeys, extra) {
388 var _props = this.props,
389 multiple = _props.multiple,
390 onChange = _props.onChange;
391 var selected = extra.selected;
392
393
394 if (multiple || selected) {
395 var value = this.getValueByKeys(selectedKeys);
396 var nonExistentValues = this.getNonExistentValues();
397 value = [].concat(nonExistentValues, value);
398
399 if (!('value' in this.props)) {
400 this.setState({
401 value: value
402 });
403 }
404 if (!multiple) {
405 this.handleVisibleChange(false, 'fromTree');
406 }
407
408 var data = this.getData(value);
409 multiple ? onChange(value, data) : onChange(value[0], data[0]);
410
411 // clear search value manually
412 this.select.handleSearchClear('select');
413 } else {
414 this.handleVisibleChange(false, 'fromTree');
415 }
416 };
417
418 TreeSelect.prototype.handleCheck = function handleCheck(checkedKeys) {
419 var onChange = this.props.onChange;
420
421
422 var value = this.getValueByKeys(checkedKeys);
423 var nonExistentValues = this.getNonExistentValues();
424 value = [].concat(nonExistentValues, value);
425
426 if (!('value' in this.props)) {
427 this.setState({
428 value: value
429 });
430 }
431
432 onChange(value, this.getData(value));
433
434 // clear search value manually
435 this.select.handleSearchClear('select');
436 };
437
438 TreeSelect.prototype.handleRemove = function handleRemove(removedItem) {
439 var _this6 = this;
440
441 var removedValue = removedItem.value;
442 var _props2 = this.props,
443 treeCheckable = _props2.treeCheckable,
444 treeCheckStrictly = _props2.treeCheckStrictly,
445 treeCheckedStrategy = _props2.treeCheckedStrategy,
446 onChange = _props2.onChange;
447
448
449 var value = void 0;
450 if (
451 // there's linkage relationship among nodes
452 treeCheckable && !treeCheckStrictly && ['parent', 'all'].indexOf(treeCheckedStrategy) !== -1 &&
453 // value exits in datasource
454 this.state._v2n[removedValue]) {
455 var removedPos = this.state._v2n[removedValue].pos;
456 value = this.state.value.filter(function (v) {
457 var p = _this6.state._v2n[v].pos;
458 return !(0, _util.isDescendantOrSelf)(removedPos, p);
459 });
460
461 var nums = removedPos.split('-');
462 for (var i = nums.length; i > 2; i--) {
463 var parentPos = nums.slice(0, i - 1).join('-');
464 var parentValue = this.state._p2n[parentPos].value;
465 var parentIndex = value.indexOf(parentValue);
466 if (parentIndex > -1) {
467 value.splice(parentIndex, 1);
468 } else {
469 break;
470 }
471 }
472 } else {
473 value = this.state.value.filter(function (v) {
474 return v !== removedValue;
475 });
476 }
477
478 if (!('value' in this.props)) {
479 this.setState({
480 value: value
481 });
482 }
483
484 var data = this.getData(value);
485 onChange(value, data);
486 };
487
488 TreeSelect.prototype.handleSearch = function handleSearch(searchedValue) {
489 var _state = this.state,
490 _k2n = _state._k2n,
491 _p2n = _state._p2n;
492
493 var _getSearchKeys2 = getSearchKeys(searchedValue, _k2n, _p2n),
494 searchedKeys = _getSearchKeys2.searchedKeys,
495 retainedKeys = _getSearchKeys2.retainedKeys;
496
497 this.setState({
498 searchedValue: searchedValue,
499 expandedKeys: searchedKeys,
500 searchedKeys: searchedKeys,
501 retainedKeys: retainedKeys,
502 autoExpandParent: true
503 });
504
505 this.props.onSearch(searchedValue);
506 };
507
508 TreeSelect.prototype.handleSearchClear = function handleSearchClear(triggerType) {
509 this.setState({
510 searchedValue: '',
511 expandedKeys: []
512 });
513 this.props.onSearchClear(triggerType);
514 };
515
516 TreeSelect.prototype.handleExpand = function handleExpand(expandedKeys) {
517 this.setState({
518 expandedKeys: expandedKeys,
519 autoExpandParent: false
520 });
521 };
522
523 TreeSelect.prototype.handleKeyDown = function handleKeyDown(e) {
524 var onKeyDown = this.props.onKeyDown;
525 var visible = this.state.visible;
526
527
528 if (onKeyDown) {
529 onKeyDown(e);
530 }
531
532 if (!visible) {
533 return;
534 }
535
536 switch (e.keyCode) {
537 case _util2.KEYCODE.UP:
538 case _util2.KEYCODE.DOWN:
539 this.tree.setFocusKey();
540 e.preventDefault();
541 break;
542 default:
543 break;
544 }
545 };
546
547 TreeSelect.prototype.handleChange = function handleChange(value, triggerType) {
548 if (this.props.hasClear && triggerType === 'clear') {
549 if (!('value' in this.props)) {
550 this.setState({
551 value: []
552 });
553 }
554
555 this.props.onChange(null, null);
556 }
557 };
558
559 TreeSelect.prototype.searchNodes = function searchNodes(children) {
560 var _state2 = this.state,
561 searchedKeys = _state2.searchedKeys,
562 retainedKeys = _state2.retainedKeys;
563
564
565 var loop = function loop(children) {
566 var retainedNodes = [];
567 _react.Children.forEach(children, function (child) {
568 if (searchedKeys.indexOf(child.key) > -1) {
569 retainedNodes.push(child);
570 } else if (retainedKeys.indexOf(child.key) > -1) {
571 var retainedNode = child.props.children ? (0, _react.cloneElement)(child, {}, loop(child.props.children)) : child;
572 retainedNodes.push(retainedNode);
573 } else {
574 var hideNode = (0, _react.cloneElement)(child, {
575 style: { display: 'none' }
576 });
577 retainedNodes.push(hideNode);
578 }
579 });
580 return retainedNodes;
581 };
582
583 return loop(children);
584 };
585
586 TreeSelect.prototype.createNodesByData = function createNodesByData(data, searching) {
587 var _this7 = this;
588
589 var _state3 = this.state,
590 searchedKeys = _state3.searchedKeys,
591 retainedKeys = _state3.retainedKeys;
592
593
594 var loop = function loop(data, isParentMatched) {
595 var prefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '0';
596
597 var retainedNodes = [];
598
599 data.forEach(function (item, index) {
600 var children = item.children,
601 others = (0, _objectWithoutProperties3.default)(item, ['children']);
602
603 var pos = prefix + '-' + index;
604 var key = _this7.state._p2n[pos].key;
605 var addNode = function addNode(isParentMatched, hide) {
606 if (hide) {
607 others.style = { display: 'none' };
608 }
609
610 retainedNodes.push(_react2.default.createElement(
611 TreeNode,
612 (0, _extends3.default)({}, others, { key: key }),
613 children && children.length ? loop(children, isParentMatched, pos) : null
614 ));
615 };
616
617 if (searching) {
618 if (searchedKeys.indexOf(key) > -1 || isParentMatched) {
619 addNode(true);
620 } else if (retainedKeys.indexOf(key) > -1) {
621 addNode(false);
622 } else {
623 addNode(false, true);
624 }
625 } else {
626 addNode();
627 }
628 });
629
630 return retainedNodes;
631 };
632
633 return loop(data, false);
634 };
635
636 /*eslint-disable max-statements*/
637
638
639 TreeSelect.prototype.renderPopupContent = function renderPopupContent() {
640 var prefix = this.props.prefix;
641 var treeSelectPrefix = prefix + 'tree-select-';
642
643 if (!this.state.visible) {
644 return _react2.default.createElement('div', { className: treeSelectPrefix + 'dropdown' });
645 }
646
647 var _props3 = this.props,
648 multiple = _props3.multiple,
649 treeCheckable = _props3.treeCheckable,
650 treeCheckStrictly = _props3.treeCheckStrictly,
651 treeCheckedStrategy = _props3.treeCheckedStrategy,
652 treeDefaultExpandAll = _props3.treeDefaultExpandAll,
653 treeDefaultExpandedKeys = _props3.treeDefaultExpandedKeys,
654 treeLoadData = _props3.treeLoadData,
655 _props3$treeProps = _props3.treeProps,
656 customTreeProps = _props3$treeProps === undefined ? {} : _props3$treeProps,
657 showSearch = _props3.showSearch,
658 filterLocal = _props3.filterLocal,
659 dataSource = _props3.dataSource,
660 children = _props3.children,
661 readOnly = _props3.readOnly,
662 notFoundContent = _props3.notFoundContent,
663 useVirtual = _props3.useVirtual;
664 var _state4 = this.state,
665 value = _state4.value,
666 searchedValue = _state4.searchedValue,
667 expandedKeys = _state4.expandedKeys,
668 autoExpandParent = _state4.autoExpandParent,
669 searchedKeys = _state4.searchedKeys;
670
671
672 var treeProps = {
673 multiple: multiple,
674 ref: this.saveTreeRef,
675 loadData: treeLoadData,
676 defaultExpandAll: treeDefaultExpandAll,
677 defaultExpandedKeys: treeDefaultExpandedKeys,
678 useVirtual: useVirtual
679 };
680
681 // 使用虚拟滚动 设置默认高度
682 if (useVirtual) {
683 customTreeProps.style = (0, _extends3.default)({
684 maxHeight: '260px',
685 overflow: 'auto',
686 boxSizing: 'border-box'
687 }, customTreeProps.style);
688 }
689
690 var keys = this.getKeysByValue(value);
691 if (treeCheckable) {
692 treeProps.checkable = treeCheckable;
693 treeProps.checkStrictly = treeCheckStrictly;
694 treeProps.checkedStrategy = treeCheckStrictly ? 'all' : treeCheckedStrategy;
695 treeProps.checkedKeys = keys;
696 if (!readOnly) {
697 treeProps.onCheck = this.handleCheck;
698 }
699 } else {
700 treeProps.selectedKeys = keys;
701 if (!readOnly) {
702 treeProps.onSelect = this.handleSelect;
703 }
704 }
705
706 var notFound = false;
707 var newChildren = void 0;
708 if (filterLocal && showSearch && searchedValue) {
709 treeProps.expandedKeys = expandedKeys;
710 treeProps.autoExpandParent = autoExpandParent;
711 treeProps.onExpand = this.handleExpand;
712 treeProps.filterTreeNode = function (node) {
713 return searchedKeys.indexOf(node.props.eventKey) > -1;
714 };
715
716 if (searchedKeys.length) {
717 newChildren = dataSource ? this.createNodesByData(dataSource, true) : this.searchNodes(children);
718 } else {
719 notFound = true;
720 }
721 } else {
722 // eslint-disable-next-line
723 if (dataSource) {
724 if (dataSource.length) {
725 newChildren = this.createNodesByData(dataSource);
726 } else {
727 notFound = true;
728 }
729 } else {
730 // eslint-disable-next-line
731 if (_react.Children.count(children)) {
732 newChildren = children;
733 } else {
734 notFound = true;
735 }
736 }
737 }
738 var contentClass = treeSelectPrefix + 'dropdown-content';
739
740 return _react2.default.createElement(
741 'div',
742 { className: treeSelectPrefix + 'dropdown' },
743 notFound ? _react2.default.createElement(
744 'div',
745 { className: treeSelectPrefix + 'not-found contentClass' },
746 notFoundContent
747 ) : _react2.default.createElement(
748 _tree2.default,
749 (0, _extends3.default)({}, treeProps, customTreeProps, { className: contentClass }),
750 newChildren
751 )
752 );
753 };
754
755 TreeSelect.prototype.renderPreview = function renderPreview(data, others) {
756 var _props4 = this.props,
757 prefix = _props4.prefix,
758 className = _props4.className,
759 renderPreview = _props4.renderPreview;
760
761
762 var previewCls = (0, _classnames2.default)(className, prefix + 'form-preview');
763 var items = data && !Array.isArray(data) ? [data] : data;
764
765 if (typeof renderPreview === 'function') {
766 return _react2.default.createElement(
767 'div',
768 (0, _extends3.default)({}, others, { className: previewCls }),
769 renderPreview(items, this.props)
770 );
771 }
772
773 return _react2.default.createElement(
774 'p',
775 (0, _extends3.default)({}, others, { className: previewCls }),
776 items && items.map(function (_ref) {
777 var label = _ref.label;
778 return label;
779 }).join(', ')
780 );
781 };
782
783 /**
784 * TreeSelect 无法直接使用 Select 的 maxTagPlaceholder 逻辑
785 * Select 的 totalValue 是所有 leaf 节点,TreeSelect 的 totalValue 受 treeCheckedStrategy 和 treeCheckStrictly 影响
786 *
787 * treeCheckStrictly = true: totalValue 为所有节点
788 *
789 * treeCheckStrictly = false: 根据 treeCheckedStrategy 判断
790 * treeCheckedStrategy = 'all': totalValue 为所有节点
791 * treeCheckedStrategy = 'parent': totalValue 无意义,不返回
792 * treeCheckedStrategy = 'child': totalValue 为所有 leaf 节点
793 */
794
795
796 TreeSelect.prototype.renderMaxTagPlaceholder = function renderMaxTagPlaceholder(value, totalValue) {
797 // 这里的 totalValue 为所有 leaf 节点
798 var _props5 = this.props,
799 treeCheckStrictly = _props5.treeCheckStrictly,
800 maxTagPlaceholder = _props5.maxTagPlaceholder,
801 treeCheckedStrategy = _props5.treeCheckedStrategy,
802 locale = _props5.locale;
803 var _v2n = this.state._v2n;
804
805
806 var treeSelectTotalValue = totalValue; // all the leaf nodes
807
808 // calculate total value
809 if (treeCheckStrictly) {
810 // all the nodes
811 treeSelectTotalValue = _util2.obj.values(_v2n);
812 } else if (treeCheckedStrategy === 'all') {
813 // all
814 treeSelectTotalValue = _util2.obj.values(_v2n);
815 } else if (treeCheckedStrategy === 'parent') {
816 // totalValue is pointless when treeCheckedStrategy = 'parent'
817 treeSelectTotalValue = undefined;
818 }
819
820 // custom render function
821 if (maxTagPlaceholder) {
822 return maxTagPlaceholder(value, treeSelectTotalValue);
823 }
824
825 // default render function
826 if (treeCheckedStrategy === 'parent') {
827 // don't show totalValue when treeCheckedStrategy = 'parent'
828 return '' + _util2.str.template(locale.shortMaxTagPlaceholder, {
829 selected: value.length
830 });
831 }
832 return '' + _util2.str.template(locale.maxTagPlaceholder, {
833 selected: value.length,
834 total: treeSelectTotalValue.length
835 });
836 };
837
838 /*eslint-enable*/
839
840
841 TreeSelect.prototype.render = function render() {
842 var _props6 = this.props,
843 prefix = _props6.prefix,
844 size = _props6.size,
845 placeholder = _props6.placeholder,
846 disabled = _props6.disabled,
847 hasArrow = _props6.hasArrow,
848 hasBorder = _props6.hasBorder,
849 hasClear = _props6.hasClear,
850 label = _props6.label,
851 readOnly = _props6.readOnly,
852 autoWidth = _props6.autoWidth,
853 popupStyle = _props6.popupStyle,
854 popupClassName = _props6.popupClassName,
855 showSearch = _props6.showSearch,
856 multiple = _props6.multiple,
857 treeCheckable = _props6.treeCheckable,
858 treeCheckStrictly = _props6.treeCheckStrictly,
859 className = _props6.className,
860 popupContainer = _props6.popupContainer,
861 popupProps = _props6.popupProps,
862 followTrigger = _props6.followTrigger,
863 isPreview = _props6.isPreview,
864 dataSource = _props6.dataSource,
865 tagInline = _props6.tagInline;
866
867 var others = _util2.obj.pickOthers(Object.keys(TreeSelect.propTypes), this.props);
868 var _state5 = this.state,
869 value = _state5.value,
870 visible = _state5.visible;
871
872 // if (non-leaf 节点可选 & 父子节点选中状态需要联动),需要额外计算父子节点间的联动关系
873
874 var valueForSelect = treeCheckable && !treeCheckStrictly ? this.getValueForSelect(value) : value;
875
876 var data = this.getData(valueForSelect, true);
877 if (!multiple && !treeCheckable) {
878 data = data[0];
879 }
880
881 if (isPreview) {
882 return this.renderPreview(data, others);
883 }
884
885 return _react2.default.createElement(_select2.default, (0, _extends3.default)({
886 prefix: prefix,
887 className: className,
888 size: size,
889 hasBorder: hasBorder,
890 hasArrow: hasArrow,
891 hasClear: hasClear,
892 placeholder: placeholder,
893 disabled: disabled,
894 autoWidth: autoWidth,
895 label: label,
896 readOnly: readOnly,
897 ref: this.saveSelectRef,
898 dataSource: dataSource,
899 value: data,
900 onChange: this.handleChange,
901 visible: visible,
902 onVisibleChange: this.handleVisibleChange,
903 showSearch: showSearch,
904 onSearch: this.handleSearch,
905 onSearchClear: this.handleSearchClear,
906 popupContainer: popupContainer,
907 popupStyle: popupStyle,
908 popupClassName: popupClassName,
909 popupProps: popupProps,
910 followTrigger: followTrigger,
911 tagInline: tagInline,
912 maxTagPlaceholder: this.renderMaxTagPlaceholder
913 }, others, {
914 onRemove: this.handleRemove,
915 onKeyDown: this.handleKeyDown,
916 popupContent: this.renderPopupContent(),
917 mode: treeCheckable || multiple ? 'multiple' : 'single'
918 }));
919 };
920
921 return TreeSelect;
922}(_react.Component), _class.propTypes = {
923 prefix: _propTypes2.default.string,
924 pure: _propTypes2.default.bool,
925 locale: _propTypes2.default.object,
926 className: _propTypes2.default.string,
927 /**
928 * 树节点
929 */
930 children: _propTypes2.default.node,
931 /**
932 * 选择框大小
933 */
934 size: _propTypes2.default.oneOf(['small', 'medium', 'large']),
935 /**
936 * 选择框占位符
937 */
938 placeholder: _propTypes2.default.string,
939 /**
940 * 是否禁用
941 */
942 disabled: _propTypes2.default.bool,
943 /**
944 * 是否有下拉箭头
945 */
946 hasArrow: _propTypes2.default.bool,
947 /**
948 * 是否有边框
949 */
950 hasBorder: _propTypes2.default.bool,
951 /**
952 * 是否有清空按钮
953 */
954 hasClear: _propTypes2.default.bool,
955 /**
956 * 自定义内联 label
957 */
958 label: _propTypes2.default.node,
959 /**
960 * 是否只读,只读模式下可以展开弹层但不能选择
961 */
962 readOnly: _propTypes2.default.bool,
963 /**
964 * 下拉框是否与选择器对齐
965 */
966 autoWidth: _propTypes2.default.bool,
967 /**
968 * 数据源,该属性优先级高于 children
969 */
970 dataSource: _propTypes2.default.arrayOf(_propTypes2.default.object),
971 /**
972 * value/defaultValue 在 dataSource 中不存在时,是否展示
973 * @version 1.25
974 */
975 preserveNonExistentValue: _propTypes2.default.bool,
976 /**
977 * (受控)当前值
978 */
979 value: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.object, _propTypes2.default.arrayOf(_propTypes2.default.any)]),
980 /**
981 * (非受控)默认值
982 */
983 defaultValue: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.object, _propTypes2.default.arrayOf(_propTypes2.default.any)]),
984 /**
985 * 选中值改变时触发的回调函数
986 * @param {String|Array} value 选中的值,单选时返回单个值,多选时返回数组
987 * @param {Object|Array} data 选中的数据,包括 value, label, pos, key属性,单选时返回单个值,多选时返回数组,父子节点选中关联时,同时选中,只返回父节点
988 */
989 onChange: _propTypes2.default.func,
990 /**
991 * 是否一行显示,仅在 multiple 和 treeCheckable 为 true 时生效
992 * @version 1.25
993 */
994 tagInline: _propTypes2.default.bool,
995 /**
996 * 隐藏多余 tag 时显示的内容,在 tagInline 生效时起作用
997 * @param {Object[]} selectedValues 当前已选中的元素
998 * @param {Object[]} totalValues 总待选元素
999 * @returns {reactNode}
1000 * @version 1.25
1001 */
1002 maxTagPlaceholder: _propTypes2.default.func,
1003 /**
1004 * 是否显示搜索框
1005 */
1006 showSearch: _propTypes2.default.bool,
1007 /**
1008 * 是否使用本地过滤,在数据源为远程的时候需要关闭此项
1009 */
1010 filterLocal: _propTypes2.default.bool,
1011 /**
1012 * 在搜索框中输入时触发的回调函数
1013 * @param {String} keyword 输入的关键字
1014 */
1015 onSearch: _propTypes2.default.func,
1016 onSearchClear: _propTypes2.default.func,
1017 /**
1018 * 无数据时显示内容
1019 */
1020 notFoundContent: _propTypes2.default.node,
1021 /**
1022 * 是否支持多选
1023 */
1024 multiple: _propTypes2.default.bool,
1025 /**
1026 * 下拉框中的树是否支持勾选节点的复选框
1027 */
1028 treeCheckable: _propTypes2.default.bool,
1029 /**
1030 * 下拉框中的树勾选节点复选框是否完全受控(父子节点选中状态不再关联)
1031 */
1032 treeCheckStrictly: _propTypes2.default.bool,
1033 /**
1034 * 定义选中时回填的方式
1035 * @enumdesc 返回所有选中的节点, 父子节点都选中时只返回父节点, 父子节点都选中时只返回子节点
1036 */
1037 treeCheckedStrategy: _propTypes2.default.oneOf(['all', 'parent', 'child']),
1038 /**
1039 * 下拉框中的树是否默认展开所有节点
1040 */
1041 treeDefaultExpandAll: _propTypes2.default.bool,
1042 /**
1043 * 下拉框中的树默认展开节点key的数组
1044 */
1045 treeDefaultExpandedKeys: _propTypes2.default.arrayOf(_propTypes2.default.string),
1046 /**
1047 * 下拉框中的树异步加载数据的函数,使用请参考[Tree的异步加载数据Demo](https://fusion.design/pc/component/tree?themeid=2#dynamic-container)
1048 * @param {ReactElement} node 被点击展开的节点
1049 */
1050 treeLoadData: _propTypes2.default.func,
1051 /**
1052 * 透传到 Tree 的属性对象
1053 */
1054 treeProps: _propTypes2.default.object,
1055 /**
1056 * 初始下拉框是否显示
1057 */
1058 defaultVisible: _propTypes2.default.bool,
1059 /**
1060 * 当前下拉框是否显示
1061 */
1062 visible: _propTypes2.default.bool,
1063 /**
1064 * 下拉框显示或关闭时触发事件的回调函数
1065 * @param {Boolean} visible 是否显示
1066 * @param {String} type 触发显示关闭的操作类型
1067 */
1068 onVisibleChange: _propTypes2.default.func,
1069 /**
1070 * 下拉框自定义样式对象
1071 */
1072 popupStyle: _propTypes2.default.object,
1073 /**
1074 * 下拉框样式自定义类名
1075 */
1076 popupClassName: _propTypes2.default.string,
1077 /**
1078 * 下拉框挂载的容器节点
1079 */
1080 popupContainer: _propTypes2.default.any,
1081 /**
1082 * 透传到 Popup 的属性对象
1083 */
1084 popupProps: _propTypes2.default.object,
1085 /**
1086 * 是否跟随滚动
1087 */
1088 followTrigger: _propTypes2.default.bool,
1089 /**
1090 * 是否为预览态
1091 */
1092 isPreview: _propTypes2.default.bool,
1093 /**
1094 * 预览态模式下渲染的内容
1095 * @param {Array<data>} value 选择值 { label: , value:}
1096 */
1097 renderPreview: _propTypes2.default.func,
1098 /**
1099 * 是否开启虚拟滚动
1100 */
1101 useVirtual: _propTypes2.default.bool,
1102 /**
1103 * 是否是不可变数据
1104 * @version 1.23
1105 */
1106 immutable: _propTypes2.default.bool
1107}, _class.defaultProps = {
1108 prefix: 'next-',
1109 pure: false,
1110 locale: _zhCn2.default.TreeSelect,
1111 size: 'medium',
1112 disabled: false,
1113 hasArrow: true,
1114 hasBorder: true,
1115 hasClear: false,
1116 autoWidth: true,
1117 defaultValue: null,
1118 onChange: noop,
1119 tagInline: false,
1120 showSearch: false,
1121 filterLocal: true,
1122 onSearch: noop,
1123 onSearchClear: noop,
1124 notFoundContent: 'Not Found',
1125 multiple: false,
1126 treeCheckable: false,
1127 treeCheckStrictly: false,
1128 treeCheckedStrategy: 'parent',
1129 treeDefaultExpandAll: false,
1130 treeDefaultExpandedKeys: [],
1131 treeProps: {},
1132 defaultVisible: false,
1133 onVisibleChange: noop,
1134 useVirtual: false,
1135 /**
1136 * TODO
1137 * 目前 select/cascade select 是默认支持的,在 2.x 版本中 tree-select 也将默认支持
1138 */
1139 preserveNonExistentValue: false
1140}, _temp);
1141TreeSelect.displayName = 'TreeSelect';
1142
1143
1144TreeSelect.Node = TreeNode;
1145
1146exports.default = (0, _reactLifecyclesCompat.polyfill)(TreeSelect);
1147module.exports = exports['default'];
\No newline at end of file