2019-01-12 11:33:27 +08:00
|
|
|
import VcTreeSelect, { TreeNode, SHOW_ALL, SHOW_PARENT, SHOW_CHILD } from '../vc-tree-select';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { TreeSelectProps } from './interface';
|
|
|
|
import warning from '../_util/warning';
|
|
|
|
import {
|
|
|
|
initDefaultProps,
|
|
|
|
getOptionProps,
|
|
|
|
getComponentFromProp,
|
|
|
|
filterEmpty,
|
2020-01-19 16:58:38 +08:00
|
|
|
getListeners,
|
2019-01-12 11:33:27 +08:00
|
|
|
} from '../_util/props-util';
|
2019-04-10 11:54:42 +08:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2019-08-28 10:50:19 +08:00
|
|
|
import Base from '../base';
|
2018-07-11 17:51:20 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
export { TreeData, TreeSelectProps } from './interface';
|
2020-03-28 15:47:48 +08:00
|
|
|
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
|
|
|
|
import CaretDownOutlined from '@ant-design/icons-vue/CaretDownOutlined';
|
|
|
|
import DownOutlined from '@ant-design/icons-vue/DownOutlined';
|
|
|
|
import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
|
|
|
|
import CloseCircleOutlined from '@ant-design/icons-vue/CloseCircleOutlined';
|
2019-01-12 11:33:27 +08:00
|
|
|
import omit from 'omit.js';
|
2018-12-12 21:24:12 +08:00
|
|
|
|
2018-09-19 13:21:57 +08:00
|
|
|
const TreeSelect = {
|
2018-07-11 17:51:20 +08:00
|
|
|
TreeNode: { ...TreeNode, name: 'ATreeSelectNode' },
|
|
|
|
SHOW_ALL,
|
|
|
|
SHOW_PARENT,
|
|
|
|
SHOW_CHILD,
|
|
|
|
name: 'ATreeSelect',
|
|
|
|
props: initDefaultProps(TreeSelectProps(), {
|
|
|
|
transitionName: 'slide-up',
|
|
|
|
choiceTransitionName: 'zoom',
|
|
|
|
showSearch: false,
|
|
|
|
}),
|
|
|
|
model: {
|
|
|
|
prop: 'value',
|
|
|
|
event: 'change',
|
|
|
|
},
|
2019-01-07 20:59:02 +08:00
|
|
|
inject: {
|
2019-09-11 22:33:36 +08:00
|
|
|
configProvider: { default: () => ConfigConsumerProps },
|
2019-01-07 20:59:02 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
created() {
|
2018-07-11 17:51:20 +08:00
|
|
|
warning(
|
|
|
|
this.multiple !== false || !this.treeCheckable,
|
2020-03-07 19:45:13 +08:00
|
|
|
'TreeSelect',
|
2018-07-11 17:51:20 +08:00
|
|
|
'`multiple` will alway be `true` when `treeCheckable` is true',
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-07-11 17:51:20 +08:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 11:33:27 +08:00
|
|
|
focus() {
|
|
|
|
this.$refs.vcTreeSelect.focus();
|
2018-07-11 17:51:20 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
blur() {
|
|
|
|
this.$refs.vcTreeSelect.blur();
|
2018-07-11 17:51:20 +08:00
|
|
|
},
|
2019-04-10 11:54:42 +08:00
|
|
|
renderSwitcherIcon(prefixCls, { isLeaf, loading }) {
|
2018-12-12 21:24:12 +08:00
|
|
|
if (loading) {
|
2020-03-28 15:47:48 +08:00
|
|
|
return <LoadingOutlined class={`${prefixCls}-switcher-loading-icon`} />;
|
2018-12-12 21:24:12 +08:00
|
|
|
}
|
|
|
|
if (isLeaf) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return null;
|
2018-12-12 21:24:12 +08:00
|
|
|
}
|
2020-03-28 15:47:48 +08:00
|
|
|
return <CaretDownOutlined class={`${prefixCls}-switcher-icon`} />;
|
2018-12-12 21:24:12 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
onChange() {
|
|
|
|
this.$emit('change', ...arguments);
|
2018-07-11 17:51:20 +08:00
|
|
|
},
|
2019-04-19 21:30:58 +08:00
|
|
|
updateTreeData(treeData) {
|
|
|
|
const { $scopedSlots } = this;
|
2020-05-19 14:40:41 +08:00
|
|
|
const defaultFields = {
|
|
|
|
children: 'children',
|
|
|
|
title: 'title',
|
|
|
|
key: 'key',
|
|
|
|
label: 'label',
|
|
|
|
value: 'value',
|
|
|
|
};
|
|
|
|
const replaceFields = { ...defaultFields, ...this.$props.replaceFields };
|
2019-04-19 21:30:58 +08:00
|
|
|
return treeData.map(item => {
|
2020-05-19 14:40:41 +08:00
|
|
|
const { scopedSlots = {} } = item;
|
|
|
|
const label = item[replaceFields.label];
|
|
|
|
const title = item[replaceFields.title];
|
|
|
|
const value = item[replaceFields.value];
|
|
|
|
const key = item[replaceFields.key];
|
|
|
|
const children = item[replaceFields.children];
|
2019-01-12 11:33:27 +08:00
|
|
|
let newLabel = typeof label === 'function' ? label(this.$createElement) : label;
|
|
|
|
let newTitle = typeof title === 'function' ? title(this.$createElement) : title;
|
2018-07-11 17:51:20 +08:00
|
|
|
if (!newLabel && scopedSlots.label && $scopedSlots[scopedSlots.label]) {
|
2019-04-19 21:30:58 +08:00
|
|
|
newLabel = $scopedSlots.label(item);
|
2018-07-11 17:51:20 +08:00
|
|
|
}
|
|
|
|
if (!newTitle && scopedSlots.title && $scopedSlots[scopedSlots.title]) {
|
2019-04-19 21:30:58 +08:00
|
|
|
newTitle = $scopedSlots.title(item);
|
2018-07-11 17:51:20 +08:00
|
|
|
}
|
2019-04-19 21:30:58 +08:00
|
|
|
const treeNodeProps = {
|
|
|
|
...item,
|
2018-07-11 17:51:20 +08:00
|
|
|
title: newTitle || newLabel,
|
2020-05-19 14:40:41 +08:00
|
|
|
value,
|
2019-04-19 21:30:58 +08:00
|
|
|
dataRef: item,
|
2020-05-19 14:40:41 +08:00
|
|
|
key,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2019-04-19 21:30:58 +08:00
|
|
|
if (children) {
|
|
|
|
return { ...treeNodeProps, children: this.updateTreeData(children) };
|
|
|
|
}
|
|
|
|
return treeNodeProps;
|
|
|
|
});
|
2018-07-11 17:51:20 +08:00
|
|
|
},
|
2019-04-10 11:54:42 +08:00
|
|
|
},
|
2018-12-12 21:24:12 +08:00
|
|
|
|
2019-04-10 11:54:42 +08:00
|
|
|
render(h) {
|
|
|
|
const props = getOptionProps(this);
|
2019-05-28 11:37:38 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
size,
|
|
|
|
dropdownStyle,
|
|
|
|
dropdownClassName,
|
|
|
|
getPopupContainer,
|
|
|
|
...restProps
|
|
|
|
} = props;
|
2019-09-11 22:35:25 +08:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-05-28 11:37:38 +08:00
|
|
|
const prefixCls = getPrefixCls('select', customizePrefixCls);
|
2019-04-10 11:54:42 +08:00
|
|
|
|
2019-09-11 22:33:36 +08:00
|
|
|
const renderEmpty = this.configProvider.renderEmpty;
|
2019-05-28 11:37:38 +08:00
|
|
|
const notFoundContent = getComponentFromProp(this, 'notFoundContent');
|
2020-03-07 19:45:13 +08:00
|
|
|
const removeIcon = getComponentFromProp(this, 'removeIcon');
|
|
|
|
const clearIcon = getComponentFromProp(this, 'clearIcon');
|
2019-05-28 11:37:38 +08:00
|
|
|
const { getPopupContainer: getContextPopupContainer } = this.configProvider;
|
|
|
|
const rest = omit(restProps, [
|
|
|
|
'inputIcon',
|
|
|
|
'removeIcon',
|
|
|
|
'clearIcon',
|
|
|
|
'switcherIcon',
|
|
|
|
'suffixIcon',
|
|
|
|
]);
|
|
|
|
let suffixIcon = getComponentFromProp(this, 'suffixIcon');
|
|
|
|
suffixIcon = Array.isArray(suffixIcon) ? suffixIcon[0] : suffixIcon;
|
|
|
|
let treeData = props.treeData;
|
|
|
|
if (treeData) {
|
|
|
|
treeData = this.updateTreeData(treeData);
|
|
|
|
}
|
|
|
|
const cls = {
|
|
|
|
[`${prefixCls}-lg`]: size === 'large',
|
|
|
|
[`${prefixCls}-sm`]: size === 'small',
|
|
|
|
};
|
2018-07-11 17:51:20 +08:00
|
|
|
|
2020-03-07 19:45:13 +08:00
|
|
|
// showSearch: single - false, multiple - true
|
|
|
|
let { showSearch } = restProps;
|
|
|
|
if (!('showSearch' in restProps)) {
|
|
|
|
showSearch = !!(restProps.multiple || restProps.treeCheckable);
|
|
|
|
}
|
|
|
|
|
2019-05-28 11:37:38 +08:00
|
|
|
let checkable = getComponentFromProp(this, 'treeCheckable');
|
|
|
|
if (checkable) {
|
|
|
|
checkable = <span class={`${prefixCls}-tree-checkbox-inner`} />;
|
|
|
|
}
|
2018-12-12 21:24:12 +08:00
|
|
|
|
2020-03-28 15:47:48 +08:00
|
|
|
const inputIcon = suffixIcon || <DownOutlined class={`${prefixCls}-arrow-icon`} />;
|
2018-12-12 21:24:12 +08:00
|
|
|
|
2020-03-28 15:47:48 +08:00
|
|
|
const finalRemoveIcon = removeIcon || <CloseOutlined class={`${prefixCls}-remove-icon`} />;
|
2018-12-12 21:24:12 +08:00
|
|
|
|
2020-03-07 19:45:13 +08:00
|
|
|
const finalClearIcon = clearIcon || (
|
2020-03-28 15:47:48 +08:00
|
|
|
<CloseCircleOutlined class={`${prefixCls}-clear-icon`} theme="filled" />
|
2020-03-07 19:45:13 +08:00
|
|
|
);
|
2019-05-28 11:37:38 +08:00
|
|
|
const VcTreeSelectProps = {
|
2019-08-21 15:17:21 +08:00
|
|
|
props: Object.assign(
|
|
|
|
{
|
|
|
|
switcherIcon: nodeProps => this.renderSwitcherIcon(prefixCls, nodeProps),
|
|
|
|
inputIcon,
|
2020-03-07 19:45:13 +08:00
|
|
|
removeIcon: finalRemoveIcon,
|
|
|
|
clearIcon: finalClearIcon,
|
2019-08-21 15:17:21 +08:00
|
|
|
...rest,
|
2020-03-07 19:45:13 +08:00
|
|
|
showSearch,
|
2019-08-21 15:17:21 +08:00
|
|
|
getPopupContainer: getPopupContainer || getContextPopupContainer,
|
|
|
|
dropdownClassName: classNames(dropdownClassName, `${prefixCls}-tree-dropdown`),
|
|
|
|
prefixCls,
|
|
|
|
dropdownStyle: { maxHeight: '100vh', overflow: 'auto', ...dropdownStyle },
|
|
|
|
treeCheckable: checkable,
|
|
|
|
notFoundContent: notFoundContent || renderEmpty(h, 'Select'),
|
|
|
|
__propsSymbol__: Symbol(),
|
|
|
|
},
|
|
|
|
treeData ? { treeData } : {},
|
|
|
|
),
|
2019-05-28 11:37:38 +08:00
|
|
|
class: cls,
|
2020-01-19 16:58:38 +08:00
|
|
|
on: { ...getListeners(this), change: this.onChange },
|
2019-05-28 11:37:38 +08:00
|
|
|
ref: 'vcTreeSelect',
|
|
|
|
scopedSlots: this.$scopedSlots,
|
|
|
|
};
|
|
|
|
return <VcTreeSelect {...VcTreeSelectProps}>{filterEmpty(this.$slots.default)}</VcTreeSelect>;
|
2018-07-11 17:51:20 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-09-19 13:21:57 +08:00
|
|
|
|
|
|
|
/* istanbul ignore next */
|
2019-01-12 11:33:27 +08:00
|
|
|
TreeSelect.install = function(Vue) {
|
2019-08-28 10:50:19 +08:00
|
|
|
Vue.use(Base);
|
2019-01-12 11:33:27 +08:00
|
|
|
Vue.component(TreeSelect.name, TreeSelect);
|
|
|
|
Vue.component(TreeSelect.TreeNode.name, TreeSelect.TreeNode);
|
|
|
|
};
|
2018-09-19 13:21:57 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
export default TreeSelect;
|