chowa
Version:
UI component library based on React
72 lines (71 loc) • 2.54 kB
JavaScript
/**
* @license chowa v1.1.3
*
* Copyright (c) Chowa Techonlogies Co.,Ltd.(http://www.chowa.cn).
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const React = require("react");
const PropTypes = require("prop-types");
const utils_1 = require("../utils");
const base_statistic_1 = require("./base-statistic");
const countdown_1 = require("./countdown");
function numberFormatter({ value, precision, decimalSeparator, groupSeparator }) {
const [integer, decimal] = value.toString().split('.');
const integerGroup = [];
let pos = integer.length - 1;
for (;;) {
const group = [];
for (let j = 0; j < 3; j++) {
const char = integer.charAt(pos - j);
if (!utils_1.isExist(char)) {
break;
}
group.unshift(char);
}
if (group.length > 0) {
integerGroup.unshift(group.join(''));
}
if (group.length < 3) {
break;
}
pos -= 3;
}
const integerStr = integerGroup.join(groupSeparator);
return [
React.createElement("span", { key: 'integer', className: utils_1.preClass('statistic-value-integer') }, integerStr),
utils_1.isExist(decimal)
? React.createElement("span", { key: 'decimal', className: utils_1.preClass('statistic-value-decimal') },
decimalSeparator,
decimal.substring(0, precision))
: null
];
}
const Statistic = (props) => {
const { value, formatter } = props;
const valueNode = utils_1.isExist(formatter) ? formatter(value) : numberFormatter(props);
return (React.createElement(base_statistic_1.default, Object.assign({ valueNode: valueNode }, utils_1.omitProps(props, ['value', 'precision', 'decimalSeparator', 'groupSeparator', 'formatter']))));
};
Statistic.propTypes = {
className: PropTypes.string,
style: PropTypes.object,
title: PropTypes.node,
value: PropTypes.number.isRequired,
valueStyle: PropTypes.object,
prefix: PropTypes.node,
suffix: PropTypes.node,
precision: PropTypes.number,
decimalSeparator: PropTypes.string,
groupSeparator: PropTypes.string,
formatter: PropTypes.func
};
Statistic.defaultProps = {
decimalSeparator: '.',
groupSeparator: ',',
precision: 2
};
Statistic.Countdown = countdown_1.default;
exports.default = Statistic;