2020-07-08 10:37:44 +00:00
|
|
|
|
import { inject, provide } from 'vue';
|
2019-01-12 03:33:27 +00:00
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
|
import VcCascader from '../vc-cascader';
|
|
|
|
|
import arrayTreeFilter from 'array-tree-filter';
|
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
import omit from 'omit.js';
|
|
|
|
|
import KeyCode from '../_util/KeyCode';
|
|
|
|
|
import Input from '../input';
|
2020-03-28 07:28:06 +00:00
|
|
|
|
import CloseCircleFilled from '@ant-design/icons-vue/CloseCircleFilled';
|
|
|
|
|
import DownOutlined from '@ant-design/icons-vue/DownOutlined';
|
|
|
|
|
import RightOutlined from '@ant-design/icons-vue/RightOutlined';
|
|
|
|
|
import RedoOutlined from '@ant-design/icons-vue/RedoOutlined';
|
2019-01-12 03:33:27 +00:00
|
|
|
|
import {
|
|
|
|
|
hasProp,
|
|
|
|
|
getOptionProps,
|
|
|
|
|
isValidElement,
|
2020-07-08 10:37:44 +00:00
|
|
|
|
getComponent,
|
|
|
|
|
splitAttrs,
|
|
|
|
|
findDOMNode,
|
2020-07-08 14:28:09 +00:00
|
|
|
|
getSlot,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
} from '../_util/props-util';
|
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
|
|
|
|
import { cloneElement } from '../_util/vnode';
|
|
|
|
|
import warning from '../_util/warning';
|
2019-03-14 13:21:58 +00:00
|
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2018-03-03 11:14:03 +00:00
|
|
|
|
|
|
|
|
|
const CascaderOptionType = PropTypes.shape({
|
2019-03-17 06:42:15 +00:00
|
|
|
|
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
2018-09-05 13:28:54 +00:00
|
|
|
|
label: PropTypes.any,
|
2018-03-03 11:14:03 +00:00
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
|
children: PropTypes.array,
|
2019-03-17 06:42:15 +00:00
|
|
|
|
key: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
2019-01-12 03:33:27 +00:00
|
|
|
|
}).loose;
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
|
|
const FieldNamesType = PropTypes.shape({
|
|
|
|
|
value: PropTypes.string.isRequired,
|
|
|
|
|
label: PropTypes.string.isRequired,
|
|
|
|
|
children: PropTypes.string,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
}).loose;
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const CascaderExpandTrigger = PropTypes.oneOf(['click', 'hover']);
|
2018-03-03 11:14:03 +00:00
|
|
|
|
|
|
|
|
|
const ShowSearchType = PropTypes.shape({
|
|
|
|
|
filter: PropTypes.func,
|
|
|
|
|
render: PropTypes.func,
|
|
|
|
|
sort: PropTypes.func,
|
|
|
|
|
matchInputWidth: PropTypes.bool,
|
2019-01-03 12:47:39 +00:00
|
|
|
|
limit: PropTypes.oneOfType([Boolean, Number]),
|
2019-01-12 03:33:27 +00:00
|
|
|
|
}).loose;
|
|
|
|
|
function noop() {}
|
2018-03-03 11:14:03 +00:00
|
|
|
|
|
|
|
|
|
const CascaderProps = {
|
|
|
|
|
/** 可选项数据源 */
|
|
|
|
|
options: PropTypes.arrayOf(CascaderOptionType).def([]),
|
|
|
|
|
/** 默认的选中项 */
|
2019-03-17 06:42:15 +00:00
|
|
|
|
defaultValue: PropTypes.array,
|
2018-03-03 11:14:03 +00:00
|
|
|
|
/** 指定选中项 */
|
2019-03-17 06:42:15 +00:00
|
|
|
|
value: PropTypes.array,
|
2018-03-03 11:14:03 +00:00
|
|
|
|
/** 选择完成后的回调 */
|
|
|
|
|
// onChange?: (value: string[], selectedOptions?: CascaderOptionType[]) => void;
|
|
|
|
|
/** 选择后展示的渲染函数 */
|
|
|
|
|
displayRender: PropTypes.func,
|
|
|
|
|
transitionName: PropTypes.string.def('slide-up'),
|
2020-03-07 11:45:13 +00:00
|
|
|
|
popupStyle: PropTypes.object.def(() => ({})),
|
2018-03-03 11:14:03 +00:00
|
|
|
|
/** 自定义浮层类名 */
|
|
|
|
|
popupClassName: PropTypes.string,
|
|
|
|
|
/** 浮层预设位置:`bottomLeft` `bottomRight` `topLeft` `topRight` */
|
2019-01-12 03:33:27 +00:00
|
|
|
|
popupPlacement: PropTypes.oneOf(['bottomLeft', 'bottomRight', 'topLeft', 'topRight']).def(
|
|
|
|
|
'bottomLeft',
|
|
|
|
|
),
|
2018-03-03 11:14:03 +00:00
|
|
|
|
/** 输入框占位文本*/
|
|
|
|
|
placeholder: PropTypes.string.def('Please select'),
|
|
|
|
|
/** 输入框大小,可选 `large` `default` `small` */
|
|
|
|
|
size: PropTypes.oneOf(['large', 'default', 'small']),
|
|
|
|
|
/** 禁用*/
|
|
|
|
|
disabled: PropTypes.bool.def(false),
|
|
|
|
|
/** 是否支持清除*/
|
|
|
|
|
allowClear: PropTypes.bool.def(true),
|
2018-06-07 06:40:10 +00:00
|
|
|
|
showSearch: PropTypes.oneOfType([Boolean, ShowSearchType]),
|
2019-09-03 09:36:50 +00:00
|
|
|
|
notFoundContent: PropTypes.any,
|
2018-03-03 11:14:03 +00:00
|
|
|
|
loadData: PropTypes.func,
|
|
|
|
|
/** 次级菜单的展开方式,可选 'click' 和 'hover' */
|
|
|
|
|
expandTrigger: CascaderExpandTrigger,
|
|
|
|
|
/** 当此项为 true 时,点选每级菜单选项值都会发生变化 */
|
|
|
|
|
changeOnSelect: PropTypes.bool,
|
|
|
|
|
/** 浮层可见变化时回调 */
|
|
|
|
|
// onPopupVisibleChange?: (popupVisible: boolean) => void;
|
2019-03-14 13:21:58 +00:00
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
|
inputPrefixCls: PropTypes.string,
|
2018-03-03 11:14:03 +00:00
|
|
|
|
getPopupContainer: PropTypes.func,
|
|
|
|
|
popupVisible: PropTypes.bool,
|
2018-09-05 13:28:54 +00:00
|
|
|
|
fieldNames: FieldNamesType,
|
2020-07-17 09:36:58 +00:00
|
|
|
|
autofocus: PropTypes.bool,
|
2018-11-29 14:20:17 +00:00
|
|
|
|
suffixIcon: PropTypes.any,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
};
|
2018-03-03 11:14:03 +00:00
|
|
|
|
|
2019-01-03 12:47:39 +00:00
|
|
|
|
// We limit the filtered item count by default
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const defaultLimit = 50;
|
2019-01-03 12:47:39 +00:00
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
function defaultFilterOption(inputValue, path, names) {
|
|
|
|
|
return path.some(option => option[names.label].indexOf(inputValue) > -1);
|
2018-03-03 11:14:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
function defaultSortFilteredOption(a, b, inputValue, names) {
|
|
|
|
|
function callback(elem) {
|
|
|
|
|
return elem[names.label].indexOf(inputValue) > -1;
|
2018-03-03 11:14:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
return a.findIndex(callback) - b.findIndex(callback);
|
2018-03-03 11:14:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
function getFilledFieldNames({ fieldNames = {} }) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const names = {
|
|
|
|
|
children: fieldNames.children || 'children',
|
|
|
|
|
label: fieldNames.label || 'label',
|
|
|
|
|
value: fieldNames.value || 'value',
|
2019-01-12 03:33:27 +00:00
|
|
|
|
};
|
|
|
|
|
return names;
|
2018-09-05 13:28:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
function flattenTree(options = [], props, ancestor = []) {
|
|
|
|
|
const names = getFilledFieldNames(props);
|
|
|
|
|
let flattenOptions = [];
|
|
|
|
|
const childrenName = names.children;
|
2019-01-03 12:47:39 +00:00
|
|
|
|
options.forEach(option => {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const path = ancestor.concat(option);
|
2019-01-03 12:47:39 +00:00
|
|
|
|
if (props.changeOnSelect || !option[childrenName] || !option[childrenName].length) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
flattenOptions.push(path);
|
2019-01-03 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
if (option[childrenName]) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
flattenOptions = flattenOptions.concat(flattenTree(option[childrenName], props, path));
|
2019-01-03 12:47:39 +00:00
|
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
|
});
|
|
|
|
|
return flattenOptions;
|
2019-01-03 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const defaultDisplayRender = ({ labels }) => labels.join(' / ');
|
2018-03-03 11:14:03 +00:00
|
|
|
|
|
2018-09-19 05:21:57 +00:00
|
|
|
|
const Cascader = {
|
2018-05-06 12:37:39 +00:00
|
|
|
|
inheritAttrs: false,
|
2018-04-08 13:17:20 +00:00
|
|
|
|
name: 'ACascader',
|
2018-03-03 11:14:03 +00:00
|
|
|
|
mixins: [BaseMixin],
|
|
|
|
|
props: CascaderProps,
|
2020-07-08 10:37:44 +00:00
|
|
|
|
// model: {
|
|
|
|
|
// prop: 'value',
|
|
|
|
|
// event: 'change',
|
|
|
|
|
// },
|
|
|
|
|
created() {
|
|
|
|
|
provide('savePopupRef', this.savePopupRef);
|
2018-03-04 08:02:46 +00:00
|
|
|
|
},
|
2020-07-08 10:37:44 +00:00
|
|
|
|
setup() {
|
2019-07-04 10:12:52 +00:00
|
|
|
|
return {
|
2020-07-08 10:37:44 +00:00
|
|
|
|
configProvider: inject('configProvider', ConfigConsumerProps),
|
|
|
|
|
localeData: inject('localeData', {}),
|
2019-07-04 10:12:52 +00:00
|
|
|
|
};
|
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
data() {
|
|
|
|
|
this.cachedOptions = [];
|
|
|
|
|
const { value, defaultValue, popupVisible, showSearch, options } = this;
|
2018-03-03 11:14:03 +00:00
|
|
|
|
return {
|
|
|
|
|
sValue: value || defaultValue || [],
|
|
|
|
|
inputValue: '',
|
|
|
|
|
inputFocused: false,
|
|
|
|
|
sPopupVisible: popupVisible,
|
2018-11-29 14:20:17 +00:00
|
|
|
|
flattenOptions: showSearch ? flattenTree(options, this.$props) : undefined,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
};
|
2018-03-03 11:14:03 +00:00
|
|
|
|
},
|
2020-07-08 10:37:44 +00:00
|
|
|
|
// mounted() {
|
|
|
|
|
// this.$nextTick(() => {
|
2020-07-17 09:36:58 +00:00
|
|
|
|
// if (this.autofocus && !this.showSearch && !this.disabled) {
|
2020-07-08 10:37:44 +00:00
|
|
|
|
// this.$refs.picker.focus();
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
// },
|
2018-03-03 11:14:03 +00:00
|
|
|
|
watch: {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
value(val) {
|
|
|
|
|
this.setState({ sValue: val || [] });
|
2018-03-03 11:14:03 +00:00
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
popupVisible(val) {
|
|
|
|
|
this.setState({ sPopupVisible: val });
|
2018-03-03 11:14:03 +00:00
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
options(val) {
|
2018-03-03 11:14:03 +00:00
|
|
|
|
if (this.showSearch) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
this.setState({ flattenOptions: flattenTree(val, this.$props) });
|
2018-03-03 11:14:03 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
2019-07-04 10:12:52 +00:00
|
|
|
|
savePopupRef(ref) {
|
|
|
|
|
this.popupRef = ref;
|
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
highlightKeyword(str, keyword, prefixCls) {
|
|
|
|
|
return str
|
|
|
|
|
.split(keyword)
|
|
|
|
|
.map((node, index) =>
|
|
|
|
|
index === 0
|
|
|
|
|
? node
|
|
|
|
|
: [<span class={`${prefixCls}-menu-item-keyword`}>{keyword}</span>, node],
|
|
|
|
|
);
|
2018-03-03 11:14:03 +00:00
|
|
|
|
},
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
defaultRenderFilteredOption({ inputValue, path, prefixCls, names }) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
return path.map((option, index) => {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const label = option[names.label];
|
|
|
|
|
const node =
|
|
|
|
|
label.indexOf(inputValue) > -1
|
|
|
|
|
? this.highlightKeyword(label, inputValue, prefixCls)
|
|
|
|
|
: label;
|
|
|
|
|
return index === 0 ? node : [' / ', node];
|
|
|
|
|
});
|
2018-03-03 11:14:03 +00:00
|
|
|
|
},
|
2020-07-08 10:37:44 +00:00
|
|
|
|
saveInput(node) {
|
|
|
|
|
this.input = node;
|
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
handleChange(value, selectedOptions) {
|
|
|
|
|
this.setState({ inputValue: '' });
|
2018-03-03 11:14:03 +00:00
|
|
|
|
if (selectedOptions[0].__IS_FILTERED_OPTION) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const unwrappedValue = value[0];
|
|
|
|
|
const unwrappedSelectedOptions = selectedOptions[0].path;
|
|
|
|
|
this.setValue(unwrappedValue, unwrappedSelectedOptions);
|
|
|
|
|
return;
|
2018-03-03 11:14:03 +00:00
|
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
|
this.setValue(value, selectedOptions);
|
2018-03-03 11:14:03 +00:00
|
|
|
|
},
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
handlePopupVisibleChange(popupVisible) {
|
2018-03-03 11:14:03 +00:00
|
|
|
|
if (!hasProp(this, 'popupVisible')) {
|
2019-01-03 12:47:39 +00:00
|
|
|
|
this.setState(state => ({
|
2018-03-03 11:14:03 +00:00
|
|
|
|
sPopupVisible: popupVisible,
|
|
|
|
|
inputFocused: popupVisible,
|
2019-01-03 12:47:39 +00:00
|
|
|
|
inputValue: popupVisible ? state.inputValue : '',
|
2019-01-12 03:33:27 +00:00
|
|
|
|
}));
|
2018-03-03 11:14:03 +00:00
|
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
|
this.$emit('popupVisibleChange', popupVisible);
|
2018-03-03 11:14:03 +00:00
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
handleInputFocus(e) {
|
|
|
|
|
this.$emit('focus', e);
|
2018-06-07 06:40:10 +00:00
|
|
|
|
},
|
2018-03-03 11:14:03 +00:00
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
handleInputBlur(e) {
|
2018-03-03 11:14:03 +00:00
|
|
|
|
this.setState({
|
|
|
|
|
inputFocused: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
});
|
|
|
|
|
this.$emit('blur', e);
|
2018-03-03 11:14:03 +00:00
|
|
|
|
},
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
handleInputClick(e) {
|
|
|
|
|
const { inputFocused, sPopupVisible } = this;
|
2018-03-03 11:14:03 +00:00
|
|
|
|
// Prevent `Trigger` behaviour.
|
|
|
|
|
if (inputFocused || sPopupVisible) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
e.stopPropagation();
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (e.nativeEvent && e.nativeEvent.stopImmediatePropagation) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
e.nativeEvent.stopImmediatePropagation();
|
2018-09-05 13:28:54 +00:00
|
|
|
|
}
|
2018-03-03 11:14:03 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
handleKeyDown(e) {
|
2019-11-19 08:21:19 +00:00
|
|
|
|
if (e.keyCode === KeyCode.BACKSPACE || e.keyCode === KeyCode.SPACE) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
e.stopPropagation();
|
2018-03-03 11:14:03 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
handleInputChange(e) {
|
|
|
|
|
const inputValue = e.target.value;
|
|
|
|
|
this.setState({ inputValue });
|
2020-04-30 08:15:05 +00:00
|
|
|
|
this.$emit('search', inputValue);
|
2018-03-03 11:14:03 +00:00
|
|
|
|
},
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
setValue(value, selectedOptions) {
|
2018-03-03 11:14:03 +00:00
|
|
|
|
if (!hasProp(this, 'value')) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
this.setState({ sValue: value });
|
2018-03-03 11:14:03 +00:00
|
|
|
|
}
|
2020-07-08 10:37:44 +00:00
|
|
|
|
this.$emit('update:value', value);
|
2019-01-12 03:33:27 +00:00
|
|
|
|
this.$emit('change', value, selectedOptions);
|
2018-03-03 11:14:03 +00:00
|
|
|
|
},
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
getLabel() {
|
2020-07-08 10:37:44 +00:00
|
|
|
|
const { options } = this;
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const names = getFilledFieldNames(this.$props);
|
2020-07-08 14:28:09 +00:00
|
|
|
|
const displayRender = getComponent(this, 'displayRender', {}, false) || defaultDisplayRender;
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const value = this.sValue;
|
|
|
|
|
const unwrappedValue = Array.isArray(value[0]) ? value[0] : value;
|
|
|
|
|
const selectedOptions = arrayTreeFilter(
|
|
|
|
|
options,
|
2018-09-05 13:28:54 +00:00
|
|
|
|
(o, level) => o[names.value] === unwrappedValue[level],
|
|
|
|
|
{ childrenKeyName: names.children },
|
2019-01-12 03:33:27 +00:00
|
|
|
|
);
|
|
|
|
|
const labels = selectedOptions.map(o => o[names.label]);
|
|
|
|
|
return displayRender({ labels, selectedOptions });
|
2018-03-03 11:14:03 +00:00
|
|
|
|
},
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
clearSelection(e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
2018-03-03 11:14:03 +00:00
|
|
|
|
if (!this.inputValue) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
this.setValue([]);
|
|
|
|
|
this.handlePopupVisibleChange(false);
|
2018-03-03 11:14:03 +00:00
|
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
this.setState({ inputValue: '' });
|
2018-03-03 11:14:03 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-14 13:21:58 +00:00
|
|
|
|
generateFilteredOptions(prefixCls, renderEmpty) {
|
2020-07-08 10:37:44 +00:00
|
|
|
|
const { showSearch, notFoundContent } = this;
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const names = getFilledFieldNames(this.$props);
|
2018-03-03 11:14:03 +00:00
|
|
|
|
const {
|
|
|
|
|
filter = defaultFilterOption,
|
2018-03-04 09:11:32 +00:00
|
|
|
|
// render = this.defaultRenderFilteredOption,
|
2018-03-03 11:14:03 +00:00
|
|
|
|
sort = defaultSortFilteredOption,
|
2019-01-03 12:47:39 +00:00
|
|
|
|
limit = defaultLimit,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
} = showSearch;
|
|
|
|
|
const render =
|
2020-07-08 10:37:44 +00:00
|
|
|
|
showSearch.render ||
|
|
|
|
|
getComponent(this, 'showSearchRender') ||
|
|
|
|
|
this.defaultRenderFilteredOption;
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const { flattenOptions = [], inputValue } = this.$data;
|
2019-01-03 12:47:39 +00:00
|
|
|
|
|
|
|
|
|
// Limit the filter if needed
|
2019-01-12 03:33:27 +00:00
|
|
|
|
let filtered;
|
2019-01-03 12:47:39 +00:00
|
|
|
|
if (limit > 0) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
filtered = [];
|
|
|
|
|
let matchCount = 0;
|
2019-01-03 12:47:39 +00:00
|
|
|
|
|
|
|
|
|
// Perf optimization to filter items only below the limit
|
|
|
|
|
flattenOptions.some(path => {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const match = filter(inputValue, path, names);
|
2019-01-03 12:47:39 +00:00
|
|
|
|
if (match) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
filtered.push(path);
|
|
|
|
|
matchCount += 1;
|
2019-01-03 12:47:39 +00:00
|
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
|
return matchCount >= limit;
|
|
|
|
|
});
|
2019-01-03 12:47:39 +00:00
|
|
|
|
} else {
|
|
|
|
|
warning(
|
|
|
|
|
typeof limit !== 'number',
|
2020-03-07 11:45:13 +00:00
|
|
|
|
'Cascader',
|
2019-01-03 12:47:39 +00:00
|
|
|
|
"'limit' of showSearch in Cascader should be positive number or false.",
|
2019-01-12 03:33:27 +00:00
|
|
|
|
);
|
|
|
|
|
filtered = flattenOptions.filter(path => filter(inputValue, path, names));
|
2019-01-03 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
filtered.sort((a, b) => sort(a, b, inputValue, names));
|
2018-03-03 11:14:03 +00:00
|
|
|
|
|
|
|
|
|
if (filtered.length > 0) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
return filtered.map(path => {
|
2018-03-03 11:14:03 +00:00
|
|
|
|
return {
|
|
|
|
|
__IS_FILTERED_OPTION: true,
|
|
|
|
|
path,
|
2018-09-05 13:28:54 +00:00
|
|
|
|
[names.label]: render({ inputValue, path, prefixCls, names }),
|
2019-01-12 03:33:27 +00:00
|
|
|
|
[names.value]: path.map(o => o[names.value]),
|
|
|
|
|
disabled: path.some(o => !!o.disabled),
|
|
|
|
|
};
|
|
|
|
|
});
|
2018-03-03 11:14:03 +00:00
|
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
|
return [
|
2019-03-14 13:21:58 +00:00
|
|
|
|
{
|
2020-07-18 13:40:26 +00:00
|
|
|
|
[names.label]: notFoundContent || renderEmpty('Cascader'),
|
2019-03-14 13:21:58 +00:00
|
|
|
|
[names.value]: 'ANT_CASCADER_NOT_FOUND',
|
|
|
|
|
disabled: true,
|
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
];
|
2018-03-03 11:14:03 +00:00
|
|
|
|
},
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
focus() {
|
2020-07-08 10:37:44 +00:00
|
|
|
|
this.input && this.input.focus();
|
2018-03-03 11:14:03 +00:00
|
|
|
|
},
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
blur() {
|
2020-07-08 10:37:44 +00:00
|
|
|
|
this.input && this.input.blur();
|
2018-03-03 11:14:03 +00:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
render() {
|
2020-07-08 14:28:09 +00:00
|
|
|
|
const { sPopupVisible, inputValue, configProvider, localeData } = this;
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const { sValue: value, inputFocused } = this.$data;
|
|
|
|
|
const props = getOptionProps(this);
|
2020-07-08 14:28:09 +00:00
|
|
|
|
let suffixIcon = getComponent(this, 'suffixIcon');
|
2019-01-12 03:33:27 +00:00
|
|
|
|
suffixIcon = Array.isArray(suffixIcon) ? suffixIcon[0] : suffixIcon;
|
|
|
|
|
const { getPopupContainer: getContextPopupContainer } = configProvider;
|
2018-03-03 11:14:03 +00:00
|
|
|
|
const {
|
2019-03-14 13:21:58 +00:00
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
|
inputPrefixCls: customizeInputPrefixCls,
|
2019-01-03 12:47:39 +00:00
|
|
|
|
placeholder = localeData.placeholder,
|
|
|
|
|
size,
|
|
|
|
|
disabled,
|
|
|
|
|
allowClear,
|
|
|
|
|
showSearch = false,
|
2020-01-09 14:56:50 +00:00
|
|
|
|
notFoundContent,
|
2019-01-03 12:47:39 +00:00
|
|
|
|
...otherProps
|
2019-01-12 03:33:27 +00:00
|
|
|
|
} = props;
|
2020-07-08 10:37:44 +00:00
|
|
|
|
const { onEvents, extraAttrs } = splitAttrs(this.$attrs);
|
|
|
|
|
const { class: className, style, ...restAttrs } = extraAttrs;
|
2019-09-11 14:35:25 +00:00
|
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
|
|
|
|
const renderEmpty = this.configProvider.renderEmpty;
|
2019-03-14 13:21:58 +00:00
|
|
|
|
const prefixCls = getPrefixCls('cascader', customizePrefixCls);
|
|
|
|
|
const inputPrefixCls = getPrefixCls('input', customizeInputPrefixCls);
|
|
|
|
|
|
2018-03-03 11:14:03 +00:00
|
|
|
|
const sizeCls = classNames({
|
|
|
|
|
[`${inputPrefixCls}-lg`]: size === 'large',
|
|
|
|
|
[`${inputPrefixCls}-sm`]: size === 'small',
|
2019-01-12 03:33:27 +00:00
|
|
|
|
});
|
|
|
|
|
const clearIcon =
|
|
|
|
|
(allowClear && !disabled && value.length > 0) || inputValue ? (
|
2020-03-28 07:28:06 +00:00
|
|
|
|
<CloseCircleFilled
|
2019-01-12 03:33:27 +00:00
|
|
|
|
class={`${prefixCls}-picker-clear`}
|
|
|
|
|
onClick={this.clearSelection}
|
|
|
|
|
key="clear-icon"
|
|
|
|
|
/>
|
|
|
|
|
) : null;
|
2018-03-03 11:14:03 +00:00
|
|
|
|
const arrowCls = classNames({
|
|
|
|
|
[`${prefixCls}-picker-arrow`]: true,
|
|
|
|
|
[`${prefixCls}-picker-arrow-expand`]: sPopupVisible,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
});
|
2020-07-08 10:37:44 +00:00
|
|
|
|
const pickerCls = classNames(className, `${prefixCls}-picker`, {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
[`${prefixCls}-picker-with-value`]: inputValue,
|
|
|
|
|
[`${prefixCls}-picker-disabled`]: disabled,
|
|
|
|
|
[`${prefixCls}-picker-${size}`]: !!size,
|
|
|
|
|
[`${prefixCls}-picker-show-search`]: !!showSearch,
|
|
|
|
|
[`${prefixCls}-picker-focused`]: inputFocused,
|
|
|
|
|
});
|
2018-03-03 11:14:03 +00:00
|
|
|
|
|
|
|
|
|
// Fix bug of https://github.com/facebook/react/pull/5004
|
|
|
|
|
// and https://fb.me/react-unknown-prop
|
|
|
|
|
const tempInputProps = omit(otherProps, [
|
|
|
|
|
'options',
|
|
|
|
|
'popupPlacement',
|
|
|
|
|
'transitionName',
|
|
|
|
|
'displayRender',
|
|
|
|
|
'changeOnSelect',
|
|
|
|
|
'expandTrigger',
|
|
|
|
|
'popupVisible',
|
|
|
|
|
'getPopupContainer',
|
|
|
|
|
'loadData',
|
|
|
|
|
'popupClassName',
|
|
|
|
|
'filterOption',
|
|
|
|
|
'renderFilteredOption',
|
|
|
|
|
'sortFilteredOption',
|
|
|
|
|
'notFoundContent',
|
2018-03-04 08:02:46 +00:00
|
|
|
|
'defaultValue',
|
2018-09-05 13:28:54 +00:00
|
|
|
|
'fieldNames',
|
2019-01-12 03:33:27 +00:00
|
|
|
|
]);
|
2018-03-03 11:14:03 +00:00
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
let options = props.options;
|
2020-01-09 14:56:50 +00:00
|
|
|
|
const names = getFilledFieldNames(this.$props);
|
|
|
|
|
if (options && options.length > 0) {
|
|
|
|
|
if (inputValue) {
|
|
|
|
|
options = this.generateFilteredOptions(prefixCls, renderEmpty);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
options = [
|
|
|
|
|
{
|
2020-07-08 10:37:44 +00:00
|
|
|
|
[names.label]: notFoundContent || renderEmpty('Cascader'),
|
2020-01-09 14:56:50 +00:00
|
|
|
|
[names.value]: 'ANT_CASCADER_NOT_FOUND',
|
|
|
|
|
disabled: true,
|
|
|
|
|
},
|
|
|
|
|
];
|
2018-03-03 11:14:03 +00:00
|
|
|
|
}
|
2020-01-09 14:56:50 +00:00
|
|
|
|
|
2018-03-03 11:14:03 +00:00
|
|
|
|
// Dropdown menu should keep previous status until it is fully closed.
|
|
|
|
|
if (!sPopupVisible) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
options = this.cachedOptions;
|
2018-03-03 11:14:03 +00:00
|
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
this.cachedOptions = options;
|
2018-03-03 11:14:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const dropdownMenuColumnStyle = {};
|
|
|
|
|
const isNotFound =
|
|
|
|
|
(options || []).length === 1 && options[0].value === 'ANT_CASCADER_NOT_FOUND';
|
2018-03-03 11:14:03 +00:00
|
|
|
|
if (isNotFound) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
dropdownMenuColumnStyle.height = 'auto'; // Height of one row.
|
2018-03-03 11:14:03 +00:00
|
|
|
|
}
|
|
|
|
|
// The default value of `matchInputWidth` is `true`
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const resultListMatchInputWidth = showSearch.matchInputWidth !== false;
|
2020-01-09 14:56:50 +00:00
|
|
|
|
if (resultListMatchInputWidth && (inputValue || isNotFound) && this.$refs.input) {
|
2020-07-08 10:37:44 +00:00
|
|
|
|
dropdownMenuColumnStyle.width = findDOMNode(this.input).offsetWidth + 'px';
|
2018-03-03 11:14:03 +00:00
|
|
|
|
}
|
2018-06-07 06:40:10 +00:00
|
|
|
|
// showSearch时,focus、blur在input上触发,反之在ref='picker'上触发
|
2018-03-03 11:14:03 +00:00
|
|
|
|
const inputProps = {
|
2020-07-08 10:37:44 +00:00
|
|
|
|
...restAttrs,
|
|
|
|
|
...tempInputProps,
|
|
|
|
|
prefixCls: inputPrefixCls,
|
|
|
|
|
placeholder: value && value.length > 0 ? undefined : placeholder,
|
|
|
|
|
value: inputValue,
|
|
|
|
|
disabled,
|
2020-07-17 09:13:30 +00:00
|
|
|
|
readonly: !showSearch,
|
2020-07-17 09:25:23 +00:00
|
|
|
|
autocomplete: 'off',
|
2018-03-03 11:14:03 +00:00
|
|
|
|
class: `${prefixCls}-input ${sizeCls}`,
|
2020-07-08 10:37:44 +00:00
|
|
|
|
onFocus: showSearch ? this.handleInputFocus : noop,
|
|
|
|
|
onClick: showSearch ? this.handleInputClick : noop,
|
|
|
|
|
onBlur: showSearch ? this.handleInputBlur : noop,
|
|
|
|
|
onKeydown: this.handleKeyDown,
|
|
|
|
|
onChange: showSearch ? this.handleInputChange : noop,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
};
|
2020-07-08 14:28:09 +00:00
|
|
|
|
const children = getSlot(this);
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const inputIcon = (suffixIcon &&
|
|
|
|
|
(isValidElement(suffixIcon) ? (
|
|
|
|
|
cloneElement(suffixIcon, {
|
|
|
|
|
class: {
|
|
|
|
|
[`${prefixCls}-picker-arrow`]: true,
|
2018-11-29 14:20:17 +00:00
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
})
|
|
|
|
|
) : (
|
|
|
|
|
<span class={`${prefixCls}-picker-arrow`}>{suffixIcon}</span>
|
2020-03-28 07:28:06 +00:00
|
|
|
|
))) || <DownOutlined class={arrowCls} />;
|
2019-01-12 03:33:27 +00:00
|
|
|
|
|
|
|
|
|
const input = children.length ? (
|
|
|
|
|
children
|
|
|
|
|
) : (
|
2020-07-08 10:37:44 +00:00
|
|
|
|
<span class={pickerCls} style={style}>
|
2019-01-12 03:33:27 +00:00
|
|
|
|
{showSearch ? <span class={`${prefixCls}-picker-label`}>{this.getLabel()}</span> : null}
|
2020-07-08 10:37:44 +00:00
|
|
|
|
<Input {...inputProps} ref={this.saveInput} />
|
2019-01-12 03:33:27 +00:00
|
|
|
|
{!showSearch ? <span class={`${prefixCls}-picker-label`}>{this.getLabel()}</span> : null}
|
2018-03-03 11:14:03 +00:00
|
|
|
|
{clearIcon}
|
2018-11-29 14:20:17 +00:00
|
|
|
|
{inputIcon}
|
|
|
|
|
</span>
|
2019-01-12 03:33:27 +00:00
|
|
|
|
);
|
2018-11-29 14:20:17 +00:00
|
|
|
|
|
2020-03-28 07:28:06 +00:00
|
|
|
|
const expandIcon = <RightOutlined />;
|
2018-11-29 14:20:17 +00:00
|
|
|
|
|
|
|
|
|
const loadingIcon = (
|
|
|
|
|
<span class={`${prefixCls}-menu-item-loading-icon`}>
|
2020-03-28 07:28:06 +00:00
|
|
|
|
<RedoOutlined spin />
|
2018-03-03 11:14:03 +00:00
|
|
|
|
</span>
|
2019-01-12 03:33:27 +00:00
|
|
|
|
);
|
|
|
|
|
const getPopupContainer = props.getPopupContainer || getContextPopupContainer;
|
2018-03-03 11:14:03 +00:00
|
|
|
|
const cascaderProps = {
|
2020-07-08 10:37:44 +00:00
|
|
|
|
...props,
|
|
|
|
|
getPopupContainer,
|
|
|
|
|
options,
|
|
|
|
|
prefixCls,
|
|
|
|
|
value,
|
|
|
|
|
popupVisible: sPopupVisible,
|
|
|
|
|
dropdownMenuColumnStyle,
|
|
|
|
|
expandIcon,
|
|
|
|
|
loadingIcon,
|
|
|
|
|
...onEvents,
|
|
|
|
|
onPopupVisibleChange: this.handlePopupVisibleChange,
|
|
|
|
|
onChange: this.handleChange,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
};
|
|
|
|
|
return <VcCascader {...cascaderProps}>{input}</VcCascader>;
|
2018-03-03 11:14:03 +00:00
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
};
|
2018-03-03 11:14:03 +00:00
|
|
|
|
|
2020-07-18 13:40:26 +00:00
|
|
|
|
Cascader.install = function(app) {
|
|
|
|
|
app.component(Cascader.name, Cascader);
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
export default Cascader;
|