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';
|
|
|
|
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
|
|
|
|
2017-12-13 23:23:38 +08:00
|
|
|
import CollapsePanel from './CollapsePanel';
|
2020-05-26 20:55:39 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2018-12-05 19:12:18 +08:00
|
|
|
import animation from '../_util/openAnimation';
|
2020-05-14 20:54:49 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
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-05 13:42:46 +08:00
|
|
|
ghost?: boolean;
|
2019-01-23 10:41:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
interface PanelProps {
|
|
|
|
isActive?: boolean;
|
|
|
|
header?: React.ReactNode;
|
|
|
|
className?: string;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
showArrow?: boolean;
|
|
|
|
forceRender?: boolean;
|
|
|
|
disabled?: boolean;
|
2019-02-13 19:32:49 +08:00
|
|
|
extra?: React.ReactNode;
|
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-06-05 13:42:46 +08:00
|
|
|
const { prefixCls: customizePrefixCls, className = '', bordered, ghost } = props;
|
2020-05-26 20:55:39 +08:00
|
|
|
const prefixCls = getPrefixCls('collapse', customizePrefixCls);
|
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;
|
2019-10-11 10:23:41 +08:00
|
|
|
const icon = (expandIcon ? (
|
2019-01-23 10:41:07 +08:00
|
|
|
expandIcon(panelProps)
|
|
|
|
) : (
|
2019-11-28 12:34:33 +08:00
|
|
|
<RightOutlined rotate={panelProps.isActive ? 90 : undefined} />
|
2019-10-11 10:23:41 +08:00
|
|
|
)) as React.ReactNode;
|
|
|
|
|
2020-05-14 20:54:49 +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 iconPosition = getIconPosition();
|
|
|
|
const collapseClassName = classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
|
|
|
[`${prefixCls}-icon-position-${iconPosition}`]: true,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2020-06-05 13:42:46 +08:00
|
|
|
[`${prefixCls}-ghost`]: !!ghost,
|
2020-05-26 20:55:39 +08:00
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
|
|
|
const openAnimation = { ...animation, appear() {} };
|
2020-03-19 11:51:38 +08:00
|
|
|
|
2020-05-26 20:55:39 +08:00
|
|
|
return (
|
|
|
|
<RcCollapse
|
|
|
|
openAnimation={openAnimation}
|
|
|
|
{...props}
|
|
|
|
expandIcon={(panelProps: PanelProps) => renderExpandIcon(panelProps)}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
className={collapseClassName}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2020-05-26 20:55:39 +08:00
|
|
|
Collapse.Panel = CollapsePanel;
|
|
|
|
|
|
|
|
Collapse.defaultProps = {
|
|
|
|
bordered: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Collapse;
|