@@ -29,37 +29,39 @@ const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((prop
2929 } = props ;
3030
3131 const disabled = collapsible === 'disabled' ;
32- const collapsibleHeader = collapsible === 'header' ;
33- const collapsibleIcon = collapsible === 'icon' ;
3432
3533 const ifExtraExist = extra !== null && extra !== undefined && typeof extra !== 'boolean' ;
3634
37- const handleItemClick = ( ) => {
38- onItemClick ?.( panelKey ! ) ;
39- } ;
40-
41- const handleKeyDown = ( e : React . KeyboardEvent ) => {
42- if ( e . key === 'Enter' || e . keyCode === KeyCode . ENTER || e . which === KeyCode . ENTER ) {
43- handleItemClick ( ) ;
44- }
35+ const collapsibleProps = {
36+ onClick : ( ) => {
37+ onItemClick ?.( panelKey ) ;
38+ } ,
39+ onKeyDown : ( e : React . KeyboardEvent ) => {
40+ if ( e . key === 'Enter' || e . keyCode === KeyCode . ENTER || e . which === KeyCode . ENTER ) {
41+ onItemClick ?.( panelKey ) ;
42+ }
43+ } ,
44+ role : accordion ? 'tab' : 'button' ,
45+ [ 'aria-expanded' ] : isActive ,
46+ [ 'aria-disabled' ] : disabled ,
47+ tabIndex : disabled ? - 1 : 0 ,
4548 } ;
4649
4750 // ======================== Icon ========================
48- let iconNode = typeof expandIcon === 'function' ? expandIcon ( props ) : < i className = "arrow" /> ;
49- if ( iconNode ) {
50- iconNode = (
51- < div
52- className = { `${ prefixCls } -expand-icon` }
53- onClick = { [ 'header' , 'icon' ] . includes ( collapsible as string ) ? handleItemClick : undefined }
54- >
55- { iconNode }
56- </ div >
57- ) ;
58- }
51+ const iconNodeInner =
52+ typeof expandIcon === 'function' ? expandIcon ( props ) : < i className = "arrow" /> ;
53+ const iconNode = iconNodeInner && (
54+ < div
55+ className = { `${ prefixCls } -expand-icon` }
56+ { ...( [ 'header' , 'icon' ] . includes ( collapsible ) ? collapsibleProps : { } ) }
57+ >
58+ { iconNodeInner }
59+ </ div >
60+ ) ;
5961
6062 const collapsePanelClassNames = classNames (
63+ `${ prefixCls } -item` ,
6164 {
62- [ `${ prefixCls } -item` ] : true ,
6365 [ `${ prefixCls } -item-active` ] : isActive ,
6466 [ `${ prefixCls } -item-disabled` ] : disabled ,
6567 } ,
@@ -68,37 +70,28 @@ const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((prop
6870
6971 const headerClassName = classNames (
7072 headerClass ,
73+ `${ prefixCls } -header` ,
7174 {
72- [ `${ prefixCls } -header` ] : true ,
73- [ `${ prefixCls } -header-collapsible-only` ] : collapsibleHeader ,
74- [ `${ prefixCls } -icon-collapsible-only` ] : collapsibleIcon ,
75+ [ `${ prefixCls } -collapsible-${ collapsible } ` ] : ! ! collapsible ,
7576 } ,
7677 customizeClassNames . header ,
7778 ) ;
7879
7980 // ======================== HeaderProps ========================
8081 const headerProps : React . HTMLAttributes < HTMLDivElement > = {
8182 className : headerClassName ,
82- 'aria-expanded' : isActive ,
83- 'aria-disabled' : disabled ,
84- onKeyDown : handleKeyDown ,
8583 style : styles . header ,
86- role : accordion ? 'tab' : 'button' ,
84+ ... ( [ 'header' , 'icon' ] . includes ( collapsible ) ? { } : collapsibleProps ) ,
8785 } ;
8886
89- if ( ! collapsibleHeader && ! collapsibleIcon ) {
90- headerProps . onClick = handleItemClick ;
91- headerProps . tabIndex = disabled ? - 1 : 0 ;
92- }
93-
9487 // ======================== Render ========================
9588 return (
9689 < div { ...resetProps } ref = { ref } className = { collapsePanelClassNames } >
9790 < div { ...headerProps } >
9891 { showArrow && iconNode }
9992 < span
10093 className = { `${ prefixCls } -header-text` }
101- onClick = { collapsible === 'header' ? handleItemClick : undefined }
94+ { ... ( collapsible === 'header' ? collapsibleProps : { } ) }
10295 >
10396 { header }
10497 </ span >
0 commit comments