|
1 | 1 | import padEnd from 'lodash/padEnd';
|
| 2 | +import { createVNode } from 'vue'; |
2 | 3 |
|
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; |
16 | 7 |
|
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; |
20 | 18 | } 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] || ''; |
31 | 22 |
|
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 | + } |
40 | 27 |
|
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}`; |
52 | 30 | }
|
| 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 | + ]; |
53 | 43 | }
|
| 44 | + } |
54 | 45 |
|
55 |
| - return <span class={`${prefixCls}-content-value`}>{valueNode}</span>; |
56 |
| - }, |
| 46 | + return <span class={`${prefixCls}-content-value`}>{valueNode}</span>; |
57 | 47 | };
|
| 48 | + |
| 49 | +const name = 'AStatisticNumber'; |
| 50 | +export { name }; |
0 commit comments