From 10ee477b879c6e7b1cd87eb8ae7e4340a0b4953d Mon Sep 17 00:00:00 2001 From: Bertho Date: Thu, 10 Jan 2019 12:19:37 +0100 Subject: [PATCH] reusable functions --- src/Collapse.jsx | 64 +++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/src/Collapse.jsx b/src/Collapse.jsx index 9dd2c83..0f45bfd 100644 --- a/src/Collapse.jsx +++ b/src/Collapse.jsx @@ -60,40 +60,44 @@ class Collapse extends Component { this.setActiveKey(activeKey); } - getItems() { + getNewChild(child, index) { + if (!child) return null; + const activeKey = this.state.activeKey; - const { prefixCls, accordion, destroyInactivePanel, expandIcon, children } = this.props; - const newChildren = []; + const { prefixCls, accordion, destroyInactivePanel, expandIcon } = this.props; + // 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 = { + key, + header, + headerClass, + isActive, + prefixCls, + destroyInactivePanel, + openAnimation: this.state.openAnimation, + accordion, + children: child.props.children, + onItemClick: disabled ? null : this.onClickItem.bind(this, key), + expandIcon, + }; + + return React.cloneElement(child, props); + } + + getItems() { + const { children } = this.props; const childList = isFragment(children) ? children.props.children : children; - Children.forEach(childList, (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 newChildren = Children.map(childList, this.getNewChild.bind(this)); - const props = { - key, - header, - headerClass, - isActive, - prefixCls, - destroyInactivePanel, - openAnimation: this.state.openAnimation, - accordion, - children: child.props.children, - onItemClick: disabled ? null : () => this.onClickItem(key), - expandIcon, - }; - - newChildren.push(React.cloneElement(child, props)); - }); // ref: https://github.com/ant-design/ant-design/issues/13884 if (isFragment(children)) { return (