2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2017-06-21 15:39:08 +08:00
|
|
|
import RcCollapse from 'rc-collapse';
|
2020-11-30 15:20:42 +08:00
|
|
|
import { CSSMotionProps } from 'rc-motion';
|
2017-06-21 15:39:08 +08:00
|
|
|
import classNames from 'classnames';
|
2020-03-02 12:09:38 +08:00
|
|
|
import RightOutlined from '@ant-design/icons/RightOutlined';
|
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-11-30 15:20:42 +08:00
|
|
|
import CollapsePanel, { CollapsibleType } from './CollapsePanel';
|
2020-05-26 20:55:39 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2020-11-30 15:20:42 +08:00
|
|
|
import collapseMotion from '../_util/motion';
|
2020-05-14 20:54:49 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2022-04-25 14:45:26 +08:00
|
|
|
import useStyle from './style';
|
2017-06-21 15:39:08 +08:00
|
|
|
|
2020-04-21 23:23:37 +08:00
|
|
|
export type ExpandIconPosition = 'left' | 'right' | 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;
|
|
|
|
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;
|
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
|
|
|
}
|
|
|
|
|
2020-05-26 20:55:39 +08:00
|
|
|
interface CollapseInterface extends React.FC<CollapseProps> {
|
|
|
|
Panel: typeof CollapsePanel;
|
|
|
|
}
|
2017-06-21 15:39:08 +08:00
|
|
|
|
2020-05-26 20:55:39 +08:00
|
|
|
const Collapse: CollapseInterface = props => {
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
2020-11-24 20:06:41 +08:00
|
|
|
const { prefixCls: customizePrefixCls, className = '', bordered = true, ghost } = props;
|
2020-05-26 20:55:39 +08:00
|
|
|
const prefixCls = getPrefixCls('collapse', customizePrefixCls);
|
2022-04-25 14:45:26 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2017-06-21 15:39:08 +08:00
|
|
|
|
2020-05-26 20:55:39 +08:00
|
|
|
const getIconPosition = () => {
|
|
|
|
const { expandIconPosition } = props;
|
2020-04-21 23:23:37 +08:00
|
|
|
if (expandIconPosition !== undefined) {
|
|
|
|
return expandIconPosition;
|
|
|
|
}
|
|
|
|
return direction === 'rtl' ? 'right' : 'left';
|
2020-05-26 20:55:39 +08:00
|
|
|
};
|
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
|
|
|
|
2021-10-14 17:16:38 +08:00
|
|
|
return (
|
|
|
|
// Create additional div here to make arrow align to center of first line
|
|
|
|
<div>
|
|
|
|
{cloneElement(icon, () => ({
|
|
|
|
className: classNames((icon as any).props.className, `${prefixCls}-arrow`),
|
|
|
|
}))}
|
|
|
|
</div>
|
|
|
|
);
|
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 iconPosition = getIconPosition();
|
|
|
|
const collapseClassName = classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
|
|
|
[`${prefixCls}-icon-position-${iconPosition}`]: true,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2020-06-28 22:41:59 +08:00
|
|
|
[`${prefixCls}-ghost`]: !!ghost,
|
2020-05-26 20:55:39 +08:00
|
|
|
},
|
|
|
|
className,
|
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 = {
|
|
|
|
...collapseMotion,
|
|
|
|
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
|
2020-11-30 15:20:42 +08:00
|
|
|
openMotion={openMotion}
|
2020-05-26 20:55:39 +08:00
|
|
|
{...props}
|
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
|
|
|
);
|
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2020-05-26 20:55:39 +08:00
|
|
|
Collapse.Panel = CollapsePanel;
|
|
|
|
|
|
|
|
export default Collapse;
|