ant-design-vue/components/statistic/Statistic.tsx

72 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-10-17 03:51:17 +00:00
import { defineComponent, inject, PropType } from 'vue';
import PropTypes from '../_util/vue-types';
2020-10-17 03:51:17 +00:00
import { getComponent } from '../_util/props-util';
import initDefaultProps from '../_util/props-util/initDefaultProps';
import { defaultConfigProvider } from '../config-provider';
import StatisticNumber from './Number';
2020-10-17 03:51:17 +00:00
import { countdownValueType } from './utils';
export const StatisticProps = {
2020-10-17 03:51:17 +00:00
prefixCls: PropTypes.string,
decimalSeparator: PropTypes.string,
groupSeparator: PropTypes.string,
format: PropTypes.string,
value: {
type: [String, Number, Object] as PropType<countdownValueType>,
},
valueStyle: PropTypes.style,
valueRender: PropTypes.any,
formatter: PropTypes.any,
precision: PropTypes.number,
prefix: PropTypes.VNodeChild,
suffix: PropTypes.VNodeChild,
title: PropTypes.VNodeChild,
2020-10-17 03:51:17 +00:00
onFinish: PropTypes.func,
};
2020-10-17 03:51:17 +00:00
export default defineComponent({
name: 'AStatistic',
2020-10-17 03:51:17 +00:00
props: initDefaultProps(StatisticProps, {
decimalSeparator: '.',
groupSeparator: ',',
}),
setup() {
return {
configProvider: inject('configProvider', defaultConfigProvider),
};
},
2020-10-17 03:51:17 +00:00
render() {
const { prefixCls: customizePrefixCls, value = 0, valueStyle, valueRender } = this.$props;
const { getPrefixCls } = this.configProvider;
const prefixCls = getPrefixCls('statistic', customizePrefixCls);
const title = getComponent(this, 'title');
2020-10-17 03:51:17 +00:00
let prefix = getComponent(this, 'prefix');
let suffix = getComponent(this, 'suffix');
const formatter = getComponent(this, 'formatter', {}, false);
const props = {
...this.$props,
prefixCls,
value,
formatter,
};
let valueNode = <StatisticNumber {...props} />;
if (valueRender) {
valueNode = valueRender(valueNode);
}
return (
<div class={prefixCls}>
{title && <div class={`${prefixCls}-title`}>{title}</div>}
<div style={valueStyle} class={`${prefixCls}-content`}>
{prefix && <span class={`${prefixCls}-content-prefix`}>{prefix}</span>}
2020-10-17 03:51:17 +00:00
{valueNode}
{suffix && <span class={`${prefixCls}-content-suffix`}>{suffix}</span>}
</div>
</div>
);
},
});