Skip to content

Commit 6fb936d

Browse files
ununiantangjinzhou
andauthored
feat: update Statistic (#2386)
Co-authored-by: tangjinzhou <[email protected]>
1 parent 1dd4388 commit 6fb936d

File tree

5 files changed

+59
-59
lines changed

5 files changed

+59
-59
lines changed

components/statistic/Countdown.jsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as moment from 'moment';
22
import interopDefault from '../_util/interopDefault';
3-
import { initDefaultProps, getListeners } from '../_util/props-util';
3+
import { initDefaultProps } from '../_util/props-util';
44
import Statistic, { StatisticProps } from './Statistic';
55
import { formatCountdown } from './utils';
66

@@ -82,7 +82,6 @@ export default {
8282
valueRender: this.valueRenderHtml,
8383
formatter: this.formatCountdown,
8484
},
85-
on: getListeners(this),
8685
}}
8786
/>
8887
);

components/statistic/Number.jsx

+40-47
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,50 @@
11
import padEnd from 'lodash/padEnd';
2+
import { createVNode } from 'vue';
23

3-
export default {
4-
name: 'AStatisticNumber',
5-
functional: true,
6-
render(h, context) {
7-
const {
8-
value,
9-
formatter,
10-
precision,
11-
decimalSeparator,
12-
groupSeparator = '',
13-
prefixCls,
14-
} = context.props;
15-
let valueNode;
4+
export default (_, { attrs }) => {
5+
const { value, formatter, precision, decimalSeparator, groupSeparator = '', prefixCls } = attrs;
6+
let valueNode;
167

17-
if (typeof formatter === 'function') {
18-
// Customize formatter
19-
valueNode = formatter({ value, h });
8+
if (typeof formatter === 'function') {
9+
// Customize formatter
10+
valueNode = formatter({ value, h: createVNode });
11+
} else {
12+
// Internal formatter
13+
const val = String(value);
14+
const cells = val.match(/^(-?)(\d*)(\.(\d+))?$/);
15+
// Process if illegal number
16+
if (!cells) {
17+
valueNode = val;
2018
} else {
21-
// Internal formatter
22-
const val = String(value);
23-
const cells = val.match(/^(-?)(\d*)(\.(\d+))?$/);
24-
// Process if illegal number
25-
if (!cells) {
26-
valueNode = val;
27-
} else {
28-
const negative = cells[1];
29-
let int = cells[2] || '0';
30-
let decimal = cells[4] || '';
19+
const negative = cells[1];
20+
let int = cells[2] || '0';
21+
let decimal = cells[4] || '';
3122

32-
int = int.replace(/\B(?=(\d{3})+(?!\d))/g, groupSeparator);
33-
if (typeof precision === 'number') {
34-
decimal = padEnd(decimal, precision, '0').slice(0, precision);
35-
}
36-
37-
if (decimal) {
38-
decimal = `${decimalSeparator}${decimal}`;
39-
}
23+
int = int.replace(/\B(?=(\d{3})+(?!\d))/g, groupSeparator);
24+
if (typeof precision === 'number') {
25+
decimal = padEnd(decimal, precision, '0').slice(0, precision);
26+
}
4027

41-
valueNode = [
42-
<span key="int" class={`${prefixCls}-content-value-int`}>
43-
{negative}
44-
{int}
45-
</span>,
46-
decimal && (
47-
<span key="decimal" class={`${prefixCls}-content-value-decimal`}>
48-
{decimal}
49-
</span>
50-
),
51-
];
28+
if (decimal) {
29+
decimal = `${decimalSeparator}${decimal}`;
5230
}
31+
32+
valueNode = [
33+
<span key="int" class={`${prefixCls}-content-value-int`}>
34+
{negative}
35+
{int}
36+
</span>,
37+
decimal && (
38+
<span key="decimal" class={`${prefixCls}-content-value-decimal`}>
39+
{decimal}
40+
</span>
41+
),
42+
];
5343
}
44+
}
5445

55-
return <span class={`${prefixCls}-content-value`}>{valueNode}</span>;
56-
},
46+
return <span class={`${prefixCls}-content-value`}>{valueNode}</span>;
5747
};
48+
49+
const name = 'AStatisticNumber';
50+
export { name };

components/statistic/Statistic.jsx

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { inject } from 'vue';
12
import PropTypes from '../_util/vue-types';
2-
import { getComponentFromProp, initDefaultProps } from '../_util/props-util';
3+
import { getComponent, initDefaultProps } from '../_util/props-util';
34
import { ConfigConsumerProps } from '../config-provider';
45
import StatisticNumber from './Number';
56

@@ -28,15 +29,21 @@ export default {
2829
configProvider: { default: () => ConfigConsumerProps },
2930
},
3031

32+
setup() {
33+
return {
34+
configProvider: inject('configProvider', ConfigConsumerProps),
35+
};
36+
},
37+
3138
render() {
3239
const { prefixCls: customizePrefixCls, value = 0, valueStyle, valueRender } = this.$props;
3340
const getPrefixCls = this.configProvider.getPrefixCls;
3441
const prefixCls = getPrefixCls('statistic', customizePrefixCls);
3542

36-
const title = getComponentFromProp(this, 'title');
37-
let prefix = getComponentFromProp(this, 'prefix');
38-
let suffix = getComponentFromProp(this, 'suffix');
39-
const formatter = getComponentFromProp(this, 'formatter', {}, false);
43+
const title = getComponent(this, 'title');
44+
let prefix = getComponent(this, 'prefix');
45+
let suffix = getComponent(this, 'suffix');
46+
const formatter = getComponent(this, 'formatter', {}, false);
4047
let valueNode = (
4148
<StatisticNumber {...{ props: { ...this.$props, prefixCls, value, formatter } }} />
4249
);

components/statistic/index.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import Statistic from './Statistic';
22
import Countdown from './Countdown';
3-
import Base from '../base';
43

54
Statistic.Countdown = Countdown;
65
/* istanbul ignore next */
7-
Statistic.install = function(Vue) {
8-
Vue.use(Base);
9-
Vue.component(Statistic.name, Statistic);
10-
Vue.component(Statistic.Countdown.name, Statistic.Countdown);
6+
Statistic.install = function(app) {
7+
app.component(Statistic.name, Statistic);
8+
app.component(Statistic.Countdown.name, Statistic.Countdown);
119
};
1210

1311
export default Statistic;

examples/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import PageHeader from 'ant-design-vue/page-header';
1818
import Skeleton from 'ant-design-vue/skeleton';
1919
import Empty from 'ant-design-vue/empty';
2020
import Timeline from 'ant-design-vue/timeline';
21+
import Statistic from 'ant-design-vue/statistic';
2122
import Checkbox from 'ant-design-vue/checkbox';
2223
import Col from 'ant-design-vue/col';
2324
import Row from 'ant-design-vue/row';
@@ -34,6 +35,7 @@ import Menu from 'ant-design-vue/menu';
3435
import Mentions from 'ant-design-vue/mentions';
3536
import Dropdown from 'ant-design-vue/dropdown';
3637
import Steps from 'ant-design-vue/steps';
38+
3739
import 'ant-design-vue/style.js';
3840

3941
const basic = {
@@ -68,6 +70,7 @@ app
6870
.use(Empty)
6971
.use(Checkbox)
7072
.use(Timeline)
73+
.use(Statistic)
7174
.use(Col)
7275
.use(Row)
7376
.use(Tooltip)

0 commit comments

Comments
 (0)