2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2015-12-29 18:31:48 +08:00
|
|
|
import classNames from 'classnames';
|
2021-08-31 15:51:02 +08:00
|
|
|
import RcCascader from 'rc-cascader';
|
2021-12-21 21:38:11 +08:00
|
|
|
import type {
|
|
|
|
CascaderProps as RcCascaderProps,
|
|
|
|
ShowSearchType,
|
|
|
|
FieldNames,
|
|
|
|
BaseOptionType,
|
|
|
|
DefaultOptionType,
|
|
|
|
} from 'rc-cascader';
|
2021-01-13 21:00:30 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2020-03-02 12:09:38 +08:00
|
|
|
import RightOutlined from '@ant-design/icons/RightOutlined';
|
|
|
|
import RedoOutlined from '@ant-design/icons/RedoOutlined';
|
|
|
|
import LeftOutlined from '@ant-design/icons/LeftOutlined';
|
2021-09-13 15:59:57 +08:00
|
|
|
import devWarning from '../_util/devWarning';
|
2021-08-31 15:51:02 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
|
|
|
import type { SizeType } from '../config-provider/SizeContext';
|
|
|
|
import SizeContext from '../config-provider/SizeContext';
|
|
|
|
import getIcons from '../select/utils/iconUtil';
|
2021-02-08 17:09:13 +08:00
|
|
|
import { getTransitionName } from '../_util/motion';
|
2015-12-29 11:46:13 +08:00
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
// Align the design since we use `rc-select` in root. This help:
|
|
|
|
// - List search content will show all content
|
|
|
|
// - Hover opacity style
|
|
|
|
// - Search filter match case
|
|
|
|
|
2021-12-21 21:38:11 +08:00
|
|
|
export { BaseOptionType, DefaultOptionType };
|
2021-11-24 17:45:13 +08:00
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
export type FieldNamesType = FieldNames;
|
|
|
|
|
|
|
|
export type FilledFieldNamesType = Required<FieldNamesType>;
|
|
|
|
|
|
|
|
function highlightKeyword(str: string, lowerKeyword: string, prefixCls: string | undefined) {
|
|
|
|
const cells = str
|
|
|
|
.toLowerCase()
|
|
|
|
.split(lowerKeyword)
|
|
|
|
.reduce((list, cur, index) => (index === 0 ? [cur] : [...list, lowerKeyword, cur]), []);
|
|
|
|
const fillCells: React.ReactNode[] = [];
|
|
|
|
let start = 0;
|
|
|
|
|
|
|
|
cells.forEach((cell, index) => {
|
|
|
|
const end = start + cell.length;
|
|
|
|
let originWorld: React.ReactNode = str.slice(start, end);
|
|
|
|
start = end;
|
|
|
|
|
|
|
|
if (index % 2 === 1) {
|
|
|
|
originWorld = (
|
|
|
|
<span className={`${prefixCls}-menu-item-keyword`} key="seperator">
|
|
|
|
{originWorld}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
2016-09-01 11:08:41 +08:00
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
fillCells.push(originWorld);
|
2016-09-01 11:08:41 +08:00
|
|
|
});
|
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
return fillCells;
|
2016-09-01 11:08:41 +08:00
|
|
|
}
|
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
const defaultSearchRender: ShowSearchType['render'] = (inputValue, path, prefixCls, fieldNames) => {
|
|
|
|
const optionList: React.ReactNode[] = [];
|
2018-11-06 12:36:03 +08:00
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
// We do lower here to save perf
|
|
|
|
const lower = inputValue.toLowerCase();
|
2018-05-04 17:27:34 +08:00
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
path.forEach((node, index) => {
|
|
|
|
if (index !== 0) {
|
|
|
|
optionList.push(' / ');
|
2018-12-14 11:40:47 +08:00
|
|
|
}
|
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
let label = (node as any)[fieldNames.label!];
|
|
|
|
const type = typeof label;
|
|
|
|
if (type === 'string' || type === 'number') {
|
|
|
|
label = highlightKeyword(String(label), lower, prefixCls);
|
|
|
|
}
|
2016-10-24 12:04:26 +08:00
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
optionList.push(label);
|
2019-07-08 10:57:34 +08:00
|
|
|
});
|
2021-08-31 15:51:02 +08:00
|
|
|
return optionList;
|
|
|
|
};
|
2019-07-08 10:57:34 +08:00
|
|
|
|
2021-11-24 17:45:13 +08:00
|
|
|
export interface CascaderProps<DataNodeType>
|
|
|
|
extends Omit<RcCascaderProps, 'checkable' | 'options'> {
|
2021-08-31 15:51:02 +08:00
|
|
|
multiple?: boolean;
|
|
|
|
size?: SizeType;
|
|
|
|
bordered?: boolean;
|
2021-11-24 17:45:13 +08:00
|
|
|
|
|
|
|
suffixIcon?: React.ReactNode;
|
|
|
|
options?: DataNodeType[];
|
2020-11-30 11:31:22 +08:00
|
|
|
}
|
|
|
|
|
2021-11-24 17:45:13 +08:00
|
|
|
export interface CascaderRef {
|
2021-08-31 15:51:02 +08:00
|
|
|
focus: () => void;
|
|
|
|
blur: () => void;
|
|
|
|
}
|
|
|
|
|
2021-11-24 17:45:13 +08:00
|
|
|
const Cascader = React.forwardRef((props: CascaderProps<any>, ref: React.Ref<CascaderRef>) => {
|
2021-08-31 15:51:02 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
size: customizeSize,
|
|
|
|
className,
|
|
|
|
multiple,
|
|
|
|
bordered = true,
|
|
|
|
transitionName,
|
|
|
|
choiceTransitionName = '',
|
2021-09-13 15:59:57 +08:00
|
|
|
popupClassName,
|
2021-08-31 15:51:02 +08:00
|
|
|
dropdownClassName,
|
|
|
|
expandIcon,
|
|
|
|
showSearch,
|
|
|
|
allowClear = true,
|
|
|
|
notFoundContent,
|
|
|
|
direction,
|
2021-09-13 15:59:57 +08:00
|
|
|
getPopupContainer,
|
2021-08-31 15:51:02 +08:00
|
|
|
...rest
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const restProps = omit(rest, ['suffixIcon' as any]);
|
|
|
|
|
|
|
|
const {
|
2021-09-13 15:59:57 +08:00
|
|
|
getPopupContainer: getContextPopupContainer,
|
2021-08-31 15:51:02 +08:00
|
|
|
getPrefixCls,
|
|
|
|
renderEmpty,
|
|
|
|
direction: rootDirection,
|
|
|
|
// virtual,
|
|
|
|
// dropdownMatchSelectWidth,
|
|
|
|
} = React.useContext(ConfigContext);
|
|
|
|
|
|
|
|
const mergedDirection = direction || rootDirection;
|
|
|
|
const isRtl = mergedDirection === 'rtl';
|
|
|
|
|
2021-09-13 15:59:57 +08:00
|
|
|
// =================== Warning =====================
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
devWarning(
|
|
|
|
popupClassName === undefined,
|
|
|
|
'Cascader',
|
|
|
|
'`popupClassName` is deprecated. Please use `dropdownClassName` instead.',
|
|
|
|
);
|
2021-11-30 17:26:51 +08:00
|
|
|
|
|
|
|
devWarning(
|
|
|
|
!multiple || !props.displayRender,
|
|
|
|
'Cascader',
|
|
|
|
'`displayRender` not work on `multiple`. Please use `tagRender` instead.',
|
|
|
|
);
|
2021-09-13 15:59:57 +08:00
|
|
|
}
|
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
// =================== No Found ====================
|
|
|
|
const mergedNotFoundContent = notFoundContent || renderEmpty('Cascader');
|
|
|
|
|
|
|
|
// ==================== Prefix =====================
|
|
|
|
const rootPrefixCls = getPrefixCls();
|
|
|
|
const prefixCls = getPrefixCls('select', customizePrefixCls);
|
|
|
|
const cascaderPrefixCls = getPrefixCls('cascader', customizePrefixCls);
|
|
|
|
|
|
|
|
// =================== Dropdown ====================
|
2021-09-13 15:59:57 +08:00
|
|
|
const mergedDropdownClassName = classNames(
|
|
|
|
dropdownClassName || popupClassName,
|
|
|
|
`${cascaderPrefixCls}-dropdown`,
|
|
|
|
{
|
|
|
|
[`${cascaderPrefixCls}-dropdown-rtl`]: mergedDirection === 'rtl',
|
|
|
|
},
|
|
|
|
);
|
2018-12-14 11:40:47 +08:00
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
// ==================== Search =====================
|
|
|
|
const mergedShowSearch = React.useMemo(() => {
|
|
|
|
if (!showSearch) {
|
|
|
|
return showSearch;
|
2019-07-08 10:57:34 +08:00
|
|
|
}
|
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
let searchConfig: ShowSearchType = {
|
|
|
|
render: defaultSearchRender,
|
2015-12-29 11:46:13 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
if (typeof showSearch === 'object') {
|
|
|
|
searchConfig = {
|
|
|
|
...searchConfig,
|
|
|
|
...showSearch,
|
|
|
|
};
|
2020-08-14 17:53:34 +08:00
|
|
|
}
|
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
return searchConfig;
|
|
|
|
}, [showSearch]);
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
// ===================== Size ======================
|
|
|
|
const size = React.useContext(SizeContext);
|
|
|
|
const mergedSize = customizeSize || size;
|
2016-09-01 11:08:41 +08:00
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
// ===================== Icon ======================
|
|
|
|
let mergedExpandIcon = expandIcon;
|
|
|
|
if (!expandIcon) {
|
|
|
|
mergedExpandIcon = isRtl ? <LeftOutlined /> : <RightOutlined />;
|
2015-12-29 21:18:27 +08:00
|
|
|
}
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
const loadingIcon = (
|
|
|
|
<span className={`${prefixCls}-menu-item-loading-icon`}>
|
|
|
|
<RedoOutlined spin />
|
|
|
|
</span>
|
|
|
|
);
|
2017-11-19 01:41:40 +08:00
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
// =================== Multiple ====================
|
|
|
|
const checkable = React.useMemo(
|
|
|
|
() => (multiple ? <span className={`${cascaderPrefixCls}-checkbox-inner`} /> : false),
|
|
|
|
[multiple],
|
|
|
|
);
|
2017-11-19 01:41:40 +08:00
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
// ===================== Icons =====================
|
|
|
|
const { suffixIcon, removeIcon, clearIcon } = getIcons({
|
|
|
|
...props,
|
|
|
|
multiple,
|
|
|
|
prefixCls,
|
|
|
|
});
|
2020-01-02 19:10:16 +08:00
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
// ==================== Render =====================
|
|
|
|
return (
|
|
|
|
<RcCascader
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
className={classNames(
|
|
|
|
!customizePrefixCls && cascaderPrefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-lg`]: mergedSize === 'large',
|
|
|
|
[`${prefixCls}-sm`]: mergedSize === 'small',
|
|
|
|
[`${prefixCls}-rtl`]: isRtl,
|
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
)}
|
|
|
|
{...(restProps as any)}
|
|
|
|
direction={mergedDirection}
|
|
|
|
notFoundContent={mergedNotFoundContent}
|
|
|
|
allowClear={allowClear}
|
|
|
|
showSearch={mergedShowSearch}
|
|
|
|
expandIcon={mergedExpandIcon}
|
|
|
|
inputIcon={suffixIcon}
|
|
|
|
removeIcon={removeIcon}
|
|
|
|
clearIcon={clearIcon}
|
|
|
|
loadingIcon={loadingIcon}
|
|
|
|
checkable={checkable}
|
|
|
|
dropdownClassName={mergedDropdownClassName}
|
|
|
|
dropdownPrefixCls={customizePrefixCls || cascaderPrefixCls}
|
|
|
|
choiceTransitionName={getTransitionName(rootPrefixCls, '', choiceTransitionName)}
|
|
|
|
transitionName={getTransitionName(rootPrefixCls, 'slide-up', transitionName)}
|
2021-09-13 15:59:57 +08:00
|
|
|
getPopupContainer={getPopupContainer || getContextPopupContainer}
|
2021-08-31 15:51:02 +08:00
|
|
|
ref={ref}
|
|
|
|
/>
|
2020-01-03 13:38:16 +08:00
|
|
|
);
|
2021-12-21 21:38:11 +08:00
|
|
|
}) as (<OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType>(
|
|
|
|
props: React.PropsWithChildren<CascaderProps<OptionType>> & { ref?: React.Ref<CascaderRef> },
|
2021-11-24 17:45:13 +08:00
|
|
|
) => React.ReactElement) & {
|
|
|
|
displayName: string;
|
|
|
|
};
|
2018-11-26 12:06:42 +08:00
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
Cascader.displayName = 'Cascader';
|
2018-12-14 11:40:47 +08:00
|
|
|
|
|
|
|
export default Cascader;
|