ant-design-vue/components/tree-select/index.jsx

164 lines
5.3 KiB
Vue
Raw Normal View History

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,
isValidElement,
} from '../_util/props-util';
2019-04-10 11:54:42 +08:00
import { ConfigConsumerProps } from '../config-provider';
2019-01-12 11:33:27 +08:00
export { TreeData, TreeSelectProps } from './interface';
import Icon from '../icon';
import omit from 'omit.js';
import { cloneElement } from '../_util/vnode';
2018-12-12 21:24:12 +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-01-28 21:09:13 +08:00
configProvider: { default: () => ({}) },
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,
'`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) {
2019-01-12 11:33:27 +08:00
return <Icon type="loading" 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
}
2019-01-12 11:33:27 +08:00
return <Icon type="caret-down" 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-01-12 11:33:27 +08:00
updateTreeData(list = []) {
2018-07-11 17:51:20 +08:00
for (let i = 0, len = list.length; i < len; i++) {
2019-01-12 11:33:27 +08:00
const { label, title, scopedSlots = {}, children } = list[i];
const { $scopedSlots } = this;
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-01-12 11:33:27 +08:00
newLabel = $scopedSlots.label(list[i]);
2018-07-11 17:51:20 +08:00
}
if (!newTitle && scopedSlots.title && $scopedSlots[scopedSlots.title]) {
2019-01-12 11:33:27 +08:00
newTitle = $scopedSlots.title(list[i]);
2018-07-11 17:51:20 +08:00
}
const item = {
// label: newLabel,
title: newTitle || newLabel,
2019-01-12 11:33:27 +08:00
};
this.updateTreeData(children || []);
Object.assign(list[i], item);
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);
2018-07-11 17:51:20 +08:00
const {
2019-04-10 11:54:42 +08:00
prefixCls: customizePrefixCls,
2018-07-11 17:51:20 +08:00
size,
dropdownStyle,
dropdownClassName,
2019-01-07 20:59:02 +08:00
getPopupContainer,
2018-07-11 17:51:20 +08:00
...restProps
2019-01-12 11:33:27 +08:00
} = props;
2019-04-10 11:54:42 +08:00
const getPrefixCls = this.configProvider.getPrefixCls || ConfigConsumerProps.getPrefixCls;
const prefixCls = getPrefixCls('select', customizePrefixCls);
const renderEmpty = (
this.configProvider.renderEmpty &&
this.configProvider.renderEmpty()
) || ConfigConsumerProps.renderEmpty;
const notFoundContent = getComponentFromProp(this, 'notFoundContent');
2019-01-12 11:33:27 +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;
this.updateTreeData(props.treeData || []);
2018-07-11 17:51:20 +08:00
const cls = {
[`${prefixCls}-lg`]: size === 'large',
[`${prefixCls}-sm`]: size === 'small',
2019-01-12 11:33:27 +08:00
};
2018-07-11 17:51:20 +08:00
2019-01-12 11:33:27 +08:00
let checkable = getComponentFromProp(this, 'treeCheckable');
2018-07-11 17:51:20 +08:00
if (checkable) {
2019-01-12 11:33:27 +08:00
checkable = <span class={`${prefixCls}-tree-checkbox-inner`} />;
2018-07-11 17:51:20 +08:00
}
2018-12-12 21:24:12 +08:00
2019-01-12 11:33:27 +08:00
const inputIcon = (suffixIcon &&
(isValidElement(suffixIcon) ? cloneElement(suffixIcon) : suffixIcon)) || (
<Icon type="down" class={`${prefixCls}-arrow-icon`} />
);
2018-12-12 21:24:12 +08:00
2019-01-12 11:33:27 +08:00
const removeIcon = <Icon type="close" class={`${prefixCls}-remove-icon`} />;
2018-12-12 21:24:12 +08:00
const clearIcon = (
2019-01-12 11:33:27 +08:00
<Icon type="close-circle" class={`${prefixCls}-clear-icon`} theme="filled" />
);
2018-07-11 17:51:20 +08:00
const VcTreeSelectProps = {
props: {
2019-04-10 11:54:42 +08:00
switcherIcon: (nodeProps) => this.renderSwitcherIcon(prefixCls, nodeProps),
2018-12-12 21:24:12 +08:00
inputIcon,
removeIcon,
clearIcon,
...rest,
2019-01-07 20:59:02 +08:00
getPopupContainer: getPopupContainer || getContextPopupContainer,
2018-07-11 17:51:20 +08:00
dropdownClassName: classNames(dropdownClassName, `${prefixCls}-tree-dropdown`),
prefixCls,
dropdownStyle: { maxHeight: '100vh', overflow: 'auto', ...dropdownStyle },
treeCheckable: checkable,
2019-04-10 11:54:42 +08:00
notFoundContent: notFoundContent || renderEmpty(h, 'Select'),
__propsSymbol__: Symbol(),
2018-07-11 17:51:20 +08:00
},
class: cls,
on: { ...this.$listeners, change: this.onChange },
ref: 'vcTreeSelect',
scopedSlots: this.$scopedSlots,
2019-01-12 11:33:27 +08:00
};
return <VcTreeSelect {...VcTreeSelectProps}>{filterEmpty(this.$slots.default)}</VcTreeSelect>;
2018-07-11 17:51:20 +08:00
},
2019-01-12 11:33:27 +08:00
};
/* istanbul ignore next */
2019-01-12 11:33:27 +08:00
TreeSelect.install = function(Vue) {
Vue.component(TreeSelect.name, TreeSelect);
Vue.component(TreeSelect.TreeNode.name, TreeSelect.TreeNode);
};
2019-01-12 11:33:27 +08:00
export default TreeSelect;