@@ -21,10 +21,14 @@ export type LabelTooltipType = TooltipProps | TooltipProps['title'];
21
21
22
22
export declare type SizeType = 'small' | 'middle' | 'large' ;
23
23
24
+ function isControlled ( props : IBlockHeaderProps ) {
25
+ return props . expand !== undefined ;
26
+ }
27
+
24
28
export interface IBlockHeaderProps {
25
- // 标题
29
+ /** 标题 */
26
30
title : string ;
27
- // 标题前的图标,默认是一个色块
31
+ /** 标题前的图标,默认是一个色块 */
28
32
addonBefore ?: ReactNode ;
29
33
// 标题后的提示说明文字
30
34
description ?: ReactNode ;
@@ -38,19 +42,24 @@ export interface IBlockHeaderProps {
38
42
* 默认 中标题
39
43
*/
40
44
size ?: SizeType ;
45
+ /** 是否展示 Bottom,默认 false,Bottom 值 16px */
41
46
hasBottom ?: boolean ;
47
+ /** 自定义 Bottom 值 */
42
48
spaceBottom ?: number ;
43
49
// 标题的样式类名
44
50
className ?: string ;
45
51
// 标题的样式
46
52
style ?: React . CSSProperties ;
47
- // 是否显示背景, 默认 true
53
+ /** 是否显示背景, 默认 true */
48
54
background ?: boolean ;
49
- // 是否默认展开内容, 默认 true
55
+ /** 当前展开状态 */
56
+ expand ?: boolean ;
57
+ /** 是否默认展开内容, 默认 true */
50
58
defaultExpand ?: boolean ;
59
+ /** 展开/收起的内容 */
51
60
children ?: ReactNode ;
52
- // 展开/收起时的回调
53
- onChange ?: ( expand : boolean ) => void ;
61
+ /** 展开/收起时的回调 */
62
+ onExpand ?: ( expand : boolean ) => void ;
54
63
}
55
64
const BlockHeader : React . FC < IBlockHeaderProps > = function ( props ) {
56
65
const prefixCls = 'dtc-block-header' ;
@@ -66,12 +75,15 @@ const BlockHeader: React.FC<IBlockHeaderProps> = function (props) {
66
75
background = true ,
67
76
defaultExpand = true ,
68
77
addonAfter,
78
+ expand,
69
79
children = '' ,
70
80
addonBefore = < div className = { `title__addon-before--${ size } ` } /> ,
71
- onChange ,
81
+ onExpand ,
72
82
} = props ;
73
83
74
- const [ expand , setExpand ] = useState ( defaultExpand ) ;
84
+ const [ internalExpand , setInternalExpand ] = useState ( defaultExpand ) ;
85
+
86
+ const currentExpand = isControlled ( props ) ? expand : internalExpand ;
75
87
76
88
const preTitleRowCls = `${ prefixCls } __title` ;
77
89
@@ -89,8 +101,8 @@ const BlockHeader: React.FC<IBlockHeaderProps> = function (props) {
89
101
90
102
const handleExpand = ( expand : boolean ) => {
91
103
if ( ! children ) return ;
92
- setExpand ( expand ) ;
93
- onChange ?.( expand ) ;
104
+ ! isControlled ( props ) && setInternalExpand ( expand ) ;
105
+ onExpand ?.( expand ) ;
94
106
} ;
95
107
96
108
return (
@@ -100,7 +112,7 @@ const BlockHeader: React.FC<IBlockHeaderProps> = function (props) {
100
112
[ `${ preTitleRowCls } --background` ] : background ,
101
113
[ `${ preTitleRowCls } --pointer` ] : children ,
102
114
} ) }
103
- onClick = { ( ) => handleExpand ( ! expand ) }
115
+ onClick = { ( ) => handleExpand ( ! currentExpand ) }
104
116
>
105
117
< div className = "title__box" >
106
118
{ addonBefore ? < div className = "title__addon-before" > { addonBefore } </ div > : null }
0 commit comments