2015-12-28 19:16:02 +08:00
|
|
|
import classNames from 'classnames';
|
2021-12-21 21:38:11 +08:00
|
|
|
import type { BaseSelectRef } from 'rc-select';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { TreeSelectProps as RcTreeSelectProps } from 'rc-tree-select';
|
|
|
|
import RcTreeSelect, { SHOW_ALL, SHOW_CHILD, SHOW_PARENT, TreeNode } from 'rc-tree-select';
|
|
|
|
import type { BaseOptionType, DefaultOptionType } from 'rc-tree-select/lib/TreeSelect';
|
|
|
|
import omit from 'rc-util/lib/omit';
|
|
|
|
import * as React from 'react';
|
2022-02-17 16:41:12 +08:00
|
|
|
import { useContext } from 'react';
|
2020-11-08 15:27:30 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-06-22 14:57:09 +08:00
|
|
|
import defaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
|
|
|
import DisabledContext from '../config-provider/DisabledContext';
|
|
|
|
import type { SizeType } from '../config-provider/SizeContext';
|
|
|
|
import SizeContext from '../config-provider/SizeContext';
|
|
|
|
import { FormItemInputContext } from '../form/context';
|
|
|
|
import getIcons from '../select/utils/iconUtil';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { AntTreeNodeProps, TreeProps } from '../tree';
|
|
|
|
import type { SwitcherIcon } from '../tree/Tree';
|
2019-09-28 11:31:28 +08:00
|
|
|
import renderSwitcherIcon from '../tree/utils/iconUtil';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { SelectCommonPlacement } from '../_util/motion';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { getTransitionDirection, getTransitionName } from '../_util/motion';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { InputStatus } from '../_util/statusUtils';
|
|
|
|
import { getMergedStatus, getStatusClassNames } from '../_util/statusUtils';
|
2022-10-18 16:23:10 +08:00
|
|
|
import { useCompactItemContext } from '../space/Compact';
|
2022-06-22 14:57:09 +08:00
|
|
|
import warning from '../_util/warning';
|
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
|
|
|
|
2021-12-21 21:38:11 +08:00
|
|
|
export interface TreeSelectProps<
|
|
|
|
ValueType = any,
|
|
|
|
OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType,
|
|
|
|
> extends Omit<
|
|
|
|
RcTreeSelectProps<ValueType, OptionType>,
|
2021-05-27 14:48:48 +08:00
|
|
|
| 'showTreeIcon'
|
|
|
|
| 'treeMotion'
|
|
|
|
| 'inputIcon'
|
|
|
|
| 'mode'
|
|
|
|
| 'getInputElement'
|
|
|
|
| 'backfill'
|
|
|
|
| 'treeLine'
|
2022-06-23 12:17:05 +08:00
|
|
|
| 'switcherIcon'
|
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;
|
2022-04-29 20:48:10 +08:00
|
|
|
disabled?: boolean;
|
2022-01-20 16:54:47 +08:00
|
|
|
placement?: SelectCommonPlacement;
|
2022-08-05 11:21:07 +08:00
|
|
|
/**
|
|
|
|
* @deprecated `dropdownClassName` is deprecated which will be removed in next major
|
|
|
|
* version.Please use `popupClassName` instead.
|
|
|
|
*/
|
|
|
|
dropdownClassName?: string;
|
|
|
|
popupClassName?: string;
|
2020-02-06 10:02:02 +08:00
|
|
|
bordered?: boolean;
|
2021-05-27 14:48:48 +08:00
|
|
|
treeLine?: TreeProps['showLine'];
|
2022-02-17 16:41:12 +08:00
|
|
|
status?: InputStatus;
|
2022-06-23 12:17:05 +08:00
|
|
|
switcherIcon?: SwitcherIcon | RcTreeSelectProps<ValueType, OptionType>['switcherIcon'];
|
2019-09-28 11:31:28 +08:00
|
|
|
}
|
|
|
|
|
2021-12-21 21:38:11 +08:00
|
|
|
const InternalTreeSelect = <OptionType extends BaseOptionType | DefaultOptionType = BaseOptionType>(
|
2020-11-08 15:27:30 +08:00
|
|
|
{
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
size: customizeSize,
|
2022-04-29 20:48:10 +08:00
|
|
|
disabled: customDisabled,
|
2020-11-08 15:27:30 +08:00
|
|
|
bordered = true,
|
|
|
|
className,
|
|
|
|
treeCheckable,
|
|
|
|
multiple,
|
|
|
|
listHeight = 256,
|
|
|
|
listItemHeight = 26,
|
2022-01-20 16:54:47 +08:00
|
|
|
placement,
|
2020-11-08 15:27:30 +08:00
|
|
|
notFoundContent,
|
|
|
|
switcherIcon,
|
|
|
|
treeLine,
|
|
|
|
getPopupContainer,
|
|
|
|
dropdownClassName,
|
2022-08-05 11:21:07 +08:00
|
|
|
popupClassName,
|
2020-11-08 15:27:30 +08:00
|
|
|
treeIcon = false,
|
2021-02-08 17:09:13 +08:00
|
|
|
transitionName,
|
2020-11-08 15:27:30 +08:00
|
|
|
choiceTransitionName = '',
|
2022-02-17 16:41:12 +08:00
|
|
|
status: customStatus,
|
|
|
|
showArrow,
|
2022-06-06 18:44:58 +08:00
|
|
|
treeExpandAction,
|
2020-11-08 15:27:30 +08:00
|
|
|
...props
|
2021-12-21 21:38:11 +08:00
|
|
|
}: TreeSelectProps<OptionType>,
|
|
|
|
ref: React.Ref<BaseSelectRef>,
|
2020-11-08 15:27:30 +08:00
|
|
|
) => {
|
|
|
|
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);
|
|
|
|
|
2022-05-10 15:43:29 +08:00
|
|
|
warning(
|
2020-11-08 15:27:30 +08:00
|
|
|
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
|
|
|
);
|
|
|
|
|
2022-08-05 11:21:07 +08:00
|
|
|
warning(
|
|
|
|
!dropdownClassName,
|
|
|
|
'TreeSelect',
|
|
|
|
'`dropdownClassName` is deprecated which will be removed in next major version. Please use `popupClassName` instead.',
|
|
|
|
);
|
|
|
|
|
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);
|
2022-10-18 16:23:10 +08:00
|
|
|
const { compactSize, compactItemClassnames } = useCompactItemContext(prefixCls, direction);
|
2020-11-08 15:27:30 +08:00
|
|
|
|
2022-08-05 11:21:07 +08:00
|
|
|
const mergedDropdownClassName = classNames(
|
|
|
|
popupClassName || dropdownClassName,
|
|
|
|
`${treeSelectPrefixCls}-dropdown`,
|
|
|
|
{
|
|
|
|
[`${treeSelectPrefixCls}-dropdown-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
);
|
2020-11-08 15:27:30 +08:00
|
|
|
|
|
|
|
const isMultiple = !!(treeCheckable || multiple);
|
2022-02-17 16:41:12 +08:00
|
|
|
const mergedShowArrow = showArrow !== undefined ? showArrow : props.loading || !isMultiple;
|
|
|
|
|
2022-03-24 21:54:20 +08:00
|
|
|
// ===================== Form =====================
|
2022-03-25 17:48:12 +08:00
|
|
|
const {
|
|
|
|
status: contextStatus,
|
|
|
|
hasFeedback,
|
|
|
|
isFormItemInput,
|
|
|
|
feedbackIcon,
|
|
|
|
} = useContext(FormItemInputContext);
|
2022-02-17 16:41:12 +08:00
|
|
|
const mergedStatus = getMergedStatus(contextStatus, customStatus);
|
2020-11-08 15:27:30 +08:00
|
|
|
|
|
|
|
// ===================== 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,
|
2022-02-17 16:41:12 +08:00
|
|
|
showArrow: mergedShowArrow,
|
|
|
|
hasFeedback,
|
2022-03-25 17:48:12 +08:00
|
|
|
feedbackIcon,
|
2020-11-08 15:27:30 +08:00
|
|
|
prefixCls,
|
|
|
|
});
|
|
|
|
|
|
|
|
// ===================== Empty =====================
|
|
|
|
let mergedNotFound: React.ReactNode;
|
|
|
|
if (notFoundContent !== undefined) {
|
|
|
|
mergedNotFound = notFoundContent;
|
|
|
|
} else {
|
2022-05-16 16:34:42 +08:00
|
|
|
mergedNotFound = (renderEmpty || defaultRenderEmpty)('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',
|
|
|
|
]);
|
|
|
|
|
2022-01-20 16:54:47 +08:00
|
|
|
// ===================== Placement =====================
|
|
|
|
const getPlacement = () => {
|
|
|
|
if (placement !== undefined) {
|
|
|
|
return placement;
|
|
|
|
}
|
|
|
|
return direction === 'rtl'
|
|
|
|
? ('bottomRight' as SelectCommonPlacement)
|
|
|
|
: ('bottomLeft' as SelectCommonPlacement);
|
|
|
|
};
|
|
|
|
|
2022-10-18 16:23:10 +08:00
|
|
|
const mergedSize = compactSize || customizeSize || size;
|
2022-04-29 20:48:10 +08:00
|
|
|
// ===================== Disabled =====================
|
|
|
|
const disabled = React.useContext(DisabledContext);
|
2022-09-20 16:48:59 +08:00
|
|
|
const mergedDisabled = customDisabled ?? disabled;
|
2022-04-29 20:48:10 +08:00
|
|
|
|
2020-11-08 15:27:30 +08:00
|
|
|
const mergedClassName = classNames(
|
|
|
|
!customizePrefixCls && treeSelectPrefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-lg`]: mergedSize === 'large',
|
|
|
|
[`${prefixCls}-sm`]: mergedSize === 'small',
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
2022-03-24 21:54:20 +08:00
|
|
|
[`${prefixCls}-in-form-item`]: isFormItemInput,
|
2020-11-08 15:27:30 +08:00
|
|
|
},
|
2022-02-17 16:41:12 +08:00
|
|
|
getStatusClassNames(prefixCls, mergedStatus, hasFeedback),
|
2022-10-18 16:23:10 +08:00
|
|
|
compactItemClassnames,
|
2020-11-08 15:27:30 +08:00
|
|
|
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}
|
2022-04-29 20:48:10 +08:00
|
|
|
disabled={mergedDisabled}
|
2020-11-08 15:27:30 +08:00
|
|
|
{...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}
|
2022-01-20 16:54:47 +08:00
|
|
|
placement={getPlacement()}
|
2020-11-08 15:27:30 +08:00
|
|
|
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)}
|
2022-01-20 16:54:47 +08:00
|
|
|
transitionName={getTransitionName(
|
|
|
|
rootPrefixCls,
|
|
|
|
getTransitionDirection(placement),
|
|
|
|
transitionName,
|
|
|
|
)}
|
2022-02-17 16:41:12 +08:00
|
|
|
showArrow={hasFeedback || showArrow}
|
2022-06-06 18:44:58 +08:00
|
|
|
treeExpandAction={treeExpandAction}
|
2020-11-08 15:27:30 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-12-21 21:38:11 +08:00
|
|
|
const TreeSelectRef = React.forwardRef(InternalTreeSelect) as <
|
|
|
|
ValueType = any,
|
|
|
|
OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType,
|
|
|
|
>(
|
|
|
|
props: React.PropsWithChildren<TreeSelectProps<ValueType, OptionType>> & {
|
|
|
|
ref?: React.Ref<BaseSelectRef>;
|
|
|
|
},
|
2020-11-08 15:27:30 +08:00
|
|
|
) => React.ReactElement;
|
|
|
|
|
|
|
|
type InternalTreeSelectType = typeof TreeSelectRef;
|
|
|
|
|
2023-01-03 11:46:48 +08:00
|
|
|
type CompoundedComponent = InternalTreeSelectType & {
|
2020-11-08 15:27:30 +08:00
|
|
|
TreeNode: typeof TreeNode;
|
|
|
|
SHOW_ALL: typeof SHOW_ALL;
|
|
|
|
SHOW_PARENT: typeof SHOW_PARENT;
|
|
|
|
SHOW_CHILD: typeof SHOW_CHILD;
|
2023-01-03 11:46:48 +08:00
|
|
|
};
|
2019-09-28 11:31:28 +08:00
|
|
|
|
2023-01-03 11:46:48 +08:00
|
|
|
const TreeSelect = TreeSelectRef as CompoundedComponent;
|
2020-11-08 15:27:30 +08:00
|
|
|
|
|
|
|
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;
|