Skip to content

Commit d05ecbe

Browse files
authored
feat(blockheader): supprt large size and change some props name (#478)
* feat(blockheader): supprt large size and change some props name * feat(blockheader): supprt padding for blockheader children * feat(blockheader): change tooltip to TooltipProps and use toTooltipProps * feat(blockheader): change classname to use BEM * feat(blockheader): change classname and add style * feat(blockheader): change doc and ts * feat(blockheader): use utils toTooltipProps and use BEM style
1 parent f8ca178 commit d05ecbe

File tree

11 files changed

+254
-223
lines changed

11 files changed

+254
-223
lines changed

src/blockHeader/__tests__/__snapshots__/blockHeader.test.tsx.snap

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,25 @@ exports[`test BlockHeader render should render BlockHeader success render 1`] =
99
class="dtc-block-header"
1010
>
1111
<div
12-
class="dtc-block-header-title-row dtc-block-header-title-row-middle dtc-block-header-title-row-background"
12+
class="dtc-block-header__title dtc-block-header__title--middle dtc-block-header__title--background"
1313
>
1414
<div
15-
class="dtc-block-header-title-box"
15+
class="title__box"
1616
>
1717
<div
18-
class="dtc-block-header-before-title"
18+
class="title__addon-before"
1919
>
2020
<div
21-
class="dtc-block-header__beforeTitle-middle"
21+
class="title__addon-before--middle"
2222
/>
2323
</div>
2424
<div
25-
class="dtc-block-header-title "
25+
class="title__text"
2626
>
2727
标题1
2828
</div>
2929
</div>
3030
</div>
31-
<div
32-
class="dtc-block-header-content "
33-
/>
3431
</div>
3532
</div>
3633
</body>,
@@ -39,28 +36,25 @@ exports[`test BlockHeader render should render BlockHeader success render 1`] =
3936
class="dtc-block-header"
4037
>
4138
<div
42-
class="dtc-block-header-title-row dtc-block-header-title-row-middle dtc-block-header-title-row-background"
39+
class="dtc-block-header__title dtc-block-header__title--middle dtc-block-header__title--background"
4340
>
4441
<div
45-
class="dtc-block-header-title-box"
42+
class="title__box"
4643
>
4744
<div
48-
class="dtc-block-header-before-title"
45+
class="title__addon-before"
4946
>
5047
<div
51-
class="dtc-block-header__beforeTitle-middle"
48+
class="title__addon-before--middle"
5249
/>
5350
</div>
5451
<div
55-
class="dtc-block-header-title "
52+
class="title__text"
5653
>
5754
标题1
5855
</div>
5956
</div>
6057
</div>
61-
<div
62-
class="dtc-block-header-content "
63-
/>
6458
</div>
6559
</div>,
6660
"debug": [Function],

src/blockHeader/__tests__/blockHeader.test.tsx

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,29 @@ import React from 'react';
22
import { cleanup, fireEvent, render } from '@testing-library/react';
33
import '@testing-library/jest-dom/extend-expect';
44

5-
import BlockHeader, { SizeType } from '../index';
5+
import BlockHeader, { IBlockHeaderProps, SizeType } from '../index';
66

7-
const props = {
7+
const props: IBlockHeaderProps = {
88
title: '标题1',
99
};
10-
const props2 = {
10+
const props2: IBlockHeaderProps = {
1111
title: '标题2',
12-
beforeTitle: <span>Icon</span>,
13-
afterTitle: '说明文字',
12+
addonBefore: <span>Icon</span>,
13+
description: '说明文字',
1414
addonAfter: <div className="test-button-after">新增按钮</div>,
1515
size: 'small' as SizeType,
16-
titleRowClassName: 'test-row-className',
17-
titleClassName: 'test-title-className',
16+
className: 'test__className',
17+
style: { height: '100px' },
18+
hasBottom: true,
1819
};
19-
const props3 = {
20+
const props3: IBlockHeaderProps = {
2021
title: 'hover',
2122
tooltip: 'hover 展示',
2223
};
23-
const props4 = {
24+
const props4: IBlockHeaderProps = {
2425
title: 'hover',
2526
tooltip: 'hover 展示',
26-
afterTitle: '我的优先级更高',
27+
description: '说明文字',
2728
};
2829
const prefixCls = 'dtc-block-header';
2930

@@ -42,21 +43,10 @@ describe('test BlockHeader render', () => {
4243
expect(getByText('标题2')).toBeTruthy();
4344
});
4445
test('should render BlockHeader props default in BlockHeader', () => {
45-
const { container } = render(<BlockHeader title="测试" showBackground />);
46+
const { container } = render(<BlockHeader title="测试" background />);
4647
const wrap = container.firstChild;
47-
expect(wrap!.firstChild!.firstChild!.firstChild).toHaveClass(`${prefixCls}-before-title`);
48-
fireEvent.click(document.getElementsByClassName(`${prefixCls}-title-row`)[0]);
49-
});
50-
test('should render BlockHeader with different props', () => {
51-
const { container, getByText } = render(<BlockHeader {...props2} />);
52-
const wrap = container.firstChild;
53-
expect(wrap).toHaveClass(`${prefixCls}`);
54-
expect(wrap!.lastChild).toHaveClass(`${prefixCls}-content`);
55-
expect(wrap!.firstChild).toHaveClass(`test-row-className`);
56-
expect(getByText('标题2')).toHaveClass('test-title-className');
57-
expect(getByText('说明文字')).toHaveClass(`${prefixCls}-after-title`);
58-
expect(getByText('新增按钮')).toHaveClass(`test-button-after`);
59-
expect(getByText('Icon')).toBeTruthy();
48+
expect(wrap!.firstChild!.firstChild!.firstChild).toHaveClass('title__addon-before');
49+
fireEvent.click(document.getElementsByClassName(`${prefixCls}__title`)[0]);
6050
});
6151
test('should render BlockHeader test click event', () => {
6252
const onChange = jest.fn();
@@ -66,7 +56,7 @@ describe('test BlockHeader render', () => {
6656
</BlockHeader>
6757
);
6858
expect(getByText('收起')).toBeTruthy();
69-
fireEvent.click(document.getElementsByClassName(`${prefixCls}-title-row`)[0]);
59+
fireEvent.click(document.getElementsByClassName(`${prefixCls}__title`)[0]);
7060
expect(getByText('展开')).toBeTruthy();
7161
expect(onChange).toHaveBeenCalledTimes(1);
7262
});
@@ -77,33 +67,30 @@ describe('test BlockHeader render', () => {
7767
</BlockHeader>
7868
);
7969
expect(getByText('收起')).toBeTruthy();
80-
fireEvent.click(document.getElementsByClassName(`${prefixCls}-title-row`)[0]);
70+
fireEvent.click(document.getElementsByClassName(`${prefixCls}__title`)[0]);
8171
expect(getByText('展开')).toBeTruthy();
8272
});
8373
test('should render BlockHeader with different props', () => {
8474
const { container, getByText } = render(<BlockHeader {...props2} />);
8575
const wrap = container.firstChild;
86-
expect(wrap).toHaveClass(`${prefixCls}`);
87-
expect(wrap!.lastChild).toHaveClass(`${prefixCls}-content`);
88-
expect(wrap!.firstChild).toHaveClass(`test-row-className`);
89-
expect(getByText('标题2')).toHaveClass('test-title-className');
90-
expect(getByText('说明文字')).toHaveClass(`${prefixCls}-after-title`);
76+
expect(wrap).toHaveClass(`${prefixCls} test__className`);
77+
expect(wrap).toHaveStyle({ height: '100px', marginBottom: '16px' });
78+
expect(getByText('标题2')).toHaveClass('title__text');
79+
expect(getByText('说明文字')).toHaveClass('title__description');
9180
expect(getByText('Icon')).toBeTruthy();
9281
});
93-
test('should render BlockHeader className when isSmall is small', () => {
94-
const props = { title: '测试1', showBackground: false };
82+
test('should render BlockHeader background success', () => {
83+
const props = { title: '测试1', background: false };
9584
const { container } = render(<BlockHeader {...props} />);
9685
const wrap = container.firstChild;
9786
expect(wrap!.firstChild).not.toHaveClass(`background`);
9887
});
9988
test('should render BlockHeader className when isSmall is small', () => {
10089
const { container, getByText } = render(<BlockHeader {...props2} />);
10190
const wrap = container.firstChild!;
102-
expect(wrap).toHaveClass(`${prefixCls}`);
103-
expect(wrap.lastChild).toHaveClass(`${prefixCls}-content`);
104-
expect(wrap.firstChild).toHaveClass(`test-row-className`);
105-
expect(getByText('标题2')).toHaveClass('test-title-className');
106-
expect(getByText('说明文字')).toHaveClass(`${prefixCls}-after-title`);
91+
expect(wrap).toHaveClass(`${prefixCls} test__className`);
92+
expect(getByText('标题2')).toHaveClass('title__text');
93+
expect(getByText('说明文字')).toHaveClass('title__description');
10794
expect(getByText('Icon')).toBeTruthy();
10895
});
10996

@@ -114,32 +101,32 @@ describe('test BlockHeader render', () => {
114101
expect(afterTitleWrap!.firstChild).toHaveClass('anticon-question-circle');
115102
});
116103

117-
test('should render BlockHeader tooltip and afterTitle success', () => {
104+
test('should render BlockHeader tooltip and desc success', () => {
118105
const { container } = render(<BlockHeader {...props4} />);
119106
const wrap = container.firstChild!;
120107
const afterTitleWrap = wrap.firstChild!.firstChild!.lastChild;
121-
expect(afterTitleWrap).toHaveTextContent('我的优先级更高');
108+
expect(afterTitleWrap).toHaveTextContent('说明文字');
122109
});
123110
test('should render BlockHeader correct dom length', () => {
124-
const { container } = render(<BlockHeader title="分类级别" beforeTitle="" />);
111+
const { container } = render(<BlockHeader title="分类级别" addonBefore="" />);
125112
const titleBoxWrap = container.firstChild!.firstChild!.firstChild;
126113
expect(titleBoxWrap!.childNodes.length).toEqual(1);
127114

128115
const { container: container1 } = render(
129-
<BlockHeader title="分类级别" afterTitle="测试" />
116+
<BlockHeader title="分类级别" description="测试" />
130117
);
131118
const titleBoxWrap1 = container1.firstChild!.firstChild!.firstChild;
132119
expect(titleBoxWrap1!.childNodes.length).toEqual(3);
133120
});
134121
test('should render BlockHeader correct margin-bottom', () => {
135-
const { container: noStyle } = render(<BlockHeader title="分类级别" beforeTitle="" />);
122+
const { container: noStyle } = render(<BlockHeader title="分类级别" addonBefore="" />);
136123
expect(noStyle.querySelector('.dtc-block-header')).not.toHaveAttribute('style');
137124
const { container: defaultBottom } = render(
138-
<BlockHeader title="分类级别" beforeTitle="" />
125+
<BlockHeader title="分类级别" addonBefore="" />
139126
);
140127
expect(defaultBottom.querySelector('.dtc-block-header')).toHaveStyle({ marginBottom: 16 });
141128
const { container: customizeBottom } = render(
142-
<BlockHeader title="分类级别" beforeTitle="" hasBottom spaceBottom={10} />
129+
<BlockHeader title="分类级别" addonBefore="" hasBottom spaceBottom={10} />
143130
);
144131
expect(customizeBottom.querySelector('.dtc-block-header')).toHaveStyle({
145132
marginBottom: 10,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react';
2+
import { Input, Space } from 'antd';
3+
import { BlockHeader } from 'dt-react-component';
4+
5+
export default () => (
6+
<Space size={12} direction="vertical" style={{ width: '100%' }}>
7+
<BlockHeader title="分类标题" addonAfter="这是 addonAfter 内容" />
8+
<BlockHeader
9+
background={false}
10+
title="分类标题"
11+
addonAfter={<Input placeholder="请输入" />}
12+
tooltip={{ title: '这里展示问号提示' }}
13+
/>
14+
</Space>
15+
);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ export default () => {
99
<br />
1010
<BlockHeader
1111
title="分类标题"
12-
beforeTitle={<PieChartOutlined style={{ fontSize: '14px' }} />}
12+
addonBefore={<PieChartOutlined style={{ fontSize: '14px' }} />}
1313
/>
1414
<br />
1515
<BlockHeader
1616
title="分类标题"
17-
beforeTitle={<PauseCircleOutlined style={{ fontSize: '14px' }} />}
17+
addonBefore={<PauseCircleOutlined style={{ fontSize: '14px' }} />}
1818
/>
1919
</>
2020
);

src/blockHeader/demos/basic.tsx

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,52 @@
11
import React, { useState } from 'react';
2-
import { Switch } from 'antd';
2+
import { Radio, Space, Switch } from 'antd';
33
import { BlockHeader } from 'dt-react-component';
4+
import { SizeType } from 'dt-react-component/blockHeader';
45

56
export default () => {
7+
const [size, setSize] = useState<SizeType>('middle');
68
const [showBackground, setShowBackground] = useState(true);
9+
const [tooltip, setTooltip] = useState(true);
10+
const [description, setDescription] = useState(true);
11+
712
return (
8-
<>
9-
背景:
10-
<Switch defaultChecked onChange={setShowBackground} />
13+
<div>
14+
<Space direction="vertical" size={12}>
15+
<Radio.Group value={size} onChange={(e) => setSize(e.target.value)}>
16+
<Radio.Button value="small">Small</Radio.Button>
17+
<Radio.Button value="middle">Middle</Radio.Button>
18+
<Radio.Button value="large">Large</Radio.Button>
19+
</Radio.Group>
20+
<Space direction="horizontal" size={12}>
21+
<Switch
22+
defaultChecked
23+
onChange={setShowBackground}
24+
checkedChildren="带背景"
25+
unCheckedChildren="不带背景"
26+
/>
27+
<Switch
28+
defaultChecked
29+
onChange={setTooltip}
30+
checkedChildren="展示 tooltip"
31+
unCheckedChildren="不展示 tooltip"
32+
/>
33+
<Switch
34+
defaultChecked
35+
onChange={setDescription}
36+
checkedChildren="展示说明文字"
37+
unCheckedChildren="不展示说明文字"
38+
/>
39+
</Space>
40+
</Space>
1141
<br />
1242
<br />
13-
<BlockHeader title="分类标题" showBackground={showBackground} />
14-
</>
43+
<BlockHeader
44+
size={size}
45+
title="分类标题"
46+
background={showBackground}
47+
tooltip={tooltip && '这里展示问号提示'}
48+
description={description && '提示说明文字'}
49+
/>
50+
</div>
1551
);
1652
};

src/blockHeader/demos/extraInfo.tsx

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/blockHeader/demos/size.tsx

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/blockHeader/index.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,29 @@ demo:
1616

1717
## 示例
1818

19-
<code src="./demos/basic.tsx" description="通过设置 `showBackground={false}` 去除标题背景,默认为 `true`">基础使用</code>
20-
<code src="./demos/size.tsx" description="标题有中、小两种尺寸,默认为中尺寸,通过设置 `size='small'` 把标题设置为小尺寸">标题尺寸</code>
21-
<code src="./demos/extraInfo.tsx" description="通过设置 `afterTitle` 和 `tooltip` 可以增加两种不同形式的提示信息,同时存在时仅 `afterTitle` 生效">带提示信息的标题</code>
22-
<code src="./demos/customIcon.tsx" description="通过设置 `beforeTitle` 可以自定义标题icon,不设置时默认是一个色块">自定义 icon</code>
19+
<code src="./demos/basic.tsx" description="配置大小、tooltip、描述">基础使用</code>
20+
<code src="./demos/addonBefore.tsx" description="通过 `addonBefore` 可以设置标题前的图标,不设置时默认是一个色块">自定义 icon</code>
21+
<code src="./demos/addonAfter.tsx" description="通过 `addonAfter` 可以设置后缀自定义内容块">带提示信息的标题</code>
2322
<code src="./demos/expand.tsx" description="若存在 `children` 则支持展开">展开/收起内容</code>
2423

2524
## API
2625

2726
### BlockHeader
2827

29-
| 参数 | 说明 | 类型 | 默认值 |
30-
| ----------------- | ----------------------------------------- | --------------------------- | ------- |
31-
| title | 标题 | `string` | - |
32-
| beforeTitle | 标题前的图标,默认是一个色块 | `React.ReactNode` | - |
33-
| afterTitle | 标题后的提示图标或文案 | `React.ReactNode` | - |
34-
| tooltip | 默认展示问号提示(优先级低于 `afterTitle`) | `React.ReactNode` | - |
35-
| isSmall | 大标题、小标题,默认为大标题 | `boolean` | `false` |
36-
| titleRowClassName | 标题一行的样式类名 | `string` | - |
37-
| titleClassName | 标题的样式类名 | `string` | - |
38-
| showBackground | 是否显示背景 | `boolean` | `true` |
39-
| defaultExpand | 是否默认展开内容 | `boolean` | `true` |
28+
| 参数 | 说明 | 类型 | 默认值 |
29+
| ----------------- | ---------------------------------- | --------------------------- | -------- |
30+
| title | 标题 | `string` | - |
31+
| addonBefore | 标题前的图标,默认是一个色块 | `React.ReactNode` | - |
32+
| description | 标题提示文案 | `React.ReactNode` | - |
33+
| tooltip | 默认展示问号提示 | `TooltipProps \| TooltipProps['title']` | - |
34+
| addonAfter | 标题后的内容 | `React.ReactNode` | - |
35+
| size | 小标题、中标题,默认为中标题 | `small \| middle \| large` | `middle` |
36+
| className | 标题一行的样式类名 | `string` | - |
37+
| style | 标题的样式 | `React.CSSProperties` | - |
38+
| background | 是否显示背景 | `boolean` | `true` |
4039
| expand | 当前展开状态 | `boolean` | |
41-
| hasBottom | 是否有默认下边距 16px | `boolean` | `false` |
42-
| spaceBottom | 自定义下边距,优先级高于 hasBottom | `number` | `0` |
43-
| children | 展开/收起的内容 | `React.ReactNode` | - |
40+
| defaultExpand | 是否默认展开内容 | `boolean` | `true` |
41+
| hasBottom | 是否有默认下边距 16px | `boolean` | `false` |
42+
| spaceBottom | 自定义下边距,优先级高于 hasBottom | `number` | `0` |
43+
| children | 展开/收起的内容 | `React.ReactNode` | - |
4444
| onExpand | 展开/收起时的回调 | `(expand: boolean) => void` | - |

0 commit comments

Comments
 (0)