@@ -7,36 +7,45 @@ import './style.scss';
77
88export declare type SizeType = 'small' | 'middle' | undefined ;
99
10+ function isControlled ( props : IBlockHeaderProps ) {
11+ return props . expand !== undefined ;
12+ }
13+
1014export interface IBlockHeaderProps {
11- // 标题
15+ /** 标题 */
1216 title : string ;
13- // 标题前的图标,默认是一个色块
17+ /** 标题前的图标,默认是一个色块 */
1418 beforeTitle ?: ReactNode ;
15- // 标题后的提示图标或文案
19+ /** 标题后的提示图标或文案 */
1620 afterTitle ?: ReactNode ;
17- // 默认展示为问号的tooltip
21+ /** 默认展示为问号的tooltip */
1822 tooltip ?: ReactNode ;
19- // 后缀自定义内容块
23+ /** 后缀自定义内容块 */
2024 addonAfter ?: ReactNode ;
2125 /**
2226 * 小标题 font-size: 12px; line-height: 32px
2327 * 中标题 font-size: 14px; line-height: 40px
2428 * 默认 中标题
2529 */
2630 size ?: SizeType ;
31+ /** 是否展示 Bottom,默认 false,Bottom 值 16px */
2732 hasBottom ?: boolean ;
33+ /** 自定义 Bottom 值 */
2834 spaceBottom ?: number ;
29- // 标题一行的样式类名
35+ /** 标题一行的样式类名 */
3036 titleRowClassName ?: string ;
31- // 标题的样式类名
37+ /** 标题的样式类名 */
3238 titleClassName ?: string ;
33- // 是否显示背景, 默认 true
39+ /** 是否显示背景, 默认 true */
3440 showBackground ?: boolean ;
35- // 是否默认展开内容, 默认 true
41+ /** 当前展开状态 */
42+ expand ?: boolean ;
43+ /** 是否默认展开内容, 默认 true */
3644 defaultExpand ?: boolean ;
37- // 展开/收起时的回调
38- onChange ?: ( expand : boolean ) => void ;
45+ /** 展开/收起的内容 */
3946 children ?: ReactNode ;
47+ /** 展开/收起时的回调 */
48+ onExpand ?: ( expand : boolean ) => void ;
4049}
4150const BlockHeader : React . FC < IBlockHeaderProps > = function ( props ) {
4251 const prefixCls = 'dtc-block-header' ;
@@ -52,12 +61,15 @@ const BlockHeader: React.FC<IBlockHeaderProps> = function (props) {
5261 showBackground = true ,
5362 defaultExpand = true ,
5463 addonAfter,
64+ expand,
5565 children = '' ,
5666 beforeTitle = < div className = { `${ prefixCls } __beforeTitle-${ size } ` } > </ div > ,
57- onChange ,
67+ onExpand ,
5868 } = props ;
5969
60- const [ expand , setExpand ] = useState ( defaultExpand ) ;
70+ const [ internalExpand , setInternalExpand ] = useState ( defaultExpand ) ;
71+
72+ const currentExpand = isControlled ( props ) ? expand : internalExpand ;
6173
6274 const preTitleRowCls = `${ prefixCls } -title-row` ;
6375
@@ -73,8 +85,8 @@ const BlockHeader: React.FC<IBlockHeaderProps> = function (props) {
7385
7486 const handleExpand = ( expand : boolean ) => {
7587 if ( ! children ) return ;
76- setExpand ( expand ) ;
77- onChange ?.( expand ) ;
88+ ! isControlled ( props ) && setInternalExpand ( expand ) ;
89+ onExpand ?.( expand ) ;
7890 } ;
7991
8092 return (
@@ -89,7 +101,7 @@ const BlockHeader: React.FC<IBlockHeaderProps> = function (props) {
89101 [ `${ preTitleRowCls } -pointer` ] : children ,
90102 }
91103 ) }
92- onClick = { ( ) => handleExpand ( ! expand ) }
104+ onClick = { ( ) => handleExpand ( ! currentExpand ) }
93105 >
94106 < div className = { `${ prefixCls } -title-box` } >
95107 { beforeTitle ? (
@@ -103,13 +115,13 @@ const BlockHeader: React.FC<IBlockHeaderProps> = function (props) {
103115 { addonAfter && < div className = { `${ prefixCls } -addonAfter-box` } > { addonAfter } </ div > }
104116 { children && (
105117 < div className = { `${ prefixCls } -collapse-box` } >
106- < div className = "text" > { expand ? '收起' : '展开' } </ div >
107- < UpOutlined className = { `icon ${ expand ? 'up' : 'down' } ` } />
118+ < div className = "text" > { currentExpand ? '收起' : '展开' } </ div >
119+ < UpOutlined className = { `icon ${ currentExpand ? 'up' : 'down' } ` } />
108120 </ div >
109121 ) }
110122 </ div >
111123
112- < div className = { `${ prefixCls } -content ${ expand ? '' : 'hide' } ` } > { children } </ div >
124+ < div className = { `${ prefixCls } -content ${ currentExpand ? '' : 'hide' } ` } > { children } </ div >
113125 </ div >
114126 ) ;
115127} ;
0 commit comments