Skip to content

Commit cfc126b

Browse files
committed
feat(blockheader): support expand controll and change callname to onExpand
1 parent 237ec5d commit cfc126b

File tree

4 files changed

+35
-23
lines changed

4 files changed

+35
-23
lines changed

src/blockHeader/__tests__/blockHeader.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('test BlockHeader render', () => {
5151
test('should render BlockHeader test click event', () => {
5252
const onChange = jest.fn();
5353
const { getByText } = render(
54-
<BlockHeader onChange={onChange} title="测试">
54+
<BlockHeader onExpand={onChange} title="测试">
5555
<div>1111</div>
5656
</BlockHeader>
5757
);

src/blockHeader/demos/expand.tsx

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
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-
<>
8+
<Space direction="vertical" style={{ width: '100%' }}>
79
<BlockHeader
8-
title="分类标题"
10+
title="非受控标题"
911
defaultExpand={false}
1012
hasBottom
11-
onChange={(expand) => console.log(expand)}
13+
onExpand={(expand) => console.log(expand)}
1214
>
1315
Hello World!
1416
</BlockHeader>
15-
<BlockHeader
16-
title="分类标题"
17-
defaultExpand={false}
18-
onChange={(expand) => console.log(expand)}
19-
>
17+
18+
<BlockHeader title="受控标题" expand={expand} onExpand={(expand) => setExpand(expand)}>
2019
Hello World!
2120
</BlockHeader>
22-
</>
21+
</Space>
2322
);
2423
};

src/blockHeader/index.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ demo:
3838
| background | 是否显示背景 | `boolean` | `true` |
3939
| defaultExpand | 是否默认展开内容 | `boolean` | `true` |
4040
| hasBottom | 是否有默认下边距 16px | `boolean` | `false` |
41+
| expand | 当前展开状态 | `boolean` | |
4142
| spaceBottom | 自定义下边距,优先级高于 hasBottom | `number` | `0` |
4243
| children | 展开/收起的内容 | `React.ReactNode` | - |
43-
| onChange | 展开/收起时的回调 | `(expand: boolean) => void` | - |
44+
| onExpand | 展开/收起时的回调 | `(expand: boolean) => void` | - |

src/blockHeader/index.tsx

+23-11
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ export type LabelTooltipType = TooltipProps | TooltipProps['title'];
2121

2222
export declare type SizeType = 'small' | 'middle' | 'large';
2323

24+
function isControlled(props: IBlockHeaderProps) {
25+
return props.expand !== undefined;
26+
}
27+
2428
export interface IBlockHeaderProps {
25-
// 标题
29+
/** 标题 */
2630
title: string;
27-
// 标题前的图标,默认是一个色块
31+
/** 标题前的图标,默认是一个色块 */
2832
addonBefore?: ReactNode;
2933
// 标题后的提示说明文字
3034
description?: ReactNode;
@@ -38,19 +42,24 @@ export interface IBlockHeaderProps {
3842
* 默认 中标题
3943
*/
4044
size?: SizeType;
45+
/** 是否展示 Bottom,默认 false,Bottom 值 16px */
4146
hasBottom?: boolean;
47+
/** 自定义 Bottom 值 */
4248
spaceBottom?: number;
4349
// 标题的样式类名
4450
className?: string;
4551
// 标题的样式
4652
style?: React.CSSProperties;
47-
// 是否显示背景, 默认 true
53+
/** 是否显示背景, 默认 true */
4854
background?: boolean;
49-
// 是否默认展开内容, 默认 true
55+
/** 当前展开状态 */
56+
expand?: boolean;
57+
/** 是否默认展开内容, 默认 true */
5058
defaultExpand?: boolean;
59+
/** 展开/收起的内容 */
5160
children?: ReactNode;
52-
// 展开/收起时的回调
53-
onChange?: (expand: boolean) => void;
61+
/** 展开/收起时的回调 */
62+
onExpand?: (expand: boolean) => void;
5463
}
5564
const BlockHeader: React.FC<IBlockHeaderProps> = function (props) {
5665
const prefixCls = 'dtc-block-header';
@@ -66,12 +75,15 @@ const BlockHeader: React.FC<IBlockHeaderProps> = function (props) {
6675
background = true,
6776
defaultExpand = true,
6877
addonAfter,
78+
expand,
6979
children = '',
7080
addonBefore = <div className={`title__addon-before--${size}`} />,
71-
onChange,
81+
onExpand,
7282
} = props;
7383

74-
const [expand, setExpand] = useState(defaultExpand);
84+
const [internalExpand, setInternalExpand] = useState(defaultExpand);
85+
86+
const currentExpand = isControlled(props) ? expand : internalExpand;
7587

7688
const preTitleRowCls = `${prefixCls}__title`;
7789

@@ -89,8 +101,8 @@ const BlockHeader: React.FC<IBlockHeaderProps> = function (props) {
89101

90102
const handleExpand = (expand: boolean) => {
91103
if (!children) return;
92-
setExpand(expand);
93-
onChange?.(expand);
104+
!isControlled(props) && setInternalExpand(expand);
105+
onExpand?.(expand);
94106
};
95107

96108
return (
@@ -100,7 +112,7 @@ const BlockHeader: React.FC<IBlockHeaderProps> = function (props) {
100112
[`${preTitleRowCls}--background`]: background,
101113
[`${preTitleRowCls}--pointer`]: children,
102114
})}
103-
onClick={() => handleExpand(!expand)}
115+
onClick={() => handleExpand(!currentExpand)}
104116
>
105117
<div className="title__box">
106118
{addonBefore ? <div className="title__addon-before">{addonBefore}</div> : null}

0 commit comments

Comments
 (0)