11import classNames from 'classnames' ;
22import toArray from 'rc-util/lib/Children/toArray' ;
33import useMergedState from 'rc-util/lib/hooks/useMergedState' ;
4+ import warning from 'rc-util/lib/warning' ;
45import React from 'react' ;
5- import type { CollapsePanelProps , CollapseProps , CollapsibleType } from './interface' ;
6+ import type { CollapsePanelProps , CollapseProps , CollapsibleType , ItemType } from './interface' ;
67import CollapsePanel from './Panel' ;
78
89function getActiveKeysArray ( activeKey : React . Key | React . Key [ ] ) {
@@ -29,6 +30,7 @@ const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>((props, ref) =>
2930 activeKey : rawActiveKey ,
3031 defaultActiveKey,
3132 onChange,
33+ items,
3234 } = props ;
3335
3436 const collapseClassName = classNames ( prefixCls , className ) ;
@@ -56,6 +58,11 @@ const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>((props, ref) =>
5658 } ) ;
5759
5860 // ======================== Children ========================
61+ warning (
62+ ! rawChildren ,
63+ '`children` will be removed in next major version. Please use `items` instead.' ,
64+ ) ;
65+
5966 const getNewChild = ( child : React . ReactElement < CollapsePanelProps > , index : number ) => {
6067 if ( ! child ) return null ;
6168
@@ -114,7 +121,54 @@ const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>((props, ref) =>
114121 return React . cloneElement ( child , childProps ) ;
115122 } ;
116123
117- const children = toArray ( rawChildren ) . map ( getNewChild ) ;
124+ // eslint-disable-next-line @typescript-eslint/no-shadow
125+ const convertItemsToNodes = ( items : ItemType [ ] ) =>
126+ items . map ( ( item , index ) => {
127+ const {
128+ children,
129+ key : rawKey ,
130+ collapsible : rawCollapsible ,
131+ onItemClick : rawOnItemClick ,
132+ destroyInactivePanel : rawDestroyInactivePanel ,
133+ ...restProps
134+ } = item ;
135+
136+ const key = rawKey || String ( index ) ;
137+ const mergeCollapsible = rawCollapsible ?? collapsible ;
138+ const mergeDestroyInactivePanel = rawDestroyInactivePanel ?? destroyInactivePanel ;
139+
140+ const handleItemClick = ( value : React . Key ) => {
141+ if ( mergeCollapsible === 'disabled' ) return ;
142+ onClickItem ( value ) ;
143+ rawOnItemClick ?.( value ) ;
144+ } ;
145+
146+ let isActive = false ;
147+ if ( accordion ) {
148+ isActive = activeKey [ 0 ] === key ;
149+ } else {
150+ isActive = activeKey . indexOf ( key ) > - 1 ;
151+ }
152+
153+ return (
154+ < CollapsePanel
155+ prefixCls = { prefixCls }
156+ key = { key }
157+ panelKey = { key }
158+ isActive = { isActive }
159+ collapsible = { mergeCollapsible }
160+ onItemClick = { handleItemClick }
161+ destroyInactivePanel = { mergeDestroyInactivePanel }
162+ { ...restProps }
163+ >
164+ { children }
165+ </ CollapsePanel >
166+ ) ;
167+ } ) ;
168+
169+ const children = Array . isArray ( items )
170+ ? convertItemsToNodes ( items )
171+ : toArray ( rawChildren ) . map ( getNewChild ) ;
118172
119173 // ======================== Render ========================
120174 return (
@@ -129,4 +183,9 @@ const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>((props, ref) =>
129183 ) ;
130184} ) ;
131185
132- export default Object . assign ( Collapse , { Panel : CollapsePanel } ) ;
186+ export default Object . assign ( Collapse , {
187+ /**
188+ * @deprecated use `items` instead, will be removed in `v4.0.0`
189+ */
190+ Panel : CollapsePanel ,
191+ } ) ;
0 commit comments