diff --git a/src/empty/__tests__/empty.test.tsx b/src/empty/__tests__/empty.test.tsx index 699d764a8..269c4897e 100644 --- a/src/empty/__tests__/empty.test.tsx +++ b/src/empty/__tests__/empty.test.tsx @@ -2,7 +2,7 @@ import * as React from 'react'; import { render } from '@testing-library/react'; import '@testing-library/jest-dom/extend-expect'; -import Empty, { IMG_MAP } from '../index'; +import Empty, { IMG_MAP } from '..'; describe('Empty', () => { test('should support empty success render', () => { @@ -10,9 +10,9 @@ describe('Empty', () => { expect(wrapper).toMatchSnapshot(); }); it('should support empty image default size', () => { - const { container } = render(); + const { container } = render(); expect(container.querySelector('.ant-empty-image')?.style.height).toBe( - '80px' + '100px' ); }); it('should support empty image size should change', () => { @@ -23,7 +23,7 @@ describe('Empty', () => { }); it('should support empty image size from iamgeStyle', () => { - const { container } = render(); + const { container } = render(); expect(container.querySelector('.ant-empty-image')?.style.height).toBe( '40px' ); @@ -45,4 +45,29 @@ describe('Empty', () => { const srcValue = container.querySelector('img')!.getAttribute('src'); expect(srcValue).toEqual(IMG_MAP['project']); }); + + it('should show correct content when not empty', () => { + const { container } = render( + +
show data
+
+ ); + expect(container.querySelector('.data')).not.toBeNull(); + expect(container.querySelector('.data')?.innerHTML).toEqual('show data'); + }); + + it('should not show content when empty', () => { + const { container } = render( + +
show data
+
+ ); + expect(container.querySelector('.dtc-empty')?.children[0].classList).toContain('ant-empty'); + }); + + it('should show correct antd empty children', () => { + const { container } = render(); + expect(container.querySelector('.ant-empty-footer')).not.toBeNull(); + expect(container.querySelector('.ant-empty-footer')?.innerHTML).toBe('antd empty children'); + }); }); diff --git a/src/empty/index.md b/src/empty/index.md index fbe16b4bb..3c3bed17e 100644 --- a/src/empty/index.md +++ b/src/empty/index.md @@ -13,6 +13,7 @@ demo: - 当目前没有数据时,用于显式的用户提示。 - 初始化场景时的引导创建流程。 - 内置 6 种空状态类型。 +- 用于三元表达式来判断展示 `` 还是 ``。 ## 示例 @@ -87,7 +88,7 @@ export default () => { ```jsx /** - * title: "更多配置" + * title: "控制图片大小" */ import React from 'react'; import { Empty } from 'dt-react-component'; @@ -96,25 +97,76 @@ import { Divider } from 'antd'; export default () => { return ( <> - + + ); }; ``` +```jsx +/** + * title: "判断展示内容" + */ +import React, { useState } from 'react'; +import { Space, Switch } from 'antd'; +import { Empty } from 'dt-react-component'; + +export default () => { + const [empty, setEmpty] = useState(false); + + return ( + + setEmpty(checked)} + checkedChildren="展示占位符" + unCheckedChildren="展示内容" + /> + More Data + + ); +}; +``` + +```jsx +/** + * title: "展示 antd Empty 组件的 children" + */ +import React, { useState } from 'react'; +import { Button, Space, Switch } from 'antd'; +import { Empty } from 'dt-react-component'; + +export default () => { + const [empty, setEmpty] = useState(false); + + return ( + + setEmpty(checked)} + checkedChildren="展示占位符" + unCheckedChildren="展示内容" + /> + 添加}> + More Data + + + ); +}; +``` + ## API -| 参数 | 说明 | 类型 | 默认值 | -| ---------- | ------------------------------------------ | --------------------------------------------------------------------------- | --------- | -| type | 默认展示图片的类型 | `default` \| `project` \| `chart` \| `search` \| `permission` \| `overview` | `default` | -| height | 图片高度 | `number` | 80 | -| image | 自定义图片(设置该参数时,默认的图片不生效) | `React.ReactNode` | - | -| imageStyle | 自定义图片样式 | `React.CSSProperties` | - | +| 参数 | 说明 | 类型 | 默认值 | +| --------- | --------------------------- | --------------------------------------------------------------------------- | --------- | +| type | 默认展示图片的类型 | `default` \| `project` \| `chart` \| `search` \| `permission` \| `overview` | `default` | +| size | 图片大小 | `default` \| `large` | `default` | +| showEmpty | 是否展示 Empty 组件 | `boolean` | `true` | +| children | 展示内容 | `React.ReactNode` | - | +| extra | 替换 antd Empty 的 children | ` React.ReactNode` | - | :::info 其余属性[继承 antd4.x 的 Empty](https://ant.design/components/empty-cn/#API) diff --git a/src/empty/index.tsx b/src/empty/index.tsx index 9a1086f51..a7b644978 100644 --- a/src/empty/index.tsx +++ b/src/empty/index.tsx @@ -1,5 +1,5 @@ -import React, { CSSProperties, ReactNode } from 'react'; -import { Empty as AntdEmpty, EmptyProps } from 'antd'; +import React, { ReactNode } from 'react'; +import { Empty as AntdEmpty, EmptyProps as AntdEmptyProps } from 'antd'; import './style.scss'; @@ -12,24 +12,40 @@ export const IMG_MAP = { permission: 'empty_permission.png', }; -interface IProps { +export interface EmptyProps extends AntdEmptyProps { type?: 'default' | 'search' | 'chart' | 'project' | 'overview' | 'permission'; - height?: number; - image?: ReactNode; - imageStyle?: CSSProperties; + size?: 'default' | 'large'; + showEmpty?: boolean; + extra?: ReactNode; } -const Empty = (props: EmptyProps & IProps) => { - const { type = 'default', height = 80, image, imageStyle, ...restProps } = props; +const Empty = (props: EmptyProps) => { + const { + type = 'default', + size = 'default', + showEmpty = true, + children, + image, + imageStyle, + extra, + ...restProps + } = props; + let newImage: ReactNode = IMG_MAP[type] ? ( ) : null; if (image) newImage = image as ReactNode; - const newImageStyle = imageStyle ? { height, ...imageStyle } : { height }; - return ( + + const height = size === 'default' ? 80 : 100; + + return showEmpty ? (
- + + {extra} +
+ ) : ( + <>{children} ); };