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';
|
2017-12-13 23:23:38 +08:00
|
|
|
import CollapsePanel from './CollapsePanel';
|
2018-08-21 20:03:48 +08:00
|
|
|
import Icon from '../icon';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
|
|
|
import animation from '../_util/openAnimation';
|
2017-06-21 15:39:08 +08:00
|
|
|
|
|
|
|
export interface CollapseProps {
|
|
|
|
activeKey?: Array<string> | string;
|
|
|
|
defaultActiveKey?: Array<string>;
|
|
|
|
/** 手风琴效果 */
|
|
|
|
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-01-23 10:41:07 +08:00
|
|
|
expandIcon?: (panelProps: any) => React.ReactNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface PanelProps {
|
|
|
|
isActive?: boolean;
|
|
|
|
header?: React.ReactNode;
|
|
|
|
className?: string;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
showArrow?: boolean;
|
|
|
|
forceRender?: boolean;
|
|
|
|
disabled?: boolean;
|
2017-06-21 15:39:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class Collapse extends React.Component<CollapseProps, any> {
|
2017-12-13 23:23:38 +08:00
|
|
|
static Panel = CollapsePanel;
|
2017-06-21 15:39:08 +08:00
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
bordered: true,
|
2018-12-07 20:02:01 +08:00
|
|
|
openAnimation: { ...animation, appear() {} },
|
2017-06-21 15:39:08 +08:00
|
|
|
};
|
|
|
|
|
2019-01-23 10:41:07 +08:00
|
|
|
renderExpandIcon = (panelProps: PanelProps = {}, prefixCls: string) => {
|
|
|
|
const { expandIcon } = this.props;
|
|
|
|
const icon = expandIcon ? (
|
|
|
|
expandIcon(panelProps)
|
|
|
|
) : (
|
|
|
|
<Icon type="right" rotate={panelProps.isActive ? 90 : undefined} />
|
|
|
|
);
|
|
|
|
return React.isValidElement(icon)
|
|
|
|
? React.cloneElement(icon as any, {
|
|
|
|
className: `${prefixCls}-arrow`,
|
|
|
|
})
|
|
|
|
: icon;
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-08-21 20:03:48 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
renderCollapse = ({ getPrefixCls }: ConfigConsumerProps) => {
|
|
|
|
const { prefixCls: customizePrefixCls, className = '', bordered } = this.props;
|
|
|
|
const prefixCls = getPrefixCls('collapse', customizePrefixCls);
|
2018-12-07 20:02:01 +08:00
|
|
|
const collapseClassName = classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2018-08-21 20:03:48 +08:00
|
|
|
return (
|
|
|
|
<RcCollapse
|
|
|
|
{...this.props}
|
2019-01-23 10:41:07 +08:00
|
|
|
expandIcon={(panelProps: PanelProps) => this.renderExpandIcon(panelProps, prefixCls)}
|
2018-12-05 19:12:18 +08:00
|
|
|
prefixCls={prefixCls}
|
2018-08-21 20:03:48 +08:00
|
|
|
className={collapseClassName}
|
|
|
|
/>
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderCollapse}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2017-06-21 15:39:08 +08:00
|
|
|
}
|