index.js
21.9 KB
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
import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
import * as React from 'react';
import RcCascader from 'rc-cascader';
import arrayTreeFilter from 'array-tree-filter';
import classNames from 'classnames';
import omit from 'omit.js';
import KeyCode from "rc-util/es/KeyCode";
import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled';
import DownOutlined from '@ant-design/icons/DownOutlined';
import RightOutlined from '@ant-design/icons/RightOutlined';
import RedoOutlined from '@ant-design/icons/RedoOutlined';
import LeftOutlined from '@ant-design/icons/LeftOutlined';
import Input from '../input';
import { ConfigConsumer } from '../config-provider';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import devWarning from '../_util/devWarning';
import SizeContext from '../config-provider/SizeContext';
import { replaceElement } from '../_util/reactNode'; // We limit the filtered item count by default
var defaultLimit = 50; // keep value when filtering
var keepFilteredValueField = '__KEEP_FILTERED_OPTION_VALUE';
function highlightKeyword(str, keyword, prefixCls) {
return str.split(keyword).map(function (node, index) {
return index === 0 ? node : [/*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-menu-item-keyword"),
key: "seperator"
}, keyword), node];
});
}
function defaultFilterOption(inputValue, path, names) {
return path.some(function (option) {
return option[names.label].indexOf(inputValue) > -1;
});
}
function defaultRenderFilteredOption(inputValue, path, prefixCls, names) {
return path.map(function (option, index) {
var label = option[names.label];
var node = label.indexOf(inputValue) > -1 ? highlightKeyword(label, inputValue, prefixCls) : label;
return index === 0 ? node : [' / ', node];
});
}
function defaultSortFilteredOption(a, b, inputValue, names) {
function callback(elem) {
return elem[names.label].indexOf(inputValue) > -1;
}
return a.findIndex(callback) - b.findIndex(callback);
}
function getFieldNames(_ref) {
var fieldNames = _ref.fieldNames;
return fieldNames;
}
function getFilledFieldNames(props) {
var fieldNames = getFieldNames(props) || {};
var names = {
children: fieldNames.children || 'children',
label: fieldNames.label || 'label',
value: fieldNames.value || 'value'
};
return names;
}
function flattenTree(options, props) {
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(label) {
return label.join(' / ');
};
function warningValueNotExist(list) {
var fieldNames = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
(list || []).forEach(function (item) {
var valueFieldName = fieldNames.value || 'value';
devWarning(valueFieldName in item, 'Cascader', 'Not found `value` in `options`.');
warningValueNotExist(item[fieldNames.children || 'children'], fieldNames);
});
}
function getEmptyNode(renderEmpty, names, notFoundContent) {
var _ref2;
return _ref2 = {}, _defineProperty(_ref2, names.value, 'ANT_CASCADER_NOT_FOUND'), _defineProperty(_ref2, names.label, notFoundContent || renderEmpty('Cascader')), _defineProperty(_ref2, "disabled", true), _defineProperty(_ref2, "isEmptyNode", true), _ref2;
}
var Cascader = /*#__PURE__*/function (_React$Component) {
_inherits(Cascader, _React$Component);
var _super = _createSuper(Cascader);
function Cascader(props) {
var _this;
_classCallCheck(this, Cascader);
_this = _super.call(this, props);
_this.cachedOptions = [];
_this.setValue = function (value) {
var selectedOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
if (!('value' in _this.props)) {
_this.setState({
value: value
});
}
var onChange = _this.props.onChange;
if (onChange) {
onChange(value, selectedOptions);
}
};
_this.saveInput = function (node) {
_this.input = node;
};
_this.handleChange = function (value, selectedOptions) {
_this.setState({
inputValue: ''
});
if (selectedOptions[0].__IS_FILTERED_OPTION) {
var unwrappedValue = selectedOptions[0][keepFilteredValueField] === undefined ? value[0] : selectedOptions[0][keepFilteredValueField];
var unwrappedSelectedOptions = selectedOptions[0].path;
_this.setValue(unwrappedValue, unwrappedSelectedOptions);
return;
}
_this.setValue(value, selectedOptions);
};
_this.handlePopupVisibleChange = function (popupVisible) {
if (!('popupVisible' in _this.props)) {
_this.setState(function (state) {
return {
popupVisible: popupVisible,
inputFocused: popupVisible,
inputValue: popupVisible ? state.inputValue : ''
};
});
}
var onPopupVisibleChange = _this.props.onPopupVisibleChange;
if (onPopupVisibleChange) {
onPopupVisibleChange(popupVisible);
}
};
_this.handleInputBlur = function () {
_this.setState({
inputFocused: false
});
};
_this.handleInputClick = function (e) {
var _this$state = _this.state,
inputFocused = _this$state.inputFocused,
popupVisible = _this$state.popupVisible; // Prevent `Trigger` behaviour.
if (inputFocused || popupVisible) {
e.stopPropagation();
}
};
_this.handleKeyDown = function (e) {
// SPACE => https://github.com/ant-design/ant-design/issues/16871
if (e.keyCode === KeyCode.BACKSPACE || e.keyCode === KeyCode.SPACE) {
e.stopPropagation();
}
};
_this.handleInputChange = function (e) {
var popupVisible = _this.state.popupVisible;
var inputValue = e.target.value;
if (!popupVisible) {
_this.handlePopupVisibleChange(true);
}
_this.setState({
inputValue: inputValue
});
};
_this.clearSelection = function (e) {
var inputValue = _this.state.inputValue;
e.preventDefault();
e.stopPropagation();
if (!inputValue) {
_this.handlePopupVisibleChange(false);
_this.clearSelectionTimeout = setTimeout(function () {
_this.setValue([]);
}, 200);
} else {
_this.setState({
inputValue: ''
});
}
};
_this.renderCascader = function (_ref3, locale) {
var getContextPopupContainer = _ref3.getPopupContainer,
getPrefixCls = _ref3.getPrefixCls,
renderEmpty = _ref3.renderEmpty,
direction = _ref3.direction;
return /*#__PURE__*/React.createElement(SizeContext.Consumer, null, function (size) {
var _classNames, _classNames2, _classNames3, _classNames5;
var _assertThisInitialize = _assertThisInitialized(_this),
props = _assertThisInitialize.props,
state = _assertThisInitialize.state;
var customizePrefixCls = props.prefixCls,
customizeInputPrefixCls = props.inputPrefixCls,
children = props.children,
_props$placeholder = props.placeholder,
placeholder = _props$placeholder === void 0 ? locale.placeholder || 'Please select' : _props$placeholder,
customizeSize = props.size,
disabled = props.disabled,
className = props.className,
style = props.style,
allowClear = props.allowClear,
_props$showSearch = props.showSearch,
showSearch = _props$showSearch === void 0 ? false : _props$showSearch,
suffixIcon = props.suffixIcon,
expandIcon = props.expandIcon,
notFoundContent = props.notFoundContent,
popupClassName = props.popupClassName,
bordered = props.bordered,
dropdownRender = props.dropdownRender,
otherProps = __rest(props, ["prefixCls", "inputPrefixCls", "children", "placeholder", "size", "disabled", "className", "style", "allowClear", "showSearch", "suffixIcon", "expandIcon", "notFoundContent", "popupClassName", "bordered", "dropdownRender"]);
var mergedSize = customizeSize || size;
var value = state.value,
inputFocused = state.inputFocused;
var isRtlLayout = direction === 'rtl';
var prefixCls = getPrefixCls('cascader', customizePrefixCls);
var inputPrefixCls = getPrefixCls('input', customizeInputPrefixCls);
var sizeCls = classNames((_classNames = {}, _defineProperty(_classNames, "".concat(inputPrefixCls, "-lg"), mergedSize === 'large'), _defineProperty(_classNames, "".concat(inputPrefixCls, "-sm"), mergedSize === 'small'), _classNames));
var clearIcon = allowClear && !disabled && value.length > 0 || state.inputValue ? /*#__PURE__*/React.createElement(CloseCircleFilled, {
className: "".concat(prefixCls, "-picker-clear"),
onClick: _this.clearSelection
}) : null;
var arrowCls = classNames((_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-picker-arrow"), true), _defineProperty(_classNames2, "".concat(prefixCls, "-picker-arrow-expand"), state.popupVisible), _classNames2));
var pickerCls = classNames("".concat(prefixCls, "-picker"), (_classNames3 = {}, _defineProperty(_classNames3, "".concat(prefixCls, "-picker-rtl"), isRtlLayout), _defineProperty(_classNames3, "".concat(prefixCls, "-picker-with-value"), state.inputValue), _defineProperty(_classNames3, "".concat(prefixCls, "-picker-disabled"), disabled), _defineProperty(_classNames3, "".concat(prefixCls, "-picker-").concat(mergedSize), !!mergedSize), _defineProperty(_classNames3, "".concat(prefixCls, "-picker-show-search"), !!showSearch), _defineProperty(_classNames3, "".concat(prefixCls, "-picker-focused"), inputFocused), _defineProperty(_classNames3, "".concat(prefixCls, "-picker-borderless"), !bordered), _classNames3), className); // Fix bug of https://github.com/facebook/react/pull/5004
// and https://fb.me/react-unknown-prop
var inputProps = omit(otherProps, ['onChange', 'options', 'popupPlacement', 'transitionName', 'displayRender', 'onPopupVisibleChange', 'changeOnSelect', 'expandTrigger', 'popupVisible', 'getPopupContainer', 'loadData', 'popupClassName', 'filterOption', 'renderFilteredOption', 'sortFilteredOption', 'notFoundContent', 'fieldNames', 'bordered']);
var options = props.options;
var names = getFilledFieldNames(_this.props);
if (options && options.length > 0) {
if (state.inputValue) {
options = _this.generateFilteredOptions(prefixCls, renderEmpty);
}
} else {
options = [getEmptyNode(renderEmpty, names, notFoundContent)];
} // Dropdown menu should keep previous status until it is fully closed.
if (!state.popupVisible) {
options = _this.cachedOptions;
} else {
_this.cachedOptions = options;
}
var dropdownMenuColumnStyle = {};
var isNotFound = (options || []).length === 1 && options[0].isEmptyNode;
if (isNotFound) {
dropdownMenuColumnStyle.height = 'auto'; // Height of one row.
} // The default value of `matchInputWidth` is `true`
var resultListMatchInputWidth = showSearch.matchInputWidth !== false;
if (resultListMatchInputWidth && (state.inputValue || isNotFound) && _this.input) {
dropdownMenuColumnStyle.width = _this.input.input.offsetWidth;
}
var inputIcon;
if (suffixIcon) {
inputIcon = replaceElement(suffixIcon, /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-picker-arrow")
}, suffixIcon), function () {
var _classNames4;
return {
className: classNames((_classNames4 = {}, _defineProperty(_classNames4, suffixIcon.props.className, suffixIcon.props.className), _defineProperty(_classNames4, "".concat(prefixCls, "-picker-arrow"), true), _classNames4))
};
});
} else {
inputIcon = /*#__PURE__*/React.createElement(DownOutlined, {
className: arrowCls
});
}
var input = children || /*#__PURE__*/React.createElement("span", {
style: style,
className: pickerCls
}, /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-picker-label")
}, _this.getLabel()), /*#__PURE__*/React.createElement(Input, _extends({}, inputProps, {
tabIndex: "-1",
ref: _this.saveInput,
prefixCls: inputPrefixCls,
placeholder: value && value.length > 0 ? undefined : placeholder,
className: "".concat(prefixCls, "-input ").concat(sizeCls),
value: state.inputValue,
disabled: disabled,
readOnly: !showSearch,
autoComplete: inputProps.autoComplete || 'off',
onClick: showSearch ? _this.handleInputClick : undefined,
onBlur: showSearch ? _this.handleInputBlur : undefined,
onKeyDown: _this.handleKeyDown,
onChange: showSearch ? _this.handleInputChange : undefined
})), clearIcon, inputIcon);
var expandIconNode;
if (expandIcon) {
expandIconNode = expandIcon;
} else {
expandIconNode = isRtlLayout ? /*#__PURE__*/React.createElement(LeftOutlined, null) : /*#__PURE__*/React.createElement(RightOutlined, null);
}
var loadingIcon = /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-menu-item-loading-icon")
}, /*#__PURE__*/React.createElement(RedoOutlined, {
spin: true
}));
var getPopupContainer = props.getPopupContainer || getContextPopupContainer;
var rest = omit(props, ['inputIcon', 'expandIcon', 'loadingIcon', 'bordered', 'className']);
var rcCascaderPopupClassName = classNames(popupClassName, (_classNames5 = {}, _defineProperty(_classNames5, "".concat(prefixCls, "-menu-").concat(direction), direction === 'rtl'), _defineProperty(_classNames5, "".concat(prefixCls, "-menu-empty"), options.length === 1 && options[0].value === 'ANT_CASCADER_NOT_FOUND'), _classNames5));
return /*#__PURE__*/React.createElement(RcCascader, _extends({}, rest, {
prefixCls: prefixCls,
getPopupContainer: getPopupContainer,
options: options,
value: value,
popupVisible: state.popupVisible,
onPopupVisibleChange: _this.handlePopupVisibleChange,
onChange: _this.handleChange,
dropdownMenuColumnStyle: dropdownMenuColumnStyle,
expandIcon: expandIconNode,
loadingIcon: loadingIcon,
popupClassName: rcCascaderPopupClassName,
popupPlacement: _this.getPopupPlacement(direction),
dropdownRender: dropdownRender
}), input);
});
};
_this.state = {
value: props.value || props.defaultValue || [],
inputValue: '',
inputFocused: false,
popupVisible: props.popupVisible,
flattenOptions: props.showSearch ? flattenTree(props.options, props) : undefined,
prevProps: props
};
return _this;
}
_createClass(Cascader, [{
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (this.clearSelectionTimeout) {
clearTimeout(this.clearSelectionTimeout);
}
}
}, {
key: "getLabel",
value: function getLabel() {
var _this$props = this.props,
options = _this$props.options,
_this$props$displayRe = _this$props.displayRender,
displayRender = _this$props$displayRe === void 0 ? defaultDisplayRender : _this$props$displayRe;
var names = getFilledFieldNames(this.props);
var value = this.state.value;
var unwrappedValue = Array.isArray(value[0]) ? value[0] : value;
var selectedOptions = arrayTreeFilter(options, function (o, level) {
return o[names.value] === unwrappedValue[level];
}, {
childrenKeyName: names.children
});
var label = selectedOptions.length ? selectedOptions.map(function (o) {
return o[names.label];
}) : value;
return displayRender(label, selectedOptions);
}
}, {
key: "generateFilteredOptions",
value: function generateFilteredOptions(prefixCls, renderEmpty) {
var _this2 = this;
var _this$props2 = this.props,
showSearch = _this$props2.showSearch,
notFoundContent = _this$props2.notFoundContent;
var names = getFilledFieldNames(this.props);
var _showSearch$filter = showSearch.filter,
filter = _showSearch$filter === void 0 ? defaultFilterOption : _showSearch$filter,
_showSearch$render = showSearch.render,
render = _showSearch$render === void 0 ? defaultRenderFilteredOption : _showSearch$render,
_showSearch$sort = showSearch.sort,
sort = _showSearch$sort === void 0 ? defaultSortFilteredOption : _showSearch$sort,
_showSearch$limit = showSearch.limit,
limit = _showSearch$limit === void 0 ? defaultLimit : _showSearch$limit;
var _this$state2 = this.state,
_this$state2$flattenO = _this$state2.flattenOptions,
flattenOptions = _this$state2$flattenO === void 0 ? [] : _this$state2$flattenO,
inputValue = _this$state2.inputValue; // Limit the filter if needed
var filtered;
if (limit > 0) {
filtered = [];
var matchCount = 0; // Perf optimization to filter items only below the limit
flattenOptions.some(function (path) {
var match = filter(_this2.state.inputValue, path, names);
if (match) {
filtered.push(path);
matchCount += 1;
}
return matchCount >= limit;
});
} else {
devWarning(typeof limit !== 'number', 'Cascader', "'limit' of showSearch should be positive number or false.");
filtered = flattenOptions.filter(function (path) {
return filter(_this2.state.inputValue, path, names);
});
}
filtered = filtered.sort(function (a, b) {
return sort(a, b, inputValue, names);
});
if (filtered.length > 0) {
// Fix issue: https://github.com/ant-design/ant-design/issues/26554
var field = names.value === names.label ? keepFilteredValueField : names.value;
return filtered.map(function (path) {
var _ref4;
return _ref4 = {
__IS_FILTERED_OPTION: true,
path: path
}, _defineProperty(_ref4, field, path.map(function (o) {
return o[names.value];
})), _defineProperty(_ref4, names.label, render(inputValue, path, prefixCls, names)), _defineProperty(_ref4, "disabled", path.some(function (o) {
return !!o.disabled;
})), _defineProperty(_ref4, "isEmptyNode", true), _ref4;
});
}
return [getEmptyNode(renderEmpty, names, notFoundContent)];
}
}, {
key: "focus",
value: function focus() {
this.input.focus();
}
}, {
key: "blur",
value: function blur() {
this.input.blur();
}
}, {
key: "getPopupPlacement",
value: function getPopupPlacement() {
var direction = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'ltr';
var popupPlacement = this.props.popupPlacement;
if (popupPlacement !== undefined) {
return popupPlacement;
}
return direction === 'rtl' ? 'bottomRight' : 'bottomLeft';
}
}, {
key: "render",
value: function render() {
var _this3 = this;
return /*#__PURE__*/React.createElement(ConfigConsumer, null, function (configArgument) {
return /*#__PURE__*/React.createElement(LocaleReceiver, null, function (locale) {
return _this3.renderCascader(configArgument, locale);
});
});
}
}], [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(nextProps, _ref5) {
var prevProps = _ref5.prevProps;
var newState = {
prevProps: nextProps
};
if ('value' in nextProps) {
newState.value = nextProps.value || [];
}
if ('popupVisible' in nextProps) {
newState.popupVisible = nextProps.popupVisible;
}
if (nextProps.showSearch && prevProps.options !== nextProps.options) {
newState.flattenOptions = flattenTree(nextProps.options, nextProps);
}
if (process.env.NODE_ENV !== 'production' && nextProps.options) {
warningValueNotExist(nextProps.options, getFieldNames(nextProps));
}
return newState;
}
}]);
return Cascader;
}(React.Component);
Cascader.defaultProps = {
transitionName: 'slide-up',
options: [],
disabled: false,
allowClear: true,
bordered: true
};
export default Cascader;