Skip to content

Commit

Permalink
feat: add loading prop for Statistic (#26811)
Browse files Browse the repository at this point in the history
* feat: add loading prop for Statistic

* feat: update statistic demo

* feat: add style dependency

* feat: replace loading component

* feat: update doc of statistic

* fix loading type

* feat: add loading prop for Statistic

* feat: update statistic demo

* update api version

Co-authored-by: lvpansen <pansen.lv@atzuche.com>
  • Loading branch information
appleshell and lvpansen authored Oct 23, 2020
1 parent 888e28f commit b4c6740
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 5 deletions.
16 changes: 11 additions & 5 deletions components/statistic/Statistic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import classNames from 'classnames';

import { ConfigConsumerProps } from '../config-provider';
import { withConfigConsumer } from '../config-provider/context';
import Skeleton from '../skeleton';
import StatisticNumber from './Number';
import Countdown from './Countdown';
import { valueType, FormatConfig } from './utils';
Expand All @@ -21,6 +22,7 @@ export interface StatisticProps extends FormatConfig {
title?: React.ReactNode;
prefix?: React.ReactNode;
suffix?: React.ReactNode;
loading?: boolean;
onMouseEnter?: React.MouseEventHandler<HTMLDivElement>;
onMouseLeave?: React.MouseEventHandler<HTMLDivElement>;
}
Expand All @@ -36,6 +38,7 @@ const Statistic: React.FC<StatisticProps & ConfigConsumerProps> = props => {
valueRender,
prefix,
suffix,
loading,
direction,
onMouseEnter,
onMouseLeave,
Expand All @@ -51,18 +54,21 @@ const Statistic: React.FC<StatisticProps & ConfigConsumerProps> = props => {
return (
<div className={cls} style={style} onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave}>
{title && <div className={`${prefixCls}-title`}>{title}</div>}
<div style={valueStyle} className={`${prefixCls}-content`}>
{prefix && <span className={`${prefixCls}-content-prefix`}>{prefix}</span>}
{valueRender ? valueRender(valueNode) : valueNode}
{suffix && <span className={`${prefixCls}-content-suffix`}>{suffix}</span>}
</div>
<Skeleton paragraph={false} loading={loading}>
<div style={valueStyle} className={`${prefixCls}-content`}>
{prefix && <span className={`${prefixCls}-content-prefix`}>{prefix}</span>}
{valueRender ? valueRender(valueNode) : valueNode}
{suffix && <span className={`${prefixCls}-content-suffix`}>{suffix}</span>}
</div>
</Skeleton>
</div>
);
};

Statistic.defaultProps = {
decimalSeparator: '.',
groupSeparator: ',',
loading: false,
};

const WrapperStatistic = withConfigConsumer<StatisticProps>({
Expand Down
25 changes: 25 additions & 0 deletions components/statistic/__tests__/__snapshots__/demo.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,31 @@ exports[`renders ./components/statistic/demo/basic.md correctly 1`] = `
</span>
</button>
</div>
<div
class="ant-col ant-col-12"
style="padding-left:8px;padding-right:8px"
>
<div
class="ant-statistic"
>
<div
class="ant-statistic-title"
>
Active Users
</div>
<div
class="ant-skeleton"
>
<div
class="ant-skeleton-content"
>
<h3
class="ant-skeleton-title"
/>
</div>
</div>
</div>
</div>
</div>
`;

Expand Down
12 changes: 12 additions & 0 deletions components/statistic/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ describe('Statistic', () => {
expect(wrapper.render()).toMatchSnapshot();
});

it('loading with skeleton', async () => {
let loading = false;
const wrapper = mount(<Statistic title="Active Users" value={112112} loading={loading} />);
expect(wrapper.find('.ant-skeleton')).toHaveLength(0);
expect(wrapper.find('.ant-statistic-content')).toHaveLength(1);

loading = true;
wrapper.setProps({ loading });
expect(wrapper.find('.ant-skeleton')).toHaveLength(1);
expect(wrapper.find('.ant-statistic-content')).toHaveLength(0);
});

describe('Countdown', () => {
it('render correctly', () => {
const now = moment().add(2, 'd').add(11, 'h').add(28, 'm').add(9, 's').add(3, 'ms');
Expand Down
3 changes: 3 additions & 0 deletions components/statistic/demo/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ ReactDOM.render(
Recharge
</Button>
</Col>
<Col span={12}>
<Statistic title="Active Users" value={112893} loading />
</Col>
</Row>,
mountNode,
);
Expand Down
1 change: 1 addition & 0 deletions components/statistic/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Display statistic number.
| decimalSeparator | The decimal separator | string | `.` | |
| formatter | Customize value display logic | (value) => ReactNode | - | |
| groupSeparator | Group separator | string | `,` | |
| loading | Loading status of Statistic | boolean | false | 4.8.0 |
| precision | The precision of input value | number | - | |
| prefix | The prefix node of value | ReactNode | - | |
| suffix | The suffix node of value | ReactNode | - | |
Expand Down
1 change: 1 addition & 0 deletions components/statistic/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ cover: https://gw.alipayobjects.com/zos/antfincdn/rcBNhLBrKbE/Statistic.svg
| decimalSeparator | 设置小数点 | string | `.` | |
| formatter | 自定义数值展示 | (value) => ReactNode | - | |
| groupSeparator | 设置千分位标识符 | string | `,` | |
| loading | 数值是否加载中 | boolean | false | 4.8.0 |
| precision | 数值精度 | number | - | |
| prefix | 设置数值的前缀 | ReactNode | - | |
| suffix | 设置数值的后缀 | ReactNode | - | |
Expand Down
3 changes: 3 additions & 0 deletions components/statistic/style/index.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
import '../../style/index.less';
import './index.less';

// style dependencies
import '../../skeleton/style';

0 comments on commit b4c6740

Please sign in to comment.