官方文档 https://ant.design/components/collapse-cn/
antd组件中有些使用了React 底层基础组件(查看具体列表点这里),collapse就是这种类型的组件
antd中collapse主要源码及组成结构如下,其中红色标注的Rc开头的组件是React底层基础组件
export default class Collapse extends React.Component<CollapseProps, any> { static Panel = CollapsePanel; static defaultProps = { prefixCls: ‘ant-collapse‘, bordered: true, openAnimation: { ...animation, appear() { } }, }; renderExpandIcon = () => { return ( <Icon type="right" className={`arrow`} /> ); } render() { const { prefixCls, className = ‘‘, bordered } = this.props; const collapseClassName = classNames({ [`${prefixCls}-borderless`]: !bordered, }, className); return ( <RcCollapse {...this.props} className={collapseClassName} expandIcon={this.renderExpandIcon} /> ); } }
export default class CollapsePanel extends React.Component<CollapsePanelProps, {}> { render() { const { prefixCls, className = ‘‘, showArrow = true } = this.props; const collapsePanelClassName = classNames({ [`${prefixCls}-no-arrow`]: !showArrow, }, className); return <RcCollapse.Panel {...this.props} className={collapsePanelClassName} />; } }
由上述Collapse源码不难看出,折叠面板组件的实现逻辑主要在RcCollapse中,下面是核心代码、组件内部属性结构及方法调用关系图
class Collapse extends Component { constructor(props) { super(props); ……this.state = { …… }; } componentWillReceiveProps(nextProps) {
…… } onClickItem(key) { …… } getItems() { const activeKey = this.state.activeKey; const { prefixCls, accordion, destroyInactivePanel, expandIcon } = this.props; const newChildren = []; Children.forEach(this.props.children, (child, index) => { if (!child) return; // If there is no key provide, use the panel order as default key const key = child.key || String(index); const { header, headerClass, disabled } = child.props; let isActive = false; if (accordion) { isActive = activeKey[0] === key; } else { isActive = activeKey.indexOf(key) > -1; } const props = { …… openAnimation: this.state.openAnimation, accordion, children: child.props.children, onItemClick: disabled ? null : () => this.onClickItem(key), expandIcon, }; newChildren.push(React.cloneElement(child, props)); }); return newChildren; } setActiveKey(activeKey) { if (!(‘activeKey‘ in this.props)) { this.setState({ activeKey }); } this.props.onChange(this.props.accordion ? activeKey[0] : activeKey); } render() { const { prefixCls, className, style, accordion } = this.props; const collapseClassName = classNames({ [prefixCls]: true, [className]: !!className, }); return ( <div className={collapseClassName} style={style} role={accordion ? ‘tablist‘ : null}> {this.getItems()} </div> ); } }
class CollapsePanel extends Component { handleItemClick = () => { if (this.props.onItemClick) { this.props.onItemClick(); } } handleKeyPress = (e) => { if (e.key === ‘Enter‘ || e.keyCode === 13 || e.which === 13) { this.handleItemClick(); } } render() { const { …… } = this.props; const headerCls = classNames(`${prefixCls}-header`, { [headerClass]: headerClass, }); const itemCls = classNames({ [`${prefixCls}-item`]: true, [`${prefixCls}-item-active`]: isActive, [`${prefixCls}-item-disabled`]: disabled, }, className); let icon = null; if (showArrow && typeof expandIcon === ‘function‘) { icon = React.createElement(expandIcon, { ...this.props }); } return ( <div className={itemCls} style={style} id={id}> <div className={headerCls} onClick={this.handleItemClick} role={accordion ? ‘tab‘ : ‘button‘} tabIndex={disabled ? -1 : 0} aria-expanded={`${isActive}`} onKeyPress={this.handleKeyPress} > {showArrow && (icon || <i className="arrow" />)} {header} </div> <Animate showProp="isActive" exclusive component="" animation={this.props.openAnimation} > <PanelContent prefixCls={prefixCls} isActive={isActive} destroyInactivePanel={destroyInactivePanel} forceRender={forceRender} role={accordion ? ‘tabpanel‘ : null} > {children} </PanelContent> </Animate> </div> ); } }
原文:https://www.cnblogs.com/zs-note/p/11322468.html