mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-25 03:29:59 +08:00
24697bbb06
* feat: clearIcon * feat: clearIcon * Update components/select/utils/iconUtil.tsx Co-authored-by: afc163 <afc163@gmail.com> Signed-off-by: kiner-tang(文辉) <1127031143@qq.com> * feat: clearIcon * chore: raise size-limit --------- Signed-off-by: kiner-tang(文辉) <1127031143@qq.com> Co-authored-by: afc163 <afc163@gmail.com> Co-authored-by: MadCcc <1075746765@qq.com>
330 lines
10 KiB
TypeScript
330 lines
10 KiB
TypeScript
'use client';
|
|
|
|
import classNames from 'classnames';
|
|
import type { BaseSelectRef } from 'rc-select';
|
|
import type { Placement } from 'rc-select/lib/BaseSelect';
|
|
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';
|
|
import genPurePanel from '../_util/PurePanel';
|
|
import type { SelectCommonPlacement } from '../_util/motion';
|
|
import { getTransitionName } from '../_util/motion';
|
|
import type { InputStatus } from '../_util/statusUtils';
|
|
import { getMergedStatus, getStatusClassNames } from '../_util/statusUtils';
|
|
import warning from '../_util/warning';
|
|
import { ConfigContext } from '../config-provider';
|
|
import DisabledContext from '../config-provider/DisabledContext';
|
|
import type { SizeType } from '../config-provider/SizeContext';
|
|
import DefaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
|
import useSize from '../config-provider/hooks/useSize';
|
|
import { FormItemInputContext } from '../form/context';
|
|
import useSelectStyle from '../select/style';
|
|
import useBuiltinPlacements from '../select/useBuiltinPlacements';
|
|
import useShowArrow from '../select/useShowArrow';
|
|
import getIcons from '../select/utils/iconUtil';
|
|
import { useCompactItemContext } from '../space/Compact';
|
|
import type { AntTreeNodeProps, TreeProps } from '../tree';
|
|
import type { SwitcherIcon } from '../tree/Tree';
|
|
import SwitcherIconCom from '../tree/utils/iconUtil';
|
|
import useStyle from './style';
|
|
|
|
type RawValue = string | number;
|
|
|
|
export interface LabeledValue {
|
|
key?: string;
|
|
value: RawValue;
|
|
label: React.ReactNode;
|
|
}
|
|
|
|
export type SelectValue = RawValue | RawValue[] | LabeledValue | LabeledValue[];
|
|
|
|
export interface TreeSelectProps<
|
|
ValueType = any,
|
|
OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType,
|
|
> extends Omit<
|
|
RcTreeSelectProps<ValueType, OptionType>,
|
|
| 'showTreeIcon'
|
|
| 'treeMotion'
|
|
| 'mode'
|
|
| 'getInputElement'
|
|
| 'backfill'
|
|
| 'treeLine'
|
|
| 'switcherIcon'
|
|
> {
|
|
suffixIcon?: React.ReactNode;
|
|
size?: SizeType;
|
|
disabled?: boolean;
|
|
placement?: SelectCommonPlacement;
|
|
popupClassName?: string;
|
|
/** @deprecated Please use `popupClassName` instead */
|
|
dropdownClassName?: string;
|
|
bordered?: boolean;
|
|
treeLine?: TreeProps['showLine'];
|
|
status?: InputStatus;
|
|
switcherIcon?: SwitcherIcon | RcTreeSelectProps<ValueType, OptionType>['switcherIcon'];
|
|
rootClassName?: string;
|
|
[key: `aria-${string}`]: React.AriaAttributes[keyof React.AriaAttributes];
|
|
/** @deprecated Please use `popupMatchSelectWidth` instead */
|
|
dropdownMatchSelectWidth?: boolean | number;
|
|
popupMatchSelectWidth?: boolean | number;
|
|
/**
|
|
* @deprecated `showArrow` is deprecated which will be removed in next major version. It will be a
|
|
* default behavior, you can hide it by setting `suffixIcon` to null.
|
|
*/
|
|
showArrow?: boolean;
|
|
}
|
|
|
|
const InternalTreeSelect = <
|
|
ValueType = any,
|
|
OptionType extends BaseOptionType | DefaultOptionType = BaseOptionType,
|
|
>(
|
|
{
|
|
prefixCls: customizePrefixCls,
|
|
size: customizeSize,
|
|
disabled: customDisabled,
|
|
bordered = true,
|
|
className,
|
|
rootClassName,
|
|
treeCheckable,
|
|
multiple,
|
|
listHeight = 256,
|
|
listItemHeight = 26,
|
|
placement,
|
|
notFoundContent,
|
|
switcherIcon,
|
|
treeLine,
|
|
getPopupContainer,
|
|
popupClassName,
|
|
dropdownClassName,
|
|
treeIcon = false,
|
|
transitionName,
|
|
choiceTransitionName = '',
|
|
status: customStatus,
|
|
treeExpandAction,
|
|
builtinPlacements,
|
|
dropdownMatchSelectWidth,
|
|
popupMatchSelectWidth,
|
|
allowClear,
|
|
...props
|
|
}: TreeSelectProps<ValueType, OptionType>,
|
|
ref: React.Ref<BaseSelectRef>,
|
|
) => {
|
|
const {
|
|
getPopupContainer: getContextPopupContainer,
|
|
getPrefixCls,
|
|
renderEmpty,
|
|
direction,
|
|
virtual,
|
|
popupMatchSelectWidth: contextPopupMatchSelectWidth,
|
|
popupOverflow,
|
|
} = React.useContext(ConfigContext);
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
warning(
|
|
multiple !== false || !treeCheckable,
|
|
'TreeSelect',
|
|
'`multiple` will always be `true` when `treeCheckable` is true',
|
|
);
|
|
|
|
warning(
|
|
!dropdownClassName,
|
|
'TreeSelect',
|
|
'`dropdownClassName` is deprecated. Please use `popupClassName` instead.',
|
|
);
|
|
|
|
warning(
|
|
dropdownMatchSelectWidth === undefined,
|
|
'Select',
|
|
'`dropdownMatchSelectWidth` is deprecated. Please use `popupMatchSelectWidth` instead.',
|
|
);
|
|
|
|
warning(
|
|
!('showArrow' in props),
|
|
'TreeSelect',
|
|
'`showArrow` is deprecated which will be removed in next major version. It will be a default behavior, you can hide it by setting `suffixIcon` to null.',
|
|
);
|
|
}
|
|
|
|
const rootPrefixCls = getPrefixCls();
|
|
const prefixCls = getPrefixCls('select', customizePrefixCls);
|
|
const treePrefixCls = getPrefixCls('select-tree', customizePrefixCls);
|
|
const treeSelectPrefixCls = getPrefixCls('tree-select', customizePrefixCls);
|
|
const { compactSize, compactItemClassnames } = useCompactItemContext(prefixCls, direction);
|
|
|
|
const [wrapSelectSSR, hashId] = useSelectStyle(prefixCls);
|
|
const [wrapTreeSelectSSR] = useStyle(treeSelectPrefixCls, treePrefixCls);
|
|
|
|
const mergedDropdownClassName = classNames(
|
|
popupClassName || dropdownClassName,
|
|
`${treeSelectPrefixCls}-dropdown`,
|
|
{
|
|
[`${treeSelectPrefixCls}-dropdown-rtl`]: direction === 'rtl',
|
|
},
|
|
rootClassName,
|
|
hashId,
|
|
);
|
|
|
|
const isMultiple = !!(treeCheckable || multiple);
|
|
const showSuffixIcon = useShowArrow(props.suffixIcon, props.showArrow);
|
|
|
|
const mergedPopupMatchSelectWidth =
|
|
popupMatchSelectWidth ?? dropdownMatchSelectWidth ?? contextPopupMatchSelectWidth;
|
|
|
|
// ===================== Form =====================
|
|
const {
|
|
status: contextStatus,
|
|
hasFeedback,
|
|
isFormItemInput,
|
|
feedbackIcon,
|
|
} = React.useContext(FormItemInputContext);
|
|
const mergedStatus = getMergedStatus(contextStatus, customStatus);
|
|
|
|
// ===================== Icons =====================
|
|
const { suffixIcon, removeIcon, clearIcon } = getIcons({
|
|
...props,
|
|
multiple: isMultiple,
|
|
showSuffixIcon,
|
|
hasFeedback,
|
|
feedbackIcon,
|
|
prefixCls,
|
|
componentName: 'TreeSelect',
|
|
});
|
|
|
|
const mergedAllowClear = allowClear === true ? { clearIcon } : allowClear;
|
|
|
|
// ===================== Empty =====================
|
|
let mergedNotFound: React.ReactNode;
|
|
if (notFoundContent !== undefined) {
|
|
mergedNotFound = notFoundContent;
|
|
} else {
|
|
mergedNotFound = renderEmpty?.('Select') || <DefaultRenderEmpty componentName="Select" />;
|
|
}
|
|
|
|
// ==================== Render =====================
|
|
const selectProps = omit(props as typeof props & { itemIcon: any; switcherIcon: any }, [
|
|
'suffixIcon',
|
|
'itemIcon',
|
|
'removeIcon',
|
|
'clearIcon',
|
|
'switcherIcon',
|
|
]);
|
|
|
|
// ===================== Placement =====================
|
|
const memoizedPlacement = React.useMemo<Placement>(() => {
|
|
if (placement !== undefined) {
|
|
return placement;
|
|
}
|
|
return direction === 'rtl' ? 'bottomRight' : 'bottomLeft';
|
|
}, [placement, direction]);
|
|
|
|
const mergedBuiltinPlacements = useBuiltinPlacements(builtinPlacements, popupOverflow);
|
|
|
|
const mergedSize = useSize((ctx) => customizeSize ?? compactSize ?? ctx);
|
|
|
|
// ===================== Disabled =====================
|
|
const disabled = React.useContext(DisabledContext);
|
|
const mergedDisabled = customDisabled ?? disabled;
|
|
|
|
const mergedClassName = classNames(
|
|
!customizePrefixCls && treeSelectPrefixCls,
|
|
{
|
|
[`${prefixCls}-lg`]: mergedSize === 'large',
|
|
[`${prefixCls}-sm`]: mergedSize === 'small',
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
[`${prefixCls}-borderless`]: !bordered,
|
|
[`${prefixCls}-in-form-item`]: isFormItemInput,
|
|
},
|
|
getStatusClassNames(prefixCls, mergedStatus, hasFeedback),
|
|
compactItemClassnames,
|
|
className,
|
|
rootClassName,
|
|
hashId,
|
|
);
|
|
|
|
const renderSwitcherIcon = (nodeProps: AntTreeNodeProps) => (
|
|
<SwitcherIconCom
|
|
prefixCls={treePrefixCls}
|
|
switcherIcon={switcherIcon}
|
|
treeNodeProps={nodeProps}
|
|
showLine={treeLine}
|
|
/>
|
|
);
|
|
|
|
const returnNode = (
|
|
<RcTreeSelect
|
|
virtual={virtual}
|
|
disabled={mergedDisabled}
|
|
{...selectProps}
|
|
dropdownMatchSelectWidth={mergedPopupMatchSelectWidth}
|
|
builtinPlacements={mergedBuiltinPlacements}
|
|
ref={ref}
|
|
prefixCls={prefixCls}
|
|
className={mergedClassName}
|
|
listHeight={listHeight}
|
|
listItemHeight={listItemHeight}
|
|
treeCheckable={
|
|
treeCheckable ? <span className={`${prefixCls}-tree-checkbox-inner`} /> : treeCheckable
|
|
}
|
|
treeLine={!!treeLine}
|
|
suffixIcon={suffixIcon}
|
|
multiple={isMultiple}
|
|
placement={memoizedPlacement}
|
|
removeIcon={removeIcon}
|
|
allowClear={mergedAllowClear}
|
|
switcherIcon={renderSwitcherIcon}
|
|
showTreeIcon={treeIcon as any}
|
|
notFoundContent={mergedNotFound}
|
|
getPopupContainer={getPopupContainer || getContextPopupContainer}
|
|
treeMotion={null}
|
|
dropdownClassName={mergedDropdownClassName}
|
|
choiceTransitionName={getTransitionName(rootPrefixCls, '', choiceTransitionName)}
|
|
transitionName={getTransitionName(rootPrefixCls, 'slide-up', transitionName)}
|
|
treeExpandAction={treeExpandAction}
|
|
/>
|
|
);
|
|
|
|
return wrapSelectSSR(wrapTreeSelectSSR(returnNode));
|
|
};
|
|
|
|
const TreeSelectRef = React.forwardRef(InternalTreeSelect) as <
|
|
ValueType = any,
|
|
OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType,
|
|
>(
|
|
props: React.PropsWithChildren<TreeSelectProps<ValueType, OptionType>> & {
|
|
ref?: React.Ref<BaseSelectRef>;
|
|
},
|
|
) => React.ReactElement;
|
|
|
|
type InternalTreeSelectType = typeof TreeSelectRef;
|
|
|
|
type CompoundedComponent = InternalTreeSelectType & {
|
|
displayName?: string;
|
|
TreeNode: typeof TreeNode;
|
|
SHOW_ALL: typeof SHOW_ALL;
|
|
SHOW_PARENT: typeof SHOW_PARENT;
|
|
SHOW_CHILD: typeof SHOW_CHILD;
|
|
_InternalPanelDoNotUseOrYouWillBeFired: typeof PurePanel;
|
|
};
|
|
|
|
const TreeSelect = TreeSelectRef as CompoundedComponent;
|
|
|
|
// We don't care debug panel
|
|
/* istanbul ignore next */
|
|
const PurePanel = genPurePanel(TreeSelect);
|
|
|
|
TreeSelect.TreeNode = TreeNode;
|
|
TreeSelect.SHOW_ALL = SHOW_ALL;
|
|
TreeSelect.SHOW_PARENT = SHOW_PARENT;
|
|
TreeSelect.SHOW_CHILD = SHOW_CHILD;
|
|
TreeSelect._InternalPanelDoNotUseOrYouWillBeFired = PurePanel;
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
TreeSelect.displayName = 'TreeSelect';
|
|
}
|
|
|
|
export { TreeNode };
|
|
|
|
export default TreeSelect;
|