Skip to content

Commit cafad70

Browse files
authored
feat(blockheader): support expand controll and change callname to onExpand (#447)
1 parent dbd07d6 commit cafad70

File tree

4 files changed

+50
-29
lines changed

4 files changed

+50
-29
lines changed

src/blockHeader/__tests__/blockHeader.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('test BlockHeader render', () => {
6161
test('should render BlockHeader test click event', () => {
6262
const onChange = jest.fn();
6363
const { getByText } = render(
64-
<BlockHeader onChange={onChange} title="测试">
64+
<BlockHeader onExpand={onChange} title="测试">
6565
<div>1111</div>
6666
</BlockHeader>
6767
);

src/blockHeader/demos/expand.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
2+
import { Space } from 'antd';
23
import { BlockHeader } from 'dt-react-component';
34

45
export default () => {
6+
const [expand, setExpand] = useState(false);
57
return (
6-
<BlockHeader
7-
title="分类标题"
8-
defaultExpand={false}
9-
onChange={(expand) => console.log(expand)}
10-
>
11-
Hello World!
12-
</BlockHeader>
8+
<Space direction="vertical" style={{ width: '100%' }}>
9+
<BlockHeader
10+
title="非受控标题"
11+
defaultExpand={false}
12+
onExpand={(expand) => console.log(expand)}
13+
>
14+
Hello World!
15+
</BlockHeader>
16+
17+
<BlockHeader title="受控标题" expand={expand} onExpand={(expand) => setExpand(expand)}>
18+
Hello World!
19+
</BlockHeader>
20+
</Space>
1321
);
1422
};

src/blockHeader/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ demo:
3737
| titleClassName | 标题的样式类名 | `string` | - |
3838
| showBackground | 是否显示背景 | `boolean` | `true` |
3939
| defaultExpand | 是否默认展开内容 | `boolean` | `true` |
40+
| expand | 当前展开状态 | `boolean` | |
4041
| hasBottom | 是否有默认下边距 16px | `boolean` | `false` |
4142
| spaceBottom | 自定义下边距,优先级高于 hasBottom | `number` | `0` |
4243
| children | 展开/收起的内容 | `React.ReactNode` | - |
43-
| onChange | 展开/收起时的回调 | `(expand: boolean) => void` | - |
44+
| onExpand | 展开/收起时的回调 | `(expand: boolean) => void` | - |

src/blockHeader/index.tsx

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,45 @@ import './style.scss';
77

88
export declare type SizeType = 'small' | 'middle' | undefined;
99

10+
function isControlled(props: IBlockHeaderProps) {
11+
return props.expand !== undefined;
12+
}
13+
1014
export 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
}
4150
const 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

Comments
 (0)