2022-06-21 10:24:52 +08:00
|
|
|
import LeftOutlined from '@ant-design/icons/LeftOutlined';
|
|
|
|
import LoadingOutlined from '@ant-design/icons/LoadingOutlined';
|
|
|
|
import RightOutlined from '@ant-design/icons/RightOutlined';
|
2015-12-29 18:31:48 +08:00
|
|
|
import classNames from 'classnames';
|
2021-12-21 21:38:11 +08:00
|
|
|
import type {
|
|
|
|
BaseOptionType,
|
|
|
|
DefaultOptionType,
|
2022-06-21 10:24:52 +08:00
|
|
|
FieldNames,
|
|
|
|
MultipleCascaderProps as RcMultipleCascaderProps,
|
|
|
|
ShowSearchType,
|
|
|
|
SingleCascaderProps as RcSingleCascaderProps,
|
2021-12-21 21:38:11 +08:00
|
|
|
} from 'rc-cascader';
|
2022-06-21 10:24:52 +08:00
|
|
|
import RcCascader from 'rc-cascader';
|
2021-01-13 21:00:30 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2022-06-21 10:24:52 +08:00
|
|
|
import * as React from 'react';
|
2022-02-17 15:08:13 +08:00
|
|
|
import { useContext } from 'react';
|
2021-08-31 15:51:02 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-05-16 16:34:42 +08:00
|
|
|
import defaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
2022-06-21 10:24:52 +08:00
|
|
|
import DisabledContext from '../config-provider/DisabledContext';
|
2021-08-31 15:51:02 +08:00
|
|
|
import type { SizeType } from '../config-provider/SizeContext';
|
|
|
|
import SizeContext from '../config-provider/SizeContext';
|
2022-10-18 16:23:10 +08:00
|
|
|
import { useCompactItemContext } from '../space/Compact';
|
|
|
|
|
2022-06-21 10:24:52 +08:00
|
|
|
import { FormItemInputContext } from '../form/context';
|
2021-08-31 15:51:02 +08:00
|
|
|
import getIcons from '../select/utils/iconUtil';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { SelectCommonPlacement } from '../_util/motion';
|
2022-06-21 10:24:52 +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-06-21 10:24:52 +08:00
|
|
|
import warning from '../_util/warning';
|
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>;
|
|
|
|
|
2022-03-26 00:17:34 +08:00
|
|
|
const { SHOW_CHILD, SHOW_PARENT } = RcCascader;
|
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
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 = (
|
2022-01-11 13:18:17 +08:00
|
|
|
// eslint-disable-next-line react/no-array-index-key
|
|
|
|
<span className={`${prefixCls}-menu-item-keyword`} key={`seperator-${index}`}>
|
2021-08-31 15:51:02 +08:00
|
|
|
{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
|
|
|
|
2022-02-12 21:56:55 +08:00
|
|
|
type SingleCascaderProps = Omit<RcSingleCascaderProps, 'checkable' | 'options'> & {
|
|
|
|
multiple?: false;
|
|
|
|
};
|
|
|
|
type MultipleCascaderProps = Omit<RcMultipleCascaderProps, 'checkable' | 'options'> & {
|
|
|
|
multiple: true;
|
|
|
|
};
|
|
|
|
|
|
|
|
type UnionCascaderProps = SingleCascaderProps | MultipleCascaderProps;
|
|
|
|
|
2023-02-28 11:03:12 +08:00
|
|
|
export type CascaderProps<DataNodeType = any> = UnionCascaderProps & {
|
2021-08-31 15:51:02 +08:00
|
|
|
multiple?: boolean;
|
|
|
|
size?: SizeType;
|
2022-04-29 20:48:10 +08:00
|
|
|
disabled?: boolean;
|
2021-08-31 15:51:02 +08:00
|
|
|
bordered?: boolean;
|
2022-01-20 16:54:47 +08:00
|
|
|
placement?: SelectCommonPlacement;
|
2021-11-24 17:45:13 +08:00
|
|
|
suffixIcon?: React.ReactNode;
|
|
|
|
options?: DataNodeType[];
|
2022-02-17 15:08:13 +08:00
|
|
|
status?: InputStatus;
|
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;
|
2022-02-17 15:08:13 +08:00
|
|
|
};
|
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;
|
|
|
|
}
|
|
|
|
|
2023-02-28 11:03:12 +08:00
|
|
|
const Cascader = React.forwardRef((props: CascaderProps, ref: React.Ref<CascaderRef>) => {
|
2021-08-31 15:51:02 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
size: customizeSize,
|
2022-04-29 20:48:10 +08:00
|
|
|
disabled: customDisabled,
|
2021-08-31 15:51:02 +08:00
|
|
|
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,
|
2022-01-20 16:54:47 +08:00
|
|
|
placement,
|
2021-08-31 15:51:02 +08:00
|
|
|
showSearch,
|
|
|
|
allowClear = true,
|
|
|
|
notFoundContent,
|
|
|
|
direction,
|
2021-09-13 15:59:57 +08:00
|
|
|
getPopupContainer,
|
2022-02-17 15:08:13 +08:00
|
|
|
status: customStatus,
|
|
|
|
showArrow,
|
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,
|
2022-02-17 15:08:13 +08:00
|
|
|
} = useContext(ConfigContext);
|
2021-08-31 15:51:02 +08:00
|
|
|
|
|
|
|
const mergedDirection = direction || rootDirection;
|
|
|
|
const isRtl = mergedDirection === 'rtl';
|
|
|
|
|
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 15:08:13 +08:00
|
|
|
const mergedStatus = getMergedStatus(contextStatus, customStatus);
|
|
|
|
|
2021-09-13 15:59:57 +08:00
|
|
|
// =================== Warning =====================
|
2022-05-10 15:43:29 +08:00
|
|
|
warning(
|
2022-08-05 11:21:07 +08:00
|
|
|
!dropdownClassName,
|
2022-05-10 15:43:29 +08:00
|
|
|
'Cascader',
|
2022-08-05 11:21:07 +08:00
|
|
|
'`dropdownClassName` is deprecated which will be removed in next major version. Please use `popupClassName` instead.',
|
2022-05-10 15:43:29 +08:00
|
|
|
);
|
|
|
|
|
2021-08-31 15:51:02 +08:00
|
|
|
// =================== No Found ====================
|
2022-05-16 16:34:42 +08:00
|
|
|
const mergedNotFoundContent = notFoundContent || (renderEmpty || defaultRenderEmpty)('Cascader');
|
2021-08-31 15:51:02 +08:00
|
|
|
|
|
|
|
// ==================== Prefix =====================
|
|
|
|
const rootPrefixCls = getPrefixCls();
|
|
|
|
const prefixCls = getPrefixCls('select', customizePrefixCls);
|
|
|
|
const cascaderPrefixCls = getPrefixCls('cascader', customizePrefixCls);
|
2022-10-18 16:23:10 +08:00
|
|
|
const { compactSize, compactItemClassnames } = useCompactItemContext(prefixCls, direction);
|
2021-08-31 15:51:02 +08:00
|
|
|
// =================== Dropdown ====================
|
2021-09-13 15:59:57 +08:00
|
|
|
const mergedDropdownClassName = classNames(
|
2022-08-05 11:21:07 +08:00
|
|
|
popupClassName || dropdownClassName,
|
2021-09-13 15:59:57 +08:00
|
|
|
`${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);
|
2022-10-18 16:23:10 +08:00
|
|
|
const mergedSize = compactSize || customizeSize || size;
|
2016-09-01 11:08:41 +08:00
|
|
|
|
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
|
|
|
|
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`}>
|
2022-01-21 10:40:34 +08:00
|
|
|
<LoadingOutlined spin />
|
2021-08-31 15:51:02 +08:00
|
|
|
</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 =====================
|
2022-02-17 15:08:13 +08:00
|
|
|
const mergedShowArrow = showArrow !== undefined ? showArrow : props.loading || !multiple;
|
2021-08-31 15:51:02 +08:00
|
|
|
const { suffixIcon, removeIcon, clearIcon } = getIcons({
|
|
|
|
...props,
|
2022-02-17 15:08:13 +08:00
|
|
|
hasFeedback,
|
2022-03-25 17:48:12 +08:00
|
|
|
feedbackIcon,
|
2022-02-17 15:08:13 +08:00
|
|
|
showArrow: mergedShowArrow,
|
2021-08-31 15:51:02 +08:00
|
|
|
multiple,
|
|
|
|
prefixCls,
|
|
|
|
});
|
2020-01-02 19:10:16 +08:00
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
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,
|
2022-03-24 21:54:20 +08:00
|
|
|
[`${prefixCls}-in-form-item`]: isFormItemInput,
|
2021-08-31 15:51:02 +08:00
|
|
|
},
|
2022-02-17 15:08:13 +08:00
|
|
|
getStatusClassNames(prefixCls, mergedStatus, hasFeedback),
|
2022-10-18 16:23:10 +08:00
|
|
|
compactItemClassnames,
|
2021-08-31 15:51:02 +08:00
|
|
|
className,
|
|
|
|
)}
|
2022-04-29 20:48:10 +08:00
|
|
|
disabled={mergedDisabled}
|
2021-08-31 15:51:02 +08:00
|
|
|
{...(restProps as any)}
|
|
|
|
direction={mergedDirection}
|
2022-01-20 16:54:47 +08:00
|
|
|
placement={getPlacement()}
|
2021-08-31 15:51:02 +08:00
|
|
|
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)}
|
2022-01-20 16:54:47 +08:00
|
|
|
transitionName={getTransitionName(
|
|
|
|
rootPrefixCls,
|
|
|
|
getTransitionDirection(placement),
|
|
|
|
transitionName,
|
|
|
|
)}
|
2021-09-13 15:59:57 +08:00
|
|
|
getPopupContainer={getPopupContainer || getContextPopupContainer}
|
2021-08-31 15:51:02 +08:00
|
|
|
ref={ref}
|
2022-02-17 15:08:13 +08:00
|
|
|
showArrow={hasFeedback || showArrow}
|
2021-08-31 15:51:02 +08:00
|
|
|
/>
|
2020-01-03 13:38:16 +08:00
|
|
|
);
|
2022-03-26 00:17:34 +08:00
|
|
|
}) as unknown as (<OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType>(
|
2021-12-21 21:38:11 +08:00
|
|
|
props: React.PropsWithChildren<CascaderProps<OptionType>> & { ref?: React.Ref<CascaderRef> },
|
2021-11-24 17:45:13 +08:00
|
|
|
) => React.ReactElement) & {
|
|
|
|
displayName: string;
|
2022-03-26 00:17:34 +08:00
|
|
|
SHOW_PARENT: typeof SHOW_PARENT;
|
|
|
|
SHOW_CHILD: typeof SHOW_CHILD;
|
2021-11-24 17:45:13 +08:00
|
|
|
};
|
2022-06-21 10:24:52 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Cascader.displayName = 'Cascader';
|
|
|
|
}
|
2022-03-26 00:17:34 +08:00
|
|
|
Cascader.SHOW_PARENT = SHOW_PARENT;
|
|
|
|
Cascader.SHOW_CHILD = SHOW_CHILD;
|
2018-12-14 11:40:47 +08:00
|
|
|
|
|
|
|
export default Cascader;
|