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';
|
2019-08-05 18:38:10 +08:00
|
|
|
import omit from 'omit.js';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2020-05-14 15:57:04 +08:00
|
|
|
import devWarning from '../_util/devWarning';
|
2018-08-24 14:08:06 +08:00
|
|
|
import { AntTreeNodeProps } 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';
|
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>,
|
|
|
|
'showTreeIcon' | 'treeMotion' | 'inputIcon' | 'mode' | 'getInputElement' | 'backfill'
|
|
|
|
> {
|
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;
|
2019-09-28 11:31:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class TreeSelect<T> extends React.Component<TreeSelectProps<T>, {}> {
|
2016-03-29 14:01:10 +08:00
|
|
|
static TreeNode = TreeNode;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-03-03 10:47:32 +08:00
|
|
|
static SHOW_ALL: typeof SHOW_ALL = SHOW_ALL;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-03-03 10:47:32 +08:00
|
|
|
static SHOW_PARENT: typeof SHOW_PARENT = SHOW_PARENT;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-03-03 10:47:32 +08:00
|
|
|
static SHOW_CHILD: typeof SHOW_CHILD = SHOW_CHILD;
|
2016-03-29 14:01:10 +08:00
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
transitionName: 'slide-up',
|
|
|
|
choiceTransitionName: 'zoom',
|
2020-02-06 10:02:02 +08:00
|
|
|
bordered: true,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-05-08 16:05:36 +08:00
|
|
|
|
2019-09-28 11:31:28 +08:00
|
|
|
selectRef = React.createRef<RcTreeSelect>();
|
2017-11-19 01:41:40 +08:00
|
|
|
|
2019-06-16 20:51:47 +08:00
|
|
|
constructor(props: TreeSelectProps<T>) {
|
2017-07-29 14:50:11 +08:00
|
|
|
super(props);
|
|
|
|
|
2020-05-14 15:57:04 +08:00
|
|
|
devWarning(
|
2017-07-29 14:50:11 +08:00
|
|
|
props.multiple !== false || !props.treeCheckable,
|
2019-02-27 15:32:29 +08:00
|
|
|
'TreeSelect',
|
2017-07-29 14:50:11 +08:00
|
|
|
'`multiple` will alway be `true` when `treeCheckable` is true',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-19 01:41:40 +08:00
|
|
|
focus() {
|
2019-09-28 11:31:28 +08:00
|
|
|
if (this.selectRef.current) {
|
|
|
|
this.selectRef.current.focus();
|
|
|
|
}
|
2017-11-19 01:41:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
blur() {
|
2019-09-28 11:31:28 +08:00
|
|
|
if (this.selectRef.current) {
|
|
|
|
this.selectRef.current.blur();
|
2018-08-24 14:08:06 +08:00
|
|
|
}
|
2019-09-28 11:31:28 +08:00
|
|
|
}
|
2018-08-24 14:08:06 +08:00
|
|
|
|
2018-12-26 16:01:00 +08:00
|
|
|
renderTreeSelect = ({
|
|
|
|
getPopupContainer: getContextPopupContainer,
|
|
|
|
getPrefixCls,
|
|
|
|
renderEmpty,
|
2020-01-02 19:10:16 +08:00
|
|
|
direction,
|
2018-12-26 16:01:00 +08:00
|
|
|
}: ConfigConsumerProps) => {
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
2020-01-03 13:38:16 +08:00
|
|
|
size: customizeSize,
|
2019-09-28 11:31:28 +08:00
|
|
|
className,
|
|
|
|
treeCheckable,
|
|
|
|
multiple,
|
|
|
|
listHeight = 256,
|
2020-04-14 19:17:42 +08:00
|
|
|
listItemHeight = 26,
|
2018-12-26 16:01:00 +08:00
|
|
|
notFoundContent,
|
2019-09-28 11:31:28 +08:00
|
|
|
switcherIcon,
|
|
|
|
treeLine,
|
2018-12-26 16:01:00 +08:00
|
|
|
getPopupContainer,
|
2019-09-28 11:31:28 +08:00
|
|
|
dropdownClassName,
|
2020-02-06 10:02:02 +08:00
|
|
|
bordered,
|
2020-03-20 16:03:13 +08:00
|
|
|
treeIcon = false,
|
2018-12-26 16:01:00 +08:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('select', customizePrefixCls);
|
2019-09-28 11:31:28 +08:00
|
|
|
const treePrefixCls = getPrefixCls('select-tree', customizePrefixCls);
|
|
|
|
const treeSelectPrefixCls = getPrefixCls('tree-select', customizePrefixCls);
|
|
|
|
|
|
|
|
const mergedDropdownClassName = classNames(
|
|
|
|
dropdownClassName,
|
|
|
|
`${treeSelectPrefixCls}-dropdown`,
|
2020-01-02 19:10:16 +08:00
|
|
|
{
|
|
|
|
[`${treeSelectPrefixCls}-dropdown-rtl`]: direction === 'rtl',
|
|
|
|
},
|
2019-09-28 11:31:28 +08:00
|
|
|
);
|
2019-05-06 12:04:39 +08:00
|
|
|
|
2019-09-28 11:31:28 +08:00
|
|
|
const isMultiple = !!(treeCheckable || multiple);
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2019-09-28 11:31:28 +08:00
|
|
|
// ===================== Icons =====================
|
|
|
|
const { suffixIcon, itemIcon, removeIcon, clearIcon } = getIcons({
|
|
|
|
...this.props,
|
|
|
|
multiple: isMultiple,
|
|
|
|
});
|
2018-12-26 16:01:00 +08:00
|
|
|
|
2019-09-28 11:31:28 +08:00
|
|
|
// ===================== Empty =====================
|
2020-01-03 13:38:16 +08:00
|
|
|
let mergedNotFound: React.ReactNode;
|
2019-09-28 11:31:28 +08:00
|
|
|
if (notFoundContent !== undefined) {
|
|
|
|
mergedNotFound = notFoundContent;
|
|
|
|
} else {
|
|
|
|
mergedNotFound = renderEmpty('Select');
|
|
|
|
}
|
2018-12-26 16:01:00 +08:00
|
|
|
|
2019-09-28 11:31:28 +08:00
|
|
|
// ==================== Render =====================
|
|
|
|
const selectProps = omit(this.props, [
|
|
|
|
'prefixCls',
|
|
|
|
'suffixIcon',
|
|
|
|
'itemIcon',
|
|
|
|
'removeIcon',
|
|
|
|
'clearIcon',
|
|
|
|
'switcherIcon',
|
|
|
|
'size',
|
2020-02-06 10:02:02 +08:00
|
|
|
'bordered',
|
2019-09-28 11:31:28 +08:00
|
|
|
]);
|
2017-03-17 15:23:25 +08:00
|
|
|
|
2017-10-20 14:54:38 +08:00
|
|
|
return (
|
2020-01-03 13:38:16 +08:00
|
|
|
<SizeContext.Consumer>
|
|
|
|
{size => {
|
|
|
|
const mergedSize = customizeSize || size;
|
|
|
|
const mergedClassName = classNames(
|
|
|
|
!customizePrefixCls && treeSelectPrefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-lg`]: mergedSize === 'large',
|
|
|
|
[`${prefixCls}-sm`]: mergedSize === 'small',
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2020-02-06 10:02:02 +08:00
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
2020-01-03 13:38:16 +08:00
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<RcTreeSelect
|
|
|
|
{...selectProps}
|
|
|
|
ref={this.selectRef}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
className={mergedClassName}
|
|
|
|
listHeight={listHeight}
|
|
|
|
listItemHeight={listItemHeight}
|
|
|
|
treeCheckable={
|
|
|
|
treeCheckable ? (
|
|
|
|
<span className={`${prefixCls}-tree-checkbox-inner`} />
|
|
|
|
) : (
|
|
|
|
treeCheckable
|
|
|
|
)
|
|
|
|
}
|
|
|
|
inputIcon={suffixIcon}
|
|
|
|
menuItemSelectedIcon={itemIcon}
|
|
|
|
removeIcon={removeIcon}
|
|
|
|
clearIcon={clearIcon}
|
|
|
|
switcherIcon={(nodeProps: AntTreeNodeProps) =>
|
|
|
|
renderSwitcherIcon(treePrefixCls, switcherIcon, treeLine, nodeProps)
|
|
|
|
}
|
2020-03-20 16:03:13 +08:00
|
|
|
showTreeIcon={treeIcon}
|
2020-01-03 13:38:16 +08:00
|
|
|
notFoundContent={mergedNotFound}
|
|
|
|
getPopupContainer={getPopupContainer || getContextPopupContainer}
|
2020-02-17 14:53:22 +08:00
|
|
|
treeMotion={null}
|
2020-01-03 13:38:16 +08:00
|
|
|
dropdownClassName={mergedDropdownClassName}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</SizeContext.Consumer>
|
2017-10-20 14:54:38 +08:00
|
|
|
);
|
2018-12-26 16:01:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return <ConfigConsumer>{this.renderTreeSelect}</ConfigConsumer>;
|
2017-10-20 14:54:38 +08:00
|
|
|
}
|
|
|
|
}
|
2019-09-28 11:31:28 +08:00
|
|
|
|
|
|
|
export { TreeNode };
|
|
|
|
|
|
|
|
export default TreeSelect;
|