Skip to content

Commit 264a042

Browse files
authored
feat(empty): update empty (#441)
* feat(empty): add size, remove height props and extends EmptyProps * feat(empty): add Empty.Placeholder * feat(empty): support use for ternary expressions to determine display * feat(empty): update some props, use isEmpty/extra replace show/render * feat(empty): update extra to reactNode and doc * feat(empty): change prop isEmpty to empty * feat(empty): change docs isEmpty to empty * feat(empty): change empty to showEmpty
1 parent 97a5cff commit 264a042

File tree

3 files changed

+119
-26
lines changed

3 files changed

+119
-26
lines changed

Diff for: src/empty/__tests__/empty.test.tsx

+29-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import * as React from 'react';
22
import { render } from '@testing-library/react';
33
import '@testing-library/jest-dom/extend-expect';
44

5-
import Empty, { IMG_MAP } from '../index';
5+
import Empty, { IMG_MAP } from '..';
66

77
describe('Empty', () => {
88
test('should support empty success render', () => {
99
const wrapper = render(<Empty />);
1010
expect(wrapper).toMatchSnapshot();
1111
});
1212
it('should support empty image default size', () => {
13-
const { container } = render(<Empty />);
13+
const { container } = render(<Empty size="large" />);
1414
expect(container.querySelector<HTMLDivElement>('.ant-empty-image')?.style.height).toBe(
15-
'80px'
15+
'100px'
1616
);
1717
});
1818
it('should support empty image size should change', () => {
@@ -23,7 +23,7 @@ describe('Empty', () => {
2323
});
2424

2525
it('should support empty image size from iamgeStyle', () => {
26-
const { container } = render(<Empty imageStyle={{ height: 40 }} height={100} />);
26+
const { container } = render(<Empty imageStyle={{ height: 40 }} size="large" />);
2727
expect(container.querySelector<HTMLDivElement>('.ant-empty-image')?.style.height).toBe(
2828
'40px'
2929
);
@@ -45,4 +45,29 @@ describe('Empty', () => {
4545
const srcValue = container.querySelector('img')!.getAttribute('src');
4646
expect(srcValue).toEqual(IMG_MAP['project']);
4747
});
48+
49+
it('should show correct content when not empty', () => {
50+
const { container } = render(
51+
<Empty type="project" showEmpty={false}>
52+
<div className="data">show data</div>
53+
</Empty>
54+
);
55+
expect(container.querySelector('.data')).not.toBeNull();
56+
expect(container.querySelector('.data')?.innerHTML).toEqual('show data');
57+
});
58+
59+
it('should not show content when empty', () => {
60+
const { container } = render(
61+
<Empty type="project">
62+
<div className="data">show data</div>
63+
</Empty>
64+
);
65+
expect(container.querySelector('.dtc-empty')?.children[0].classList).toContain('ant-empty');
66+
});
67+
68+
it('should show correct antd empty children', () => {
69+
const { container } = render(<Empty type="project" extra={'antd empty children'} />);
70+
expect(container.querySelector('.ant-empty-footer')).not.toBeNull();
71+
expect(container.querySelector('.ant-empty-footer')?.innerHTML).toBe('antd empty children');
72+
});
4873
});

Diff for: src/empty/index.md

+63-11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ demo:
1313
- 当目前没有数据时,用于显式的用户提示。
1414
- 初始化场景时的引导创建流程。
1515
- 内置 6 种空状态类型。
16+
- 用于三元表达式来判断展示 `<Empty />` 还是 `<OtherComponent />`
1617

1718
## 示例
1819

@@ -87,7 +88,7 @@ export default () => {
8788

8889
```jsx
8990
/**
90-
* title: "更多配置"
91+
* title: "控制图片大小"
9192
*/
9293
import React from 'react';
9394
import { Empty } from 'dt-react-component';
@@ -96,25 +97,76 @@ import { Divider } from 'antd';
9697
export default () => {
9798
return (
9899
<>
99-
<Empty description="使用 height 定义图片大小" height={60} />
100+
<Empty description="使用 size: default, 默认大小为 80" />
101+
<Empty size="large" description="使用 size: large, 默认大小为 100" />
100102
<Empty
101-
height={60}
102-
imageStyle={{ height: 120 }}
103-
description="使用继承自antd的属性imageStyle"
103+
imageStyle={{ height: 160 }}
104+
description="使用 imageStyle, 设置其他高度以及属性"
104105
/>
105106
</>
106107
);
107108
};
108109
```
109110

111+
```jsx
112+
/**
113+
* title: "判断展示内容"
114+
*/
115+
import React, { useState } from 'react';
116+
import { Space, Switch } from 'antd';
117+
import { Empty } from 'dt-react-component';
118+
119+
export default () => {
120+
const [empty, setEmpty] = useState(false);
121+
122+
return (
123+
<Space direction="vertical" style={{ width: '100%' }} size={16}>
124+
<Switch
125+
onChange={(checked) => setEmpty(checked)}
126+
checkedChildren="展示占位符"
127+
unCheckedChildren="展示内容"
128+
/>
129+
<Empty showEmpty={empty}>More Data</Empty>
130+
</Space>
131+
);
132+
};
133+
```
134+
135+
```jsx
136+
/**
137+
* title: "展示 antd Empty 组件的 children"
138+
*/
139+
import React, { useState } from 'react';
140+
import { Button, Space, Switch } from 'antd';
141+
import { Empty } from 'dt-react-component';
142+
143+
export default () => {
144+
const [empty, setEmpty] = useState(false);
145+
146+
return (
147+
<Space direction="vertical" style={{ width: '100%' }} size={16}>
148+
<Switch
149+
onChange={(checked) => setEmpty(checked)}
150+
checkedChildren="展示占位符"
151+
unCheckedChildren="展示内容"
152+
/>
153+
<Empty showEmpty={empty} extra={<Button>添加</Button>}>
154+
More Data
155+
</Empty>
156+
</Space>
157+
);
158+
};
159+
```
160+
110161
## API
111162

112-
| 参数 | 说明 | 类型 | 默认值 |
113-
| ---------- | ------------------------------------------ | --------------------------------------------------------------------------- | --------- |
114-
| type | 默认展示图片的类型 | `default` \| `project` \| `chart` \| `search` \| `permission` \| `overview` | `default` |
115-
| height | 图片高度 | `number` | 80 |
116-
| image | 自定义图片(设置该参数时,默认的图片不生效) | `React.ReactNode` | - |
117-
| imageStyle | 自定义图片样式 | `React.CSSProperties` | - |
163+
| 参数 | 说明 | 类型 | 默认值 |
164+
| --------- | --------------------------- | --------------------------------------------------------------------------- | --------- |
165+
| type | 默认展示图片的类型 | `default` \| `project` \| `chart` \| `search` \| `permission` \| `overview` | `default` |
166+
| size | 图片大小 | `default` \| `large` | `default` |
167+
| showEmpty | 是否展示 Empty 组件 | `boolean` | `true` |
168+
| children | 展示内容 | `React.ReactNode` | - |
169+
| extra | 替换 antd Empty 的 children | ` React.ReactNode` | - |
118170

119171
:::info
120172
其余属性[继承 antd4.x 的 Empty](https://ant.design/components/empty-cn/#API)

Diff for: src/empty/index.tsx

+27-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { CSSProperties, ReactNode } from 'react';
2-
import { Empty as AntdEmpty, EmptyProps } from 'antd';
1+
import React, { ReactNode } from 'react';
2+
import { Empty as AntdEmpty, EmptyProps as AntdEmptyProps } from 'antd';
33

44
import './style.scss';
55

@@ -12,24 +12,40 @@ export const IMG_MAP = {
1212
permission: 'empty_permission.png',
1313
};
1414

15-
interface IProps {
15+
export interface EmptyProps extends AntdEmptyProps {
1616
type?: 'default' | 'search' | 'chart' | 'project' | 'overview' | 'permission';
17-
height?: number;
18-
image?: ReactNode;
19-
imageStyle?: CSSProperties;
17+
size?: 'default' | 'large';
18+
showEmpty?: boolean;
19+
extra?: ReactNode;
2020
}
2121

22-
const Empty = (props: EmptyProps & IProps) => {
23-
const { type = 'default', height = 80, image, imageStyle, ...restProps } = props;
22+
const Empty = (props: EmptyProps) => {
23+
const {
24+
type = 'default',
25+
size = 'default',
26+
showEmpty = true,
27+
children,
28+
image,
29+
imageStyle,
30+
extra,
31+
...restProps
32+
} = props;
33+
2434
let newImage: ReactNode = IMG_MAP[type] ? (
2535
<img src={require('./emptyImg/' + IMG_MAP[type])}></img>
2636
) : null;
2737
if (image) newImage = image as ReactNode;
28-
const newImageStyle = imageStyle ? { height, ...imageStyle } : { height };
29-
return (
38+
39+
const height = size === 'default' ? 80 : 100;
40+
41+
return showEmpty ? (
3042
<div className="dtc-empty">
31-
<AntdEmpty {...restProps} image={newImage} imageStyle={newImageStyle} />
43+
<AntdEmpty {...restProps} image={newImage} imageStyle={{ height, ...imageStyle }}>
44+
{extra}
45+
</AntdEmpty>
3246
</div>
47+
) : (
48+
<>{children}</>
3349
);
3450
};
3551

0 commit comments

Comments
 (0)