2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2019-09-28 11:31:28 +08:00
|
|
|
import RcTreeSelect, {
|
|
|
|
TreeNode,
|
|
|
|
SHOW_ALL,
|
|
|
|
SHOW_PARENT,
|
|
|
|
SHOW_CHILD,
|
|
|
|
TreeSelectProps as RcTreeSelectProps,
|
|
|
|
} from 'rc-tree-select';
|
2015-12-28 19:16:02 +08:00
|
|
|
import classNames from 'classnames';
|
2021-01-13 21:00:30 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2020-11-08 15:27:30 +08:00
|
|
|
import { DefaultValueType } from 'rc-tree-select/lib/interface';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
2020-05-14 15:57:04 +08:00
|
|
|
import devWarning from '../_util/devWarning';
|
2021-05-27 14:48:48 +08:00
|
|
|
import { AntTreeNodeProps, TreeProps } from '../tree';
|
2019-09-28 11:31:28 +08:00
|
|
|
import getIcons from '../select/utils/iconUtil';
|
|
|
|
import renderSwitcherIcon from '../tree/utils/iconUtil';
|
2020-01-03 13:38:16 +08:00
|
|
|
import SizeContext, { SizeType } from '../config-provider/SizeContext';
|
2021-02-08 17:09:13 +08:00
|
|
|
import { getTransitionName } from '../_util/motion';
|
2019-09-28 11:31:28 +08:00
|
|
|
|
|
|
|
type RawValue = string | number;
|
|
|
|
|
|
|
|
export interface LabeledValue {
|
|
|
|
key?: string;
|
|
|
|
value: RawValue;
|
|
|
|
label: React.ReactNode;
|
|
|
|
}
|
2015-12-28 19:16:02 +08:00
|
|
|
|
2019-09-28 11:31:28 +08:00
|
|
|
export type SelectValue = RawValue | RawValue[] | LabeledValue | LabeledValue[];
|
2016-08-01 16:35:01 +08:00
|
|
|
|
2019-09-28 11:31:28 +08:00
|
|
|
export interface TreeSelectProps<T>
|
|
|
|
extends Omit<
|
|
|
|
RcTreeSelectProps<T>,
|
2021-05-27 14:48:48 +08:00
|
|
|
| 'showTreeIcon'
|
|
|
|
| 'treeMotion'
|
|
|
|
| 'inputIcon'
|
|
|
|
| 'mode'
|
|
|
|
| 'getInputElement'
|
|
|
|
| 'backfill'
|
|
|
|
| 'treeLine'
|
2019-09-28 11:31:28 +08:00
|
|
|
> {
|
2020-02-29 23:35:57 +08:00
|
|
|
suffixIcon?: React.ReactNode;
|
2020-01-03 13:38:16 +08:00
|
|
|
size?: SizeType;
|
2020-02-06 10:02:02 +08:00
|
|
|
bordered?: boolean;
|
2021-05-27 14:48:48 +08:00
|
|
|
treeLine?: TreeProps['showLine'];
|
2019-09-28 11:31:28 +08:00
|
|
|
}
|
|
|
|
|
2020-11-08 15:27:30 +08:00
|
|
|
export interface RefTreeSelectProps {
|
|
|
|
focus: () => void;
|
|
|
|
blur: () => void;
|
|
|
|
}
|
2018-08-24 14:08:06 +08:00
|
|
|
|
2020-11-08 15:27:30 +08:00
|
|
|
const InternalTreeSelect = <T extends DefaultValueType>(
|
|
|
|
{
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
size: customizeSize,
|
|
|
|
bordered = true,
|
|
|
|
className,
|
|
|
|
treeCheckable,
|
|
|
|
multiple,
|
|
|
|
listHeight = 256,
|
|
|
|
listItemHeight = 26,
|
|
|
|
notFoundContent,
|
|
|
|
switcherIcon,
|
|
|
|
treeLine,
|
|
|
|
getPopupContainer,
|
|
|
|
dropdownClassName,
|
|
|
|
treeIcon = false,
|
2021-02-08 17:09:13 +08:00
|
|
|
transitionName,
|
2020-11-08 15:27:30 +08:00
|
|
|
choiceTransitionName = '',
|
|
|
|
...props
|
|
|
|
}: TreeSelectProps<T>,
|
|
|
|
ref: React.Ref<RefTreeSelectProps>,
|
|
|
|
) => {
|
|
|
|
const {
|
2018-12-26 16:01:00 +08:00
|
|
|
getPopupContainer: getContextPopupContainer,
|
|
|
|
getPrefixCls,
|
|
|
|
renderEmpty,
|
2020-01-02 19:10:16 +08:00
|
|
|
direction,
|
2020-05-05 15:00:43 +08:00
|
|
|
virtual,
|
|
|
|
dropdownMatchSelectWidth,
|
2020-11-08 15:27:30 +08:00
|
|
|
} = React.useContext(ConfigContext);
|
|
|
|
const size = React.useContext(SizeContext);
|
|
|
|
|
|
|
|
devWarning(
|
|
|
|
multiple !== false || !treeCheckable,
|
|
|
|
'TreeSelect',
|
2021-07-03 16:05:30 +08:00
|
|
|
'`multiple` will always be `true` when `treeCheckable` is true',
|
2020-11-08 15:27:30 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('select', customizePrefixCls);
|
|
|
|
const treePrefixCls = getPrefixCls('select-tree', customizePrefixCls);
|
|
|
|
const treeSelectPrefixCls = getPrefixCls('tree-select', customizePrefixCls);
|
|
|
|
|
|
|
|
const mergedDropdownClassName = classNames(dropdownClassName, `${treeSelectPrefixCls}-dropdown`, {
|
|
|
|
[`${treeSelectPrefixCls}-dropdown-rtl`]: direction === 'rtl',
|
|
|
|
});
|
|
|
|
|
|
|
|
const isMultiple = !!(treeCheckable || multiple);
|
|
|
|
|
|
|
|
// ===================== Icons =====================
|
2021-01-13 21:00:30 +08:00
|
|
|
const { suffixIcon, removeIcon, clearIcon } = getIcons({
|
2020-11-08 15:27:30 +08:00
|
|
|
...props,
|
|
|
|
multiple: isMultiple,
|
|
|
|
prefixCls,
|
|
|
|
});
|
|
|
|
|
|
|
|
// ===================== Empty =====================
|
|
|
|
let mergedNotFound: React.ReactNode;
|
|
|
|
if (notFoundContent !== undefined) {
|
|
|
|
mergedNotFound = notFoundContent;
|
|
|
|
} else {
|
|
|
|
mergedNotFound = renderEmpty('Select');
|
2017-10-20 14:54:38 +08:00
|
|
|
}
|
2020-11-08 15:27:30 +08:00
|
|
|
|
|
|
|
// ==================== Render =====================
|
2021-01-13 21:00:30 +08:00
|
|
|
const selectProps = omit(props as typeof props & { itemIcon: any; switcherIcon: any }, [
|
2020-11-08 15:27:30 +08:00
|
|
|
'suffixIcon',
|
|
|
|
'itemIcon',
|
|
|
|
'removeIcon',
|
|
|
|
'clearIcon',
|
|
|
|
'switcherIcon',
|
|
|
|
]);
|
|
|
|
|
|
|
|
const mergedSize = customizeSize || size;
|
|
|
|
const mergedClassName = classNames(
|
|
|
|
!customizePrefixCls && treeSelectPrefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-lg`]: mergedSize === 'large',
|
|
|
|
[`${prefixCls}-sm`]: mergedSize === 'small',
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2021-02-08 17:09:13 +08:00
|
|
|
const rootPrefixCls = getPrefixCls();
|
2020-11-08 15:27:30 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<RcTreeSelect
|
|
|
|
virtual={virtual}
|
|
|
|
dropdownMatchSelectWidth={dropdownMatchSelectWidth}
|
|
|
|
{...selectProps}
|
2021-01-13 21:00:30 +08:00
|
|
|
ref={ref as any}
|
2020-11-08 15:27:30 +08:00
|
|
|
prefixCls={prefixCls}
|
|
|
|
className={mergedClassName}
|
|
|
|
listHeight={listHeight}
|
|
|
|
listItemHeight={listItemHeight}
|
|
|
|
treeCheckable={
|
|
|
|
treeCheckable ? <span className={`${prefixCls}-tree-checkbox-inner`} /> : treeCheckable
|
|
|
|
}
|
2021-05-27 14:48:48 +08:00
|
|
|
treeLine={!!treeLine}
|
2020-11-08 15:27:30 +08:00
|
|
|
inputIcon={suffixIcon}
|
|
|
|
multiple={multiple}
|
|
|
|
removeIcon={removeIcon}
|
|
|
|
clearIcon={clearIcon}
|
|
|
|
switcherIcon={(nodeProps: AntTreeNodeProps) =>
|
|
|
|
renderSwitcherIcon(treePrefixCls, switcherIcon, treeLine, nodeProps)
|
|
|
|
}
|
2021-01-13 21:00:30 +08:00
|
|
|
showTreeIcon={treeIcon as any}
|
2020-11-08 15:27:30 +08:00
|
|
|
notFoundContent={mergedNotFound}
|
|
|
|
getPopupContainer={getPopupContainer || getContextPopupContainer}
|
|
|
|
treeMotion={null}
|
|
|
|
dropdownClassName={mergedDropdownClassName}
|
2021-02-08 17:09:13 +08:00
|
|
|
choiceTransitionName={getTransitionName(rootPrefixCls, '', choiceTransitionName)}
|
|
|
|
transitionName={getTransitionName(rootPrefixCls, 'slide-up', transitionName)}
|
2020-11-08 15:27:30 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const TreeSelectRef = React.forwardRef(InternalTreeSelect) as <T extends DefaultValueType>(
|
|
|
|
props: TreeSelectProps<T> & { ref?: React.Ref<RefTreeSelectProps> },
|
|
|
|
) => React.ReactElement;
|
|
|
|
|
|
|
|
type InternalTreeSelectType = typeof TreeSelectRef;
|
|
|
|
|
|
|
|
interface TreeSelectInterface extends InternalTreeSelectType {
|
|
|
|
TreeNode: typeof TreeNode;
|
|
|
|
SHOW_ALL: typeof SHOW_ALL;
|
|
|
|
SHOW_PARENT: typeof SHOW_PARENT;
|
|
|
|
SHOW_CHILD: typeof SHOW_CHILD;
|
2017-10-20 14:54:38 +08:00
|
|
|
}
|
2019-09-28 11:31:28 +08:00
|
|
|
|
2020-11-08 15:27:30 +08:00
|
|
|
const TreeSelect = TreeSelectRef as TreeSelectInterface;
|
|
|
|
|
|
|
|
TreeSelect.TreeNode = TreeNode;
|
|
|
|
TreeSelect.SHOW_ALL = SHOW_ALL;
|
|
|
|
TreeSelect.SHOW_PARENT = SHOW_PARENT;
|
|
|
|
TreeSelect.SHOW_CHILD = SHOW_CHILD;
|
|
|
|
|
2019-09-28 11:31:28 +08:00
|
|
|
export { TreeNode };
|
|
|
|
|
|
|
|
export default TreeSelect;
|