1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
var _defineProperty2 = require('babel-runtime/helpers/defineProperty');
var _defineProperty3 = _interopRequireDefault(_defineProperty2);
var _vueTypes = require('../_util/vue-types');
var _vueTypes2 = _interopRequireDefault(_vueTypes);
var _vcCascader = require('../vc-cascader');
var _vcCascader2 = _interopRequireDefault(_vcCascader);
var _arrayTreeFilter = require('array-tree-filter');
var _arrayTreeFilter2 = _interopRequireDefault(_arrayTreeFilter);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var _omit = require('omit.js');
var _omit2 = _interopRequireDefault(_omit);
var _KeyCode = require('../_util/KeyCode');
var _KeyCode2 = _interopRequireDefault(_KeyCode);
var _input = require('../input');
var _input2 = _interopRequireDefault(_input);
var _icon = require('../icon');
var _icon2 = _interopRequireDefault(_icon);
var _propsUtil = require('../_util/props-util');
var _BaseMixin = require('../_util/BaseMixin');
var _BaseMixin2 = _interopRequireDefault(_BaseMixin);
var _vnode = require('../_util/vnode');
var _warning = require('../_util/warning');
var _warning2 = _interopRequireDefault(_warning);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var CascaderOptionType = _vueTypes2['default'].shape({
value: _vueTypes2['default'].oneOfType([_vueTypes2['default'].string, _vueTypes2['default'].number]),
label: _vueTypes2['default'].any,
disabled: _vueTypes2['default'].bool,
children: _vueTypes2['default'].array,
key: _vueTypes2['default'].oneOfType([_vueTypes2['default'].string, _vueTypes2['default'].number])
}).loose;
var FieldNamesType = _vueTypes2['default'].shape({
value: _vueTypes2['default'].string.isRequired,
label: _vueTypes2['default'].string.isRequired,
children: _vueTypes2['default'].string
}).loose;
var CascaderExpandTrigger = _vueTypes2['default'].oneOf(['click', 'hover']);
var ShowSearchType = _vueTypes2['default'].shape({
filter: _vueTypes2['default'].func,
render: _vueTypes2['default'].func,
sort: _vueTypes2['default'].func,
matchInputWidth: _vueTypes2['default'].bool,
limit: _vueTypes2['default'].oneOfType([Boolean, Number])
}).loose;
function noop() {}
var CascaderProps = {
/** 可选项数据源 */
options: _vueTypes2['default'].arrayOf(CascaderOptionType).def([]),
/** 默认的选中项 */
defaultValue: _vueTypes2['default'].array,
/** 指定选中项 */
value: _vueTypes2['default'].array,
/** 选择完成后的回调 */
// onChange?: (value: string[], selectedOptions?: CascaderOptionType[]) => void;
/** 选择后展示的渲染函数 */
displayRender: _vueTypes2['default'].func,
transitionName: _vueTypes2['default'].string.def('slide-up'),
popupStyle: _vueTypes2['default'].object.def({}),
/** 自定义浮层类名 */
popupClassName: _vueTypes2['default'].string,
/** 浮层预设位置:`bottomLeft` `bottomRight` `topLeft` `topRight` */
popupPlacement: _vueTypes2['default'].oneOf(['bottomLeft', 'bottomRight', 'topLeft', 'topRight']).def('bottomLeft'),
/** 输入框占位文本*/
placeholder: _vueTypes2['default'].string.def('Please select'),
/** 输入框大小,可选 `large` `default` `small` */
size: _vueTypes2['default'].oneOf(['large', 'default', 'small']),
/** 禁用*/
disabled: _vueTypes2['default'].bool.def(false),
/** 是否支持清除*/
allowClear: _vueTypes2['default'].bool.def(true),
showSearch: _vueTypes2['default'].oneOfType([Boolean, ShowSearchType]),
notFoundContent: _vueTypes2['default'].any.def('Not Found'),
loadData: _vueTypes2['default'].func,
/** 次级菜单的展开方式,可选 'click' 和 'hover' */
expandTrigger: CascaderExpandTrigger,
/** 当此项为 true 时,点选每级菜单选项值都会发生变化 */
changeOnSelect: _vueTypes2['default'].bool,
/** 浮层可见变化时回调 */
// onPopupVisibleChange?: (popupVisible: boolean) => void;
prefixCls: _vueTypes2['default'].string.def('ant-cascader'),
inputPrefixCls: _vueTypes2['default'].string.def('ant-input'),
getPopupContainer: _vueTypes2['default'].func,
popupVisible: _vueTypes2['default'].bool,
fieldNames: FieldNamesType,
autoFocus: _vueTypes2['default'].bool,
suffixIcon: _vueTypes2['default'].any
};
// We limit the filtered item count by default
var defaultLimit = 50;
function defaultFilterOption(inputValue, path, names) {
return path.some(function (option) {
return option[names.label].indexOf(inputValue) > -1;
});
}
function defaultSortFilteredOption(a, b, inputValue, names) {
function callback(elem) {
return elem[names.label].indexOf(inputValue) > -1;
}
return a.findIndex(callback) - b.findIndex(callback);
}
function getFilledFieldNames(_ref) {
var _ref$fieldNames = _ref.fieldNames,
fieldNames = _ref$fieldNames === undefined ? {} : _ref$fieldNames;
var names = {
children: fieldNames.children || 'children',
label: fieldNames.label || 'label',
value: fieldNames.value || 'value'
};
return names;
}
function flattenTree() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var props = arguments[1];
var ancestor = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
var names = getFilledFieldNames(props);
var flattenOptions = [];
var childrenName = names.children;
options.forEach(function (option) {
var path = ancestor.concat(option);
if (props.changeOnSelect || !option[childrenName] || !option[childrenName].length) {
flattenOptions.push(path);
}
if (option[childrenName]) {
flattenOptions = flattenOptions.concat(flattenTree(option[childrenName], props, path));
}
});
return flattenOptions;
}
var defaultDisplayRender = function defaultDisplayRender(_ref2) {
var labels = _ref2.labels;
return labels.join(' / ');
};
var Cascader = {
inheritAttrs: false,
name: 'ACascader',
mixins: [_BaseMixin2['default']],
props: CascaderProps,
model: {
prop: 'value',
event: 'change'
},
inject: {
configProvider: { 'default': function _default() {
return {};
} },
localeData: { 'default': function _default() {
return {};
} }
},
data: function data() {
this.cachedOptions = [];
var value = this.value,
defaultValue = this.defaultValue,
popupVisible = this.popupVisible,
showSearch = this.showSearch,
options = this.options;
return {
sValue: value || defaultValue || [],
inputValue: '',
inputFocused: false,
sPopupVisible: popupVisible,
flattenOptions: showSearch ? flattenTree(options, this.$props) : undefined
};
},
mounted: function mounted() {
var _this = this;
this.$nextTick(function () {
if (_this.autoFocus && !_this.showSearch && !_this.disabled) {
_this.$refs.picker.focus();
}
});
},
watch: {
value: function value(val) {
this.setState({ sValue: val || [] });
},
popupVisible: function popupVisible(val) {
this.setState({ sPopupVisible: val });
},
options: function options(val) {
if (this.showSearch) {
this.setState({ flattenOptions: flattenTree(val, this.$props) });
}
}
},
methods: {
highlightKeyword: function highlightKeyword(str, keyword, prefixCls) {
var h = this.$createElement;
return str.split(keyword).map(function (node, index) {
return index === 0 ? node : [h(
'span',
{ 'class': prefixCls + '-menu-item-keyword' },
[keyword]
), node];
});
},
defaultRenderFilteredOption: function defaultRenderFilteredOption(_ref3) {
var _this2 = this;
var inputValue = _ref3.inputValue,
path = _ref3.path,
prefixCls = _ref3.prefixCls,
names = _ref3.names;
return path.map(function (option, index) {
var label = option[names.label];
var node = label.indexOf(inputValue) > -1 ? _this2.highlightKeyword(label, inputValue, prefixCls) : label;
return index === 0 ? node : [' / ', node];
});
},
handleChange: function handleChange(value, selectedOptions) {
this.setState({ inputValue: '' });
if (selectedOptions[0].__IS_FILTERED_OPTION) {
var unwrappedValue = value[0];
var unwrappedSelectedOptions = selectedOptions[0].path;
this.setValue(unwrappedValue, unwrappedSelectedOptions);
return;
}
this.setValue(value, selectedOptions);
},
handlePopupVisibleChange: function handlePopupVisibleChange(popupVisible) {
if (!(0, _propsUtil.hasProp)(this, 'popupVisible')) {
this.setState(function (state) {
return {
sPopupVisible: popupVisible,
inputFocused: popupVisible,
inputValue: popupVisible ? state.inputValue : ''
};
});
}
this.$emit('popupVisibleChange', popupVisible);
},
handleInputFocus: function handleInputFocus(e) {
this.$emit('focus', e);
},
handleInputBlur: function handleInputBlur(e) {
this.setState({
inputFocused: false
});
this.$emit('blur', e);
},
handleInputClick: function handleInputClick(e) {
var inputFocused = this.inputFocused,
sPopupVisible = this.sPopupVisible;
// Prevent `Trigger` behaviour.
if (inputFocused || sPopupVisible) {
e.stopPropagation();
if (e.nativeEvent && e.nativeEvent.stopImmediatePropagation) {
e.nativeEvent.stopImmediatePropagation();
}
}
},
handleKeyDown: function handleKeyDown(e) {
if (e.keyCode === _KeyCode2['default'].BACKSPACE) {
e.stopPropagation();
}
},
handleInputChange: function handleInputChange(e) {
var inputValue = e.target.value;
this.setState({ inputValue: inputValue });
},
setValue: function setValue(value, selectedOptions) {
if (!(0, _propsUtil.hasProp)(this, 'value')) {
this.setState({ sValue: value });
}
this.$emit('change', value, selectedOptions);
},
getLabel: function getLabel() {
var options = this.options,
$scopedSlots = this.$scopedSlots;
var names = getFilledFieldNames(this.$props);
var displayRender = this.displayRender || $scopedSlots.displayRender || defaultDisplayRender;
var value = this.sValue;
var unwrappedValue = Array.isArray(value[0]) ? value[0] : value;
var selectedOptions = (0, _arrayTreeFilter2['default'])(options, function (o, level) {
return o[names.value] === unwrappedValue[level];
}, { childrenKeyName: names.children });
var labels = selectedOptions.map(function (o) {
return o[names.label];
});
return displayRender({ labels: labels, selectedOptions: selectedOptions });
},
clearSelection: function clearSelection(e) {
e.preventDefault();
e.stopPropagation();
if (!this.inputValue) {
this.setValue([]);
this.handlePopupVisibleChange(false);
} else {
this.setState({ inputValue: '' });
}
},
generateFilteredOptions: function generateFilteredOptions(prefixCls) {
var _ref5;
var showSearch = this.showSearch,
notFoundContent = this.notFoundContent,
$scopedSlots = this.$scopedSlots;
var names = getFilledFieldNames(this.$props);
var _showSearch$filter = showSearch.filter,
filter = _showSearch$filter === undefined ? defaultFilterOption : _showSearch$filter,
_showSearch$sort = showSearch.sort,
sort = _showSearch$sort === undefined ? defaultSortFilteredOption : _showSearch$sort,
_showSearch$limit = showSearch.limit,
limit = _showSearch$limit === undefined ? defaultLimit : _showSearch$limit;
var render = showSearch.render || $scopedSlots.showSearchRender || this.defaultRenderFilteredOption;
var _$data = this.$data,
_$data$flattenOptions = _$data.flattenOptions,
flattenOptions = _$data$flattenOptions === undefined ? [] : _$data$flattenOptions,
inputValue = _$data.inputValue;
// Limit the filter if needed
var filtered = void 0;
if (limit > 0) {
filtered = [];
var matchCount = 0;
// Perf optimization to filter items only below the limit
flattenOptions.some(function (path) {
var match = filter(inputValue, path, names);
if (match) {
filtered.push(path);
matchCount += 1;
}
return matchCount >= limit;
});
} else {
(0, _warning2['default'])(typeof limit !== 'number', "'limit' of showSearch in Cascader should be positive number or false.");
filtered = flattenOptions.filter(function (path) {
return filter(inputValue, path, names);
});
}
filtered.sort(function (a, b) {
return sort(a, b, inputValue, names);
});
if (filtered.length > 0) {
return filtered.map(function (path) {
var _ref4;
return _ref4 = {
__IS_FILTERED_OPTION: true,
path: path
}, (0, _defineProperty3['default'])(_ref4, names.label, render({ inputValue: inputValue, path: path, prefixCls: prefixCls, names: names })), (0, _defineProperty3['default'])(_ref4, names.value, path.map(function (o) {
return o[names.value];
})), (0, _defineProperty3['default'])(_ref4, 'disabled', path.some(function (o) {
return !!o.disabled;
})), _ref4;
});
}
return [(_ref5 = {}, (0, _defineProperty3['default'])(_ref5, names.label, notFoundContent), (0, _defineProperty3['default'])(_ref5, names.value, 'ANT_CASCADER_NOT_FOUND'), (0, _defineProperty3['default'])(_ref5, 'disabled', true), _ref5)];
},
focus: function focus() {
if (this.showSearch) {
this.$refs.input.focus();
} else {
this.$refs.picker.focus();
}
},
blur: function blur() {
if (this.showSearch) {
this.$refs.input.blur();
} else {
this.$refs.picker.blur();
}
}
},
render: function render() {
var _classNames, _classNames2, _classNames3;
var h = arguments[0];
var $slots = this.$slots,
sPopupVisible = this.sPopupVisible,
inputValue = this.inputValue,
$listeners = this.$listeners,
configProvider = this.configProvider,
localeData = this.localeData;
var _$data2 = this.$data,
value = _$data2.sValue,
inputFocused = _$data2.inputFocused;
var props = (0, _propsUtil.getOptionProps)(this);
var suffixIcon = (0, _propsUtil.getComponentFromProp)(this, 'suffixIcon');
suffixIcon = Array.isArray(suffixIcon) ? suffixIcon[0] : suffixIcon;
var getContextPopupContainer = configProvider.getPopupContainer;
var prefixCls = props.prefixCls,
inputPrefixCls = props.inputPrefixCls,
_props$placeholder = props.placeholder,
placeholder = _props$placeholder === undefined ? localeData.placeholder : _props$placeholder,
size = props.size,
disabled = props.disabled,
allowClear = props.allowClear,
_props$showSearch = props.showSearch,
showSearch = _props$showSearch === undefined ? false : _props$showSearch,
otherProps = (0, _objectWithoutProperties3['default'])(props, ['prefixCls', 'inputPrefixCls', 'placeholder', 'size', 'disabled', 'allowClear', 'showSearch']);
var sizeCls = (0, _classnames2['default'])((_classNames = {}, (0, _defineProperty3['default'])(_classNames, inputPrefixCls + '-lg', size === 'large'), (0, _defineProperty3['default'])(_classNames, inputPrefixCls + '-sm', size === 'small'), _classNames));
var clearIcon = allowClear && !disabled && value.length > 0 || inputValue ? h(_icon2['default'], {
attrs: {
type: 'close-circle',
theme: 'filled'
},
'class': prefixCls + '-picker-clear',
on: {
'click': this.clearSelection
},
key: 'clear-icon'
}) : null;
var arrowCls = (0, _classnames2['default'])((_classNames2 = {}, (0, _defineProperty3['default'])(_classNames2, prefixCls + '-picker-arrow', true), (0, _defineProperty3['default'])(_classNames2, prefixCls + '-picker-arrow-expand', sPopupVisible), _classNames2));
var pickerCls = (0, _classnames2['default'])((0, _propsUtil.getClass)(this), prefixCls + '-picker', (_classNames3 = {}, (0, _defineProperty3['default'])(_classNames3, prefixCls + '-picker-with-value', inputValue), (0, _defineProperty3['default'])(_classNames3, prefixCls + '-picker-disabled', disabled), (0, _defineProperty3['default'])(_classNames3, prefixCls + '-picker-' + size, !!size), (0, _defineProperty3['default'])(_classNames3, prefixCls + '-picker-show-search', !!showSearch), (0, _defineProperty3['default'])(_classNames3, prefixCls + '-picker-focused', inputFocused), _classNames3));
// Fix bug of https://github.com/facebook/react/pull/5004
// and https://fb.me/react-unknown-prop
var tempInputProps = (0, _omit2['default'])(otherProps, ['options', 'popupPlacement', 'transitionName', 'displayRender', 'changeOnSelect', 'expandTrigger', 'popupVisible', 'getPopupContainer', 'loadData', 'popupClassName', 'filterOption', 'renderFilteredOption', 'sortFilteredOption', 'notFoundContent', 'defaultValue', 'fieldNames']);
var options = props.options;
if (inputValue) {
options = this.generateFilteredOptions(prefixCls);
}
// Dropdown menu should keep previous status until it is fully closed.
if (!sPopupVisible) {
options = this.cachedOptions;
} else {
this.cachedOptions = options;
}
var dropdownMenuColumnStyle = {};
var isNotFound = (options || []).length === 1 && options[0].value === 'ANT_CASCADER_NOT_FOUND';
if (isNotFound) {
dropdownMenuColumnStyle.height = 'auto'; // Height of one row.
}
// The default value of `matchInputWidth` is `true`
var resultListMatchInputWidth = showSearch.matchInputWidth !== false;
if (resultListMatchInputWidth && inputValue && this.input) {
dropdownMenuColumnStyle.width = this.input.input.offsetWidth;
}
// showSearch时,focus、blur在input上触发,反之在ref='picker'上触发
var inputProps = {
props: (0, _extends3['default'])({}, tempInputProps, {
prefixCls: inputPrefixCls,
placeholder: value && value.length > 0 ? undefined : placeholder,
value: inputValue,
disabled: disabled,
readOnly: !showSearch,
autoComplete: 'off'
}),
'class': prefixCls + '-input ' + sizeCls,
ref: 'input',
on: {
focus: showSearch ? this.handleInputFocus : noop,
click: showSearch ? this.handleInputClick : noop,
blur: showSearch ? this.handleInputBlur : noop,
keydown: this.handleKeyDown,
change: showSearch ? this.handleInputChange : noop
},
attrs: (0, _propsUtil.getAttrs)(this)
};
var children = (0, _propsUtil.filterEmpty)($slots['default']);
var inputIcon = suffixIcon && ((0, _propsUtil.isValidElement)(suffixIcon) ? (0, _vnode.cloneElement)(suffixIcon, {
'class': (0, _defineProperty3['default'])({}, prefixCls + '-picker-arrow', true)
}) : h(
'span',
{ 'class': prefixCls + '-picker-arrow' },
[suffixIcon]
)) || h(_icon2['default'], {
attrs: { type: 'down' },
'class': arrowCls });
var input = children.length ? children : h(
'span',
{ 'class': pickerCls, style: (0, _propsUtil.getStyle)(this), ref: 'picker' },
[showSearch ? h(
'span',
{ 'class': prefixCls + '-picker-label' },
[this.getLabel()]
) : null, h(_input2['default'], inputProps), !showSearch ? h(
'span',
{ 'class': prefixCls + '-picker-label' },
[this.getLabel()]
) : null, clearIcon, inputIcon]
);
var expandIcon = h(_icon2['default'], {
attrs: { type: 'right' }
});
var loadingIcon = h(
'span',
{ 'class': prefixCls + '-menu-item-loading-icon' },
[h(_icon2['default'], {
attrs: { type: 'redo', spin: true }
})]
);
var getPopupContainer = props.getPopupContainer || getContextPopupContainer;
var cascaderProps = {
props: (0, _extends3['default'])({}, props, {
getPopupContainer: getPopupContainer,
options: options,
value: value,
popupVisible: sPopupVisible,
dropdownMenuColumnStyle: dropdownMenuColumnStyle,
expandIcon: expandIcon,
loadingIcon: loadingIcon
}),
on: (0, _extends3['default'])({}, $listeners, {
popupVisibleChange: this.handlePopupVisibleChange,
change: this.handleChange
})
};
return h(
_vcCascader2['default'],
cascaderProps,
[input]
);
}
};
/* istanbul ignore next */
Cascader.install = function (Vue) {
Vue.component(Cascader.name, Cascader);
};
exports['default'] = Cascader;