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

315 lines
9.7 KiB
Vue
Raw Normal View History

2021-08-22 02:47:41 +00:00
import type { App, ExtractPropTypes, Plugin, PropType } from 'vue';
import { computed, ref, watchEffect } from 'vue';
import { defineComponent } from 'vue';
import VcTreeSelect, {
TreeNode,
SHOW_ALL,
SHOW_PARENT,
SHOW_CHILD,
treeSelectProps as vcTreeSelectProps,
} from '../vc-tree-select';
2020-08-31 08:53:19 +00:00
import classNames from '../_util/classNames';
2020-10-22 02:06:57 +00:00
import initDefaultProps from '../_util/props-util/initDefaultProps';
2021-08-22 02:47:41 +00:00
import type { SizeType } from '../config-provider';
2018-07-11 09:51:20 +00:00
2019-01-12 03:33:27 +00:00
export { TreeData, TreeSelectProps } from './interface';
2020-03-28 07:47:48 +00:00
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
import CaretDownOutlined from '@ant-design/icons-vue/CaretDownOutlined';
2021-08-22 02:47:41 +00:00
import type { DefaultValueType, FieldNames } from '../vc-tree-select/interface';
import omit from '../_util/omit';
import PropTypes from '../_util/vue-types';
import useConfigInject from '../_util/hooks/useConfigInject';
import devWarning from '../vc-util/devWarning';
import getIcons from '../select/utils/iconUtil';
import renderSwitcherIcon from '../tree/utils/iconUtil';
import type { AntTreeNodeProps } from '../tree/Tree';
const getTransitionName = (rootPrefixCls: string, motion: string, transitionName?: string) => {
if (transitionName !== undefined) {
return transitionName;
}
return `${rootPrefixCls}-${motion}`;
};
2020-09-02 04:54:24 +00:00
2021-08-22 02:47:41 +00:00
type RawValue = string | number;
export interface LabeledValue {
key?: string;
value: RawValue;
label: any;
}
export type SelectValue = RawValue | RawValue[] | LabeledValue | LabeledValue[];
export interface RefTreeSelectProps {
focus: () => void;
blur: () => void;
}
export const treeSelectProps = {
...omit(vcTreeSelectProps<DefaultValueType>(), ['showTreeIcon', 'treeMotion', 'inputIcon']),
suffixIcon: PropTypes.any,
size: { type: String as PropType<SizeType> },
bordered: { type: Boolean, default: undefined },
replaceFields: { type: Object as PropType<FieldNames> },
};
export type TreeSelectProps = Partial<ExtractPropTypes<typeof treeSelectProps>>;
2020-10-22 02:06:57 +00:00
const TreeSelect = defineComponent({
2020-09-02 04:54:24 +00:00
TreeNode,
2018-07-11 09:51:20 +00:00
SHOW_ALL,
SHOW_PARENT,
SHOW_CHILD,
name: 'ATreeSelect',
2020-11-07 07:37:17 +00:00
inheritAttrs: false,
2021-08-22 02:47:41 +00:00
props: initDefaultProps(treeSelectProps, {
2018-07-11 09:51:20 +00:00
transitionName: 'slide-up',
choiceTransitionName: '',
2021-08-22 02:47:41 +00:00
listHeight: 256,
treeIcon: false,
listItemHeight: 26,
bordered: true,
2018-07-11 09:51:20 +00:00
}),
2021-08-22 02:47:41 +00:00
slots: ['placeholder', 'maxTagPlaceholder', 'treeIcon', 'switcherIcon', 'notFoundContent'],
setup(props, { attrs, slots, expose, emit }) {
watchEffect(() => {
devWarning(
props.multiple !== false || !props.treeCheckable,
'TreeSelect',
'`multiple` will alway be `true` when `treeCheckable` is true',
);
devWarning(
props.replaceFields === undefined,
'TreeSelect',
'`replaceFields` is deprecated, please use fieldNames instead',
);
});
const {
configProvider,
prefixCls,
renderEmpty,
direction,
virtual,
dropdownMatchSelectWidth,
size,
getPopupContainer,
} = useConfigInject('select', props);
const treePrefixCls = computed(() =>
configProvider.getPrefixCls('select-tree', props.prefixCls),
2019-01-12 03:33:27 +00:00
);
2021-08-22 02:47:41 +00:00
const treeSelectPrefixCls = computed(() =>
configProvider.getPrefixCls('tree-select', props.prefixCls),
);
const mergedDropdownClassName = computed(() =>
classNames(props.dropdownClassName, `${treeSelectPrefixCls.value}-dropdown`, {
[`${treeSelectPrefixCls.value}-dropdown-rtl`]: direction.value === 'rtl',
}),
);
const isMultiple = computed(() => !!(props.treeCheckable || props.multiple));
const treeSelectRef = ref();
expose({
focus() {
treeSelectRef.value.focus?.();
},
blur() {
treeSelectRef.value.blur?.();
},
});
const handleChange = (...args: any[]) => {
emit('update:value', args[0]);
emit('change', ...args);
};
const handleTreeExpand = (...args: any[]) => {
emit('update:treeExpandedKeys', args[0]);
emit('treeExpand', ...args);
};
const handleSearch = (...args: any[]) => {
emit('update:searchValue', args[0]);
emit('search', ...args);
};
return () => {
const {
notFoundContent = slots.notFoundContent?.(),
prefixCls: customizePrefixCls,
bordered,
listHeight,
listItemHeight,
multiple,
treeIcon,
transitionName,
choiceTransitionName,
treeLine,
switcherIcon = slots.switcherIcon?.(),
fieldNames = props.replaceFields,
} = props;
// ===================== Icons =====================
const { suffixIcon, removeIcon, clearIcon } = getIcons(
{
...props,
multiple: isMultiple.value,
prefixCls: prefixCls.value,
},
slots,
);
// ===================== Empty =====================
let mergedNotFound;
if (notFoundContent !== undefined) {
mergedNotFound = notFoundContent;
} else {
mergedNotFound = renderEmpty.value('Select');
}
// ==================== Render =====================
const selectProps = omit(props as typeof props & { itemIcon: any; switcherIcon: any }, [
'suffixIcon',
'itemIcon',
'removeIcon',
'clearIcon',
'switcherIcon',
]);
const mergedClassName = classNames(
!customizePrefixCls && treeSelectPrefixCls.value,
{
[`${prefixCls.value}-lg`]: size.value === 'large',
[`${prefixCls.value}-sm`]: size.value === 'small',
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
[`${prefixCls.value}-borderless`]: !bordered,
},
attrs.class,
);
const rootPrefixCls = configProvider.getPrefixCls();
return (
<VcTreeSelect
{...attrs}
virtual={virtual.value}
dropdownMatchSelectWidth={dropdownMatchSelectWidth.value}
{...selectProps}
fieldNames={fieldNames}
ref={treeSelectRef}
prefixCls={prefixCls.value}
class={mergedClassName}
listHeight={listHeight}
listItemHeight={listItemHeight}
inputIcon={suffixIcon}
multiple={multiple}
removeIcon={removeIcon}
clearIcon={clearIcon}
switcherIcon={(nodeProps: AntTreeNodeProps) =>
renderSwitcherIcon(treePrefixCls.value, switcherIcon, treeLine, nodeProps)
}
showTreeIcon={treeIcon as any}
notFoundContent={mergedNotFound}
getPopupContainer={getPopupContainer.value}
treeMotion={null}
dropdownClassName={mergedDropdownClassName.value}
choiceTransitionName={getTransitionName(rootPrefixCls, '', choiceTransitionName)}
transitionName={getTransitionName(rootPrefixCls, 'slide-up', transitionName)}
onChange={handleChange}
onSearch={handleSearch}
onTreeExpand={handleTreeExpand}
v-slots={{
treeCheckable: () => <span class={`${prefixCls.value}-tree-checkbox-inner`} />,
}}
children={slots.default?.()}
/>
);
};
2018-07-11 09:51:20 +00:00
},
2021-08-22 02:47:41 +00:00
2018-07-11 09:51:20 +00:00
methods: {
2020-10-22 03:35:05 +00:00
saveTreeSelect(node: any) {
2020-07-18 13:40:26 +00:00
this.vcTreeSelect = node;
},
2019-01-12 03:33:27 +00:00
focus() {
2020-07-18 13:40:26 +00:00
this.vcTreeSelect.focus();
2018-07-11 09:51:20 +00:00
},
2019-01-12 03:33:27 +00:00
blur() {
2020-07-18 13:40:26 +00:00
this.vcTreeSelect.blur();
2018-07-11 09:51:20 +00:00
},
2020-10-22 03:35:05 +00:00
renderSwitcherIcon(prefixCls: string, { isLeaf, loading }) {
2018-12-12 13:24:12 +00:00
if (loading) {
2020-03-28 07:47:48 +00:00
return <LoadingOutlined class={`${prefixCls}-switcher-loading-icon`} />;
2018-12-12 13:24:12 +00:00
}
if (isLeaf) {
2019-01-12 03:33:27 +00:00
return null;
2018-12-12 13:24:12 +00:00
}
2020-03-28 07:47:48 +00:00
return <CaretDownOutlined class={`${prefixCls}-switcher-icon`} />;
2018-12-12 13:24:12 +00:00
},
2020-10-22 03:35:05 +00:00
handleChange(...args: any[]) {
2020-07-18 13:40:26 +00:00
this.$emit('update:value', args[0]);
this.$emit('change', ...args);
2018-07-11 09:51:20 +00:00
},
2020-10-22 03:35:05 +00:00
handleTreeExpand(...args: any[]) {
2020-08-12 13:35:45 +00:00
this.$emit('update:treeExpandedKeys', args[0]);
this.$emit('treeExpand', ...args);
},
2020-10-22 03:35:05 +00:00
handleSearch(...args: any[]) {
2020-08-12 13:35:45 +00:00
this.$emit('update:searchValue', args[0]);
this.$emit('search', ...args);
},
2020-10-22 03:35:05 +00:00
updateTreeData(treeData: any[]) {
2020-07-18 13:40:26 +00:00
const { $slots } = this;
const defaultFields = {
children: 'children',
title: 'title',
key: 'key',
label: 'label',
value: 'value',
};
const replaceFields = { ...defaultFields, ...this.$props.replaceFields };
return treeData.map(item => {
2020-07-18 13:40:26 +00:00
const { slots = {} } = 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];
2020-07-18 13:40:26 +00:00
let newLabel = typeof label === 'function' ? label() : label;
let newTitle = typeof title === 'function' ? title() : title;
if (!newLabel && slots.label && $slots[slots.label]) {
newLabel = <>{$slots[slots.label](item)}</>;
2018-07-11 09:51:20 +00:00
}
2020-07-18 13:40:26 +00:00
if (!newTitle && slots.title && $slots[slots.title]) {
newTitle = <>{$slots[slots.title](item)}</>;
2018-07-11 09:51:20 +00:00
}
const treeNodeProps = {
...item,
2018-07-11 09:51:20 +00:00
title: newTitle || newLabel,
value,
dataRef: item,
key,
2019-01-12 03:33:27 +00:00
};
if (children) {
return { ...treeNodeProps, children: this.updateTreeData(children) };
}
return treeNodeProps;
});
2018-07-11 09:51:20 +00:00
},
2019-04-10 03:54:42 +00:00
},
2020-10-22 02:06:57 +00:00
});
/* istanbul ignore next */
2021-06-23 15:08:16 +00:00
TreeSelect.install = function (app: App) {
2020-07-18 13:40:26 +00:00
app.component(TreeSelect.name, TreeSelect);
app.component(TreeSelect.TreeNode.displayName, TreeSelect.TreeNode);
2020-10-13 11:14:56 +00:00
return app;
2019-01-12 03:33:27 +00:00
};
2021-06-23 13:47:53 +00:00
export const TreeSelectNode = TreeSelect.TreeNode;
2020-11-01 07:03:33 +00:00
export default TreeSelect as typeof TreeSelect &
Plugin & {
readonly TreeNode: typeof TreeNode;
2020-10-22 03:35:05 +00:00
2020-11-01 07:03:33 +00:00
readonly SHOW_ALL: typeof SHOW_ALL;
2020-10-22 03:35:05 +00:00
2020-11-01 07:03:33 +00:00
readonly SHOW_PARENT: typeof SHOW_PARENT;
2020-10-22 03:35:05 +00:00
2020-11-01 07:03:33 +00:00
readonly SHOW_CHILD: typeof SHOW_CHILD;
};