2022-06-22 14:57:09 +08:00
|
|
|
import RightOutlined from '@ant-design/icons/RightOutlined';
|
|
|
|
import classNames from 'classnames';
|
2017-06-21 15:39:08 +08:00
|
|
|
import RcCollapse from 'rc-collapse';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { CSSMotionProps } from 'rc-motion';
|
2022-06-22 14:57:09 +08:00
|
|
|
import * as React from 'react';
|
2019-08-13 14:07:17 +08:00
|
|
|
|
2020-11-30 15:20:42 +08:00
|
|
|
import toArray from 'rc-util/lib/Children/toArray';
|
2021-01-13 21:00:30 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2020-05-26 20:55:39 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-07-19 22:53:38 +08:00
|
|
|
import initCollapseMotion from '../_util/motion';
|
2020-05-14 20:54:49 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2022-05-27 16:07:32 +08:00
|
|
|
import warning from '../_util/warning';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { CollapsibleType } from './CollapsePanel';
|
2023-01-21 18:45:09 +08:00
|
|
|
import type { SizeType } from '../config-provider/SizeContext';
|
|
|
|
import SizeContext from '../config-provider/SizeContext';
|
2022-06-22 14:57:09 +08:00
|
|
|
import CollapsePanel from './CollapsePanel';
|
2017-06-21 15:39:08 +08:00
|
|
|
|
2022-04-25 14:45:26 +08:00
|
|
|
import useStyle from './style';
|
2017-06-21 15:39:08 +08:00
|
|
|
|
2022-05-27 16:07:32 +08:00
|
|
|
/** @deprecated Please use `start` | `end` instead */
|
|
|
|
type ExpandIconPositionLegacy = 'left' | 'right';
|
|
|
|
export type ExpandIconPosition = 'start' | 'end' | ExpandIconPositionLegacy | undefined;
|
2019-05-06 12:04:39 +08:00
|
|
|
|
2017-06-21 15:39:08 +08:00
|
|
|
export interface CollapseProps {
|
2019-07-13 10:31:56 +08:00
|
|
|
activeKey?: Array<string | number> | string | number;
|
|
|
|
defaultActiveKey?: Array<string | number> | string | number;
|
2017-06-21 15:39:08 +08:00
|
|
|
/** 手风琴效果 */
|
|
|
|
accordion?: boolean;
|
2018-08-07 16:40:22 +08:00
|
|
|
destroyInactivePanel?: boolean;
|
2017-12-13 23:23:38 +08:00
|
|
|
onChange?: (key: string | string[]) => void;
|
2017-06-21 15:39:08 +08:00
|
|
|
style?: React.CSSProperties;
|
|
|
|
className?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2017-06-21 15:39:08 +08:00
|
|
|
bordered?: boolean;
|
|
|
|
prefixCls?: string;
|
2019-06-16 20:51:47 +08:00
|
|
|
expandIcon?: (panelProps: PanelProps) => React.ReactNode;
|
2019-05-06 12:04:39 +08:00
|
|
|
expandIconPosition?: ExpandIconPosition;
|
2020-06-28 22:41:59 +08:00
|
|
|
ghost?: boolean;
|
2023-01-21 18:45:09 +08:00
|
|
|
size?: SizeType;
|
2020-11-30 15:20:42 +08:00
|
|
|
collapsible?: CollapsibleType;
|
2022-04-08 22:55:42 +08:00
|
|
|
children?: React.ReactNode;
|
2019-01-23 10:41:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
interface PanelProps {
|
|
|
|
isActive?: boolean;
|
|
|
|
header?: React.ReactNode;
|
|
|
|
className?: string;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
showArrow?: boolean;
|
|
|
|
forceRender?: boolean;
|
2020-11-30 15:20:42 +08:00
|
|
|
/** @deprecated Use `collapsible="disabled"` instead */
|
2019-01-23 10:41:07 +08:00
|
|
|
disabled?: boolean;
|
2019-02-13 19:32:49 +08:00
|
|
|
extra?: React.ReactNode;
|
2020-11-30 15:20:42 +08:00
|
|
|
collapsible?: CollapsibleType;
|
2017-06-21 15:39:08 +08:00
|
|
|
}
|
|
|
|
|
2023-02-01 22:55:41 +08:00
|
|
|
const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>((props, ref) => {
|
2020-05-26 20:55:39 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
2023-01-21 18:45:09 +08:00
|
|
|
const size = React.useContext(SizeContext);
|
|
|
|
|
2022-05-27 16:07:32 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
2023-01-20 11:03:50 +08:00
|
|
|
className,
|
|
|
|
rootClassName,
|
2022-05-27 16:07:32 +08:00
|
|
|
bordered = true,
|
|
|
|
ghost,
|
2023-01-21 18:45:09 +08:00
|
|
|
size: customizeSize,
|
2022-05-27 16:07:32 +08:00
|
|
|
expandIconPosition = 'start',
|
|
|
|
} = props;
|
2023-01-21 18:45:09 +08:00
|
|
|
|
|
|
|
const mergedSize = customizeSize || size || 'middle';
|
2020-05-26 20:55:39 +08:00
|
|
|
const prefixCls = getPrefixCls('collapse', customizePrefixCls);
|
2022-07-19 22:53:38 +08:00
|
|
|
const rootPrefixCls = getPrefixCls();
|
2022-04-25 14:45:26 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2017-06-21 15:39:08 +08:00
|
|
|
|
2022-05-27 16:07:32 +08:00
|
|
|
// Warning if use legacy type `expandIconPosition`
|
|
|
|
warning(
|
|
|
|
expandIconPosition !== 'left' && expandIconPosition !== 'right',
|
|
|
|
'Collapse',
|
|
|
|
'`expandIconPosition` with `left` or `right` is deprecated. Please use `start` or `end` instead.',
|
|
|
|
);
|
|
|
|
|
|
|
|
// Align with logic position
|
|
|
|
const mergedExpandIconPosition = React.useMemo(() => {
|
|
|
|
if (expandIconPosition === 'left') {
|
|
|
|
return 'start';
|
2020-04-21 23:23:37 +08:00
|
|
|
}
|
2022-05-27 16:07:32 +08:00
|
|
|
return expandIconPosition === 'right' ? 'end' : expandIconPosition;
|
|
|
|
}, [expandIconPosition]);
|
2020-04-21 23:23:37 +08:00
|
|
|
|
2020-05-26 20:55:39 +08:00
|
|
|
const renderExpandIcon = (panelProps: PanelProps = {}) => {
|
|
|
|
const { expandIcon } = props;
|
2021-07-26 16:47:55 +08:00
|
|
|
const icon = (
|
|
|
|
expandIcon ? (
|
|
|
|
expandIcon(panelProps)
|
|
|
|
) : (
|
|
|
|
<RightOutlined rotate={panelProps.isActive ? 90 : undefined} />
|
|
|
|
)
|
|
|
|
) as React.ReactNode;
|
2019-10-11 10:23:41 +08:00
|
|
|
|
2022-05-27 23:18:36 +08:00
|
|
|
return cloneElement(icon, () => ({
|
|
|
|
className: classNames((icon as any).props.className, `${prefixCls}-arrow`),
|
|
|
|
}));
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-08-21 20:03:48 +08:00
|
|
|
|
2020-05-26 20:55:39 +08:00
|
|
|
const collapseClassName = classNames(
|
2022-05-27 16:07:32 +08:00
|
|
|
`${prefixCls}-icon-position-${mergedExpandIconPosition}`,
|
2020-05-26 20:55:39 +08:00
|
|
|
{
|
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2020-06-28 22:41:59 +08:00
|
|
|
[`${prefixCls}-ghost`]: !!ghost,
|
2023-01-21 18:45:09 +08:00
|
|
|
[`${prefixCls}-${mergedSize}`]: mergedSize !== 'middle',
|
2020-05-26 20:55:39 +08:00
|
|
|
},
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-04-25 14:45:26 +08:00
|
|
|
hashId,
|
2020-05-26 20:55:39 +08:00
|
|
|
);
|
2020-11-30 15:20:42 +08:00
|
|
|
const openMotion: CSSMotionProps = {
|
2022-07-19 22:53:38 +08:00
|
|
|
...initCollapseMotion(rootPrefixCls),
|
2020-11-30 15:20:42 +08:00
|
|
|
motionAppear: false,
|
|
|
|
leavedClassName: `${prefixCls}-content-hidden`,
|
|
|
|
};
|
|
|
|
|
|
|
|
const getItems = () => {
|
|
|
|
const { children } = props;
|
|
|
|
return toArray(children).map((child: React.ReactElement, index: number) => {
|
|
|
|
if (child.props?.disabled) {
|
|
|
|
const key = child.key || String(index);
|
|
|
|
const { disabled, collapsible } = child.props;
|
2021-01-13 21:00:30 +08:00
|
|
|
const childProps: CollapseProps & { key: React.Key } = {
|
|
|
|
...omit(child.props, ['disabled']),
|
2020-11-30 15:20:42 +08:00
|
|
|
key,
|
|
|
|
collapsible: collapsible ?? (disabled ? 'disabled' : undefined),
|
|
|
|
};
|
|
|
|
return cloneElement(child, childProps);
|
|
|
|
}
|
|
|
|
return child;
|
|
|
|
});
|
|
|
|
};
|
2020-03-19 11:51:38 +08:00
|
|
|
|
2022-04-25 14:45:26 +08:00
|
|
|
return wrapSSR(
|
2020-05-26 20:55:39 +08:00
|
|
|
<RcCollapse
|
2023-02-01 22:55:41 +08:00
|
|
|
ref={ref}
|
2020-11-30 15:20:42 +08:00
|
|
|
openMotion={openMotion}
|
2023-01-20 11:03:50 +08:00
|
|
|
{...omit(props, ['rootClassName'])}
|
2020-11-25 10:50:22 +08:00
|
|
|
expandIcon={renderExpandIcon}
|
2020-05-26 20:55:39 +08:00
|
|
|
prefixCls={prefixCls}
|
|
|
|
className={collapseClassName}
|
2020-11-30 15:20:42 +08:00
|
|
|
>
|
|
|
|
{getItems()}
|
2022-04-25 14:45:26 +08:00
|
|
|
</RcCollapse>,
|
2020-05-26 20:55:39 +08:00
|
|
|
);
|
2023-02-01 22:55:41 +08:00
|
|
|
});
|
2020-05-26 20:55:39 +08:00
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Collapse.displayName = 'Collapse';
|
|
|
|
}
|
|
|
|
|
2023-02-01 22:55:41 +08:00
|
|
|
export default Object.assign(Collapse, { Panel: CollapsePanel });
|