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';
|
2019-09-28 11:31:28 +08:00
|
|
|
import collapseMotion from '../_util/motion';
|
2017-07-29 14:50:11 +08:00
|
|
|
import warning from '../_util/warning';
|
2018-08-24 14:08:06 +08:00
|
|
|
import { AntTreeNodeProps } from '../tree';
|
2019-09-28 11:31:28 +08:00
|
|
|
import { Size } from '../select';
|
|
|
|
import getIcons from '../select/utils/iconUtil';
|
|
|
|
import renderSwitcherIcon from '../tree/utils/iconUtil';
|
|
|
|
|
|
|
|
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'
|
|
|
|
> {
|
|
|
|
size?: Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-03-29 14:01:10 +08:00
|
|
|
static SHOW_ALL = SHOW_ALL;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2016-03-29 14:01:10 +08:00
|
|
|
static SHOW_PARENT = SHOW_PARENT;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2016-03-29 14:01:10 +08:00
|
|
|
static SHOW_CHILD = SHOW_CHILD;
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
transitionName: 'slide-up',
|
|
|
|
choiceTransitionName: 'zoom',
|
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);
|
|
|
|
|
|
|
|
warning(
|
|
|
|
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,
|
|
|
|
}: ConfigConsumerProps) => {
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
size,
|
2019-09-28 11:31:28 +08:00
|
|
|
className,
|
|
|
|
treeCheckable,
|
|
|
|
multiple,
|
|
|
|
listHeight = 256,
|
|
|
|
listItemHeight = 32,
|
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,
|
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 mergedClassName = classNames(
|
|
|
|
!customizePrefixCls && treeSelectPrefixCls,
|
2018-12-26 16:01:00 +08:00
|
|
|
{
|
|
|
|
[`${prefixCls}-lg`]: size === 'large',
|
|
|
|
[`${prefixCls}-sm`]: size === 'small',
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
|
|
|
|
2019-09-28 11:31:28 +08:00
|
|
|
const mergedDropdownClassName = classNames(
|
|
|
|
dropdownClassName,
|
|
|
|
`${treeSelectPrefixCls}-dropdown`,
|
|
|
|
);
|
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 =====================
|
|
|
|
let mergedNotFound;
|
|
|
|
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',
|
|
|
|
]);
|
2017-03-17 15:23:25 +08:00
|
|
|
|
2017-10-20 14:54:38 +08:00
|
|
|
return (
|
2018-12-26 16:01:00 +08:00
|
|
|
<RcTreeSelect
|
2019-09-28 11:31:28 +08:00
|
|
|
{...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}
|
2018-12-26 16:01:00 +08:00
|
|
|
switcherIcon={(nodeProps: AntTreeNodeProps) =>
|
2019-09-28 11:31:28 +08:00
|
|
|
renderSwitcherIcon(treePrefixCls, switcherIcon, treeLine, nodeProps)
|
2018-12-26 16:01:00 +08:00
|
|
|
}
|
2019-09-28 11:31:28 +08:00
|
|
|
showTreeIcon={false}
|
|
|
|
notFoundContent={mergedNotFound}
|
2018-12-26 16:01:00 +08:00
|
|
|
getPopupContainer={getPopupContainer || getContextPopupContainer}
|
2019-09-28 11:31:28 +08:00
|
|
|
treeMotion={collapseMotion}
|
|
|
|
dropdownClassName={mergedDropdownClassName}
|
2018-12-26 16:01:00 +08:00
|
|
|
/>
|
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;
|