2021-11-10 02:06:59 +00:00
|
|
|
import type { App, ExtractPropTypes, PropType } from 'vue';
|
2021-09-25 08:51:32 +00:00
|
|
|
import { computed, ref, watchEffect, 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-09-25 08:51:32 +00:00
|
|
|
import type { SizeType } from '../config-provider';
|
2022-01-21 13:58:10 +00:00
|
|
|
import type { FieldNames, Key } from '../vc-tree-select/interface';
|
2021-09-25 08:51:32 +00:00
|
|
|
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';
|
|
|
|
import { warning } from '../vc-util/warning';
|
|
|
|
import { flattenChildren } from '../_util/props-util';
|
|
|
|
import { useInjectFormItemContext } from '../form/FormItemContext';
|
2022-01-21 13:58:10 +00:00
|
|
|
import type { BaseSelectRef } from '../vc-select';
|
|
|
|
import type { BaseOptionType, DefaultOptionType } from '../vc-tree-select/TreeSelect';
|
|
|
|
import type { TreeProps } from '../tree';
|
2018-07-11 09:51:20 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
const getTransitionName = (rootPrefixCls: string, motion: string, transitionName?: string) => {
|
|
|
|
if (transitionName !== undefined) {
|
|
|
|
return transitionName;
|
|
|
|
}
|
|
|
|
return `${rootPrefixCls}-${motion}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
type RawValue = string | number;
|
|
|
|
|
|
|
|
export interface LabeledValue {
|
|
|
|
key?: string;
|
|
|
|
value: RawValue;
|
2022-01-21 13:58:10 +00:00
|
|
|
label?: any;
|
2021-09-25 08:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export type SelectValue = RawValue | RawValue[] | LabeledValue | LabeledValue[];
|
|
|
|
|
2022-01-21 13:58:10 +00:00
|
|
|
export type RefTreeSelectProps = BaseSelectRef;
|
|
|
|
|
|
|
|
export function treeSelectProps<
|
|
|
|
ValueType = any,
|
|
|
|
OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType,
|
|
|
|
>() {
|
|
|
|
return {
|
|
|
|
...omit(vcTreeSelectProps<ValueType, OptionType>(), [
|
|
|
|
'showTreeIcon',
|
|
|
|
'treeMotion',
|
|
|
|
'inputIcon',
|
|
|
|
'getInputElement',
|
|
|
|
'treeLine',
|
|
|
|
'customSlots',
|
|
|
|
]),
|
|
|
|
suffixIcon: PropTypes.any,
|
|
|
|
size: { type: String as PropType<SizeType> },
|
|
|
|
bordered: { type: Boolean, default: undefined },
|
|
|
|
treeLine: { type: [Boolean, Object] as PropType<TreeProps['showLine']>, default: undefined },
|
|
|
|
replaceFields: { type: Object as PropType<FieldNames> },
|
|
|
|
'onUpdate:value': { type: Function as PropType<(value: any) => void> },
|
|
|
|
'onUpdate:treeExpandedKeys': { type: Function as PropType<(keys: Key[]) => void> },
|
|
|
|
'onUpdate:searchValue': { type: Function as PropType<(value: string) => void> },
|
|
|
|
};
|
2021-09-25 08:51:32 +00:00
|
|
|
}
|
2022-01-21 13:58:10 +00:00
|
|
|
export type TreeSelectProps = Partial<ExtractPropTypes<ReturnType<typeof treeSelectProps>>>;
|
2020-09-02 04:54:24 +00:00
|
|
|
|
2020-10-22 02:06:57 +00:00
|
|
|
const TreeSelect = defineComponent({
|
2018-07-11 09:51:20 +00:00
|
|
|
name: 'ATreeSelect',
|
2020-11-07 07:37:17 +00:00
|
|
|
inheritAttrs: false,
|
2022-01-21 13:58:10 +00:00
|
|
|
props: initDefaultProps(treeSelectProps(), {
|
2020-10-17 14:19:52 +00:00
|
|
|
choiceTransitionName: '',
|
2021-09-25 08:51:32 +00:00
|
|
|
listHeight: 256,
|
|
|
|
treeIcon: false,
|
|
|
|
listItemHeight: 26,
|
|
|
|
bordered: true,
|
2018-07-11 09:51:20 +00:00
|
|
|
}),
|
2021-09-25 08:51:32 +00:00
|
|
|
slots: [
|
|
|
|
'title',
|
|
|
|
'titleRender',
|
|
|
|
'placeholder',
|
|
|
|
'maxTagPlaceholder',
|
|
|
|
'treeIcon',
|
|
|
|
'switcherIcon',
|
|
|
|
'notFoundContent',
|
|
|
|
],
|
|
|
|
setup(props, { attrs, slots, expose, emit }) {
|
2018-07-11 09:51:20 +00:00
|
|
|
warning(
|
2021-09-25 08:51:32 +00:00
|
|
|
!(props.treeData === undefined && slots.default),
|
|
|
|
'`children` of TreeSelect is deprecated. Please use `treeData` instead.',
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2021-09-25 08:51:32 +00:00
|
|
|
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',
|
|
|
|
);
|
|
|
|
});
|
2018-12-12 13:24:12 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
const formItemContext = useInjectFormItemContext();
|
2019-05-28 03:37:38 +00:00
|
|
|
const {
|
2021-09-25 08:51:32 +00:00
|
|
|
configProvider,
|
|
|
|
prefixCls,
|
|
|
|
renderEmpty,
|
|
|
|
direction,
|
|
|
|
virtual,
|
|
|
|
dropdownMatchSelectWidth,
|
2019-05-28 03:37:38 +00:00
|
|
|
size,
|
|
|
|
getPopupContainer,
|
2021-12-17 08:59:50 +00:00
|
|
|
getPrefixCls,
|
2021-09-25 08:51:32 +00:00
|
|
|
} = useConfigInject('select', props);
|
2021-12-17 08:59:50 +00:00
|
|
|
const rootPrefixCls = computed(() => getPrefixCls());
|
|
|
|
const transitionName = computed(() =>
|
|
|
|
getTransitionName(rootPrefixCls.value, 'slide-up', props.transitionName),
|
|
|
|
);
|
|
|
|
const choiceTransitionName = computed(() =>
|
|
|
|
getTransitionName(rootPrefixCls.value, '', props.choiceTransitionName),
|
|
|
|
);
|
2021-09-25 08:51:32 +00:00
|
|
|
const treePrefixCls = computed(() =>
|
|
|
|
configProvider.getPrefixCls('select-tree', props.prefixCls),
|
|
|
|
);
|
|
|
|
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?.();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-01-21 13:58:10 +00:00
|
|
|
const handleChange: TreeSelectProps['onChange'] = (...args: any[]) => {
|
2021-09-25 08:51:32 +00:00
|
|
|
emit('update:value', args[0]);
|
|
|
|
emit('change', ...args);
|
|
|
|
formItemContext.onFieldChange();
|
2019-05-28 03:37:38 +00:00
|
|
|
};
|
2022-01-21 13:58:10 +00:00
|
|
|
const handleTreeExpand: TreeSelectProps['onTreeExpand'] = (keys: Key[]) => {
|
|
|
|
emit('update:treeExpandedKeys', keys);
|
|
|
|
emit('treeExpand', keys);
|
2021-09-25 08:51:32 +00:00
|
|
|
};
|
2022-01-21 13:58:10 +00:00
|
|
|
const handleSearch: TreeSelectProps['onSearch'] = (value: string) => {
|
|
|
|
emit('update:searchValue', value);
|
|
|
|
emit('search', value);
|
2021-09-25 08:51:32 +00:00
|
|
|
};
|
|
|
|
const handleBlur = () => {
|
|
|
|
emit('blur');
|
|
|
|
formItemContext.onFieldBlur();
|
|
|
|
};
|
|
|
|
return () => {
|
|
|
|
const {
|
|
|
|
notFoundContent = slots.notFoundContent?.(),
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
bordered,
|
|
|
|
listHeight,
|
|
|
|
listItemHeight,
|
|
|
|
multiple,
|
|
|
|
treeIcon,
|
|
|
|
treeLine,
|
|
|
|
switcherIcon = slots.switcherIcon?.(),
|
|
|
|
fieldNames = props.replaceFields,
|
|
|
|
id = formItemContext.id.value,
|
|
|
|
} = props;
|
|
|
|
// ===================== Icons =====================
|
|
|
|
const { suffixIcon, removeIcon, clearIcon } = getIcons(
|
|
|
|
{
|
|
|
|
...props,
|
|
|
|
multiple: isMultiple.value,
|
|
|
|
prefixCls: prefixCls.value,
|
|
|
|
},
|
|
|
|
slots,
|
|
|
|
);
|
2018-07-11 09:51:20 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
// ===================== 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',
|
2022-01-21 13:58:10 +00:00
|
|
|
'bordered',
|
|
|
|
'onUpdate:value',
|
|
|
|
'onUpdate:treeExpandedKeys',
|
|
|
|
'onUpdate:searchValue',
|
2021-09-25 08:51:32 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
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 otherProps: any = {};
|
|
|
|
if (props.treeData === undefined && slots.default) {
|
|
|
|
otherProps.children = flattenChildren(slots.default());
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<VcTreeSelect
|
|
|
|
{...attrs}
|
2022-02-14 02:14:16 +00:00
|
|
|
{...selectProps}
|
2021-09-25 08:51:32 +00:00
|
|
|
virtual={virtual.value}
|
|
|
|
dropdownMatchSelectWidth={dropdownMatchSelectWidth.value}
|
|
|
|
id={id}
|
|
|
|
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}
|
2021-12-17 08:59:50 +00:00
|
|
|
choiceTransitionName={choiceTransitionName.value}
|
2021-09-25 08:51:32 +00:00
|
|
|
onChange={handleChange}
|
|
|
|
onBlur={handleBlur}
|
|
|
|
onSearch={handleSearch}
|
|
|
|
onTreeExpand={handleTreeExpand}
|
|
|
|
v-slots={{
|
|
|
|
...slots,
|
|
|
|
treeCheckable: () => <span class={`${prefixCls.value}-tree-checkbox-inner`} />,
|
|
|
|
}}
|
|
|
|
{...otherProps}
|
2021-12-17 08:59:50 +00:00
|
|
|
transitionName={transitionName.value}
|
2022-01-21 13:58:10 +00:00
|
|
|
customSlots={{
|
|
|
|
...slots,
|
|
|
|
treeCheckable: () => <span class={`${prefixCls.value}-tree-checkbox-inner`} />,
|
|
|
|
}}
|
|
|
|
maxTagPlaceholder={props.maxTagPlaceholder || slots.maxTagPlaceholder}
|
2021-09-25 08:51:32 +00:00
|
|
|
/>
|
|
|
|
);
|
2019-05-28 03:37:38 +00:00
|
|
|
};
|
2018-07-11 09:51:20 +00:00
|
|
|
},
|
2020-10-22 02:06:57 +00:00
|
|
|
});
|
2018-09-19 05:21:57 +00:00
|
|
|
|
|
|
|
/* istanbul ignore next */
|
2021-11-10 02:06:59 +00:00
|
|
|
export const TreeSelectNode = TreeNode;
|
|
|
|
export default Object.assign(TreeSelect, {
|
|
|
|
TreeNode,
|
2022-01-03 14:45:16 +00:00
|
|
|
SHOW_ALL: SHOW_ALL as typeof SHOW_ALL,
|
|
|
|
SHOW_PARENT: SHOW_PARENT as typeof SHOW_PARENT,
|
|
|
|
SHOW_CHILD: SHOW_CHILD as typeof SHOW_CHILD,
|
2021-11-10 02:06:59 +00:00
|
|
|
install: (app: App) => {
|
|
|
|
app.component(TreeSelect.name, TreeSelect);
|
|
|
|
app.component(TreeSelectNode.displayName, TreeSelectNode);
|
|
|
|
return app;
|
|
|
|
},
|
|
|
|
});
|