2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2024-04-08 14:04:08 +08:00
|
|
|
import classNames from 'classnames';
|
2024-01-26 15:27:02 +08:00
|
|
|
import pickAttrs from 'rc-util/lib/pickAttrs';
|
|
|
|
|
2024-04-08 14:04:08 +08:00
|
|
|
import type { HTMLAriaDataAttributes } from '../_util/aria-data-attrs';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { ConfigConsumerProps } from '../config-provider';
|
2022-12-25 22:54:56 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2020-10-23 10:09:31 +08:00
|
|
|
import Skeleton from '../skeleton';
|
2019-01-09 20:38:09 +08:00
|
|
|
import StatisticNumber from './Number';
|
2022-03-16 12:02:52 +08:00
|
|
|
import useStyle from './style';
|
2023-06-05 10:48:27 +08:00
|
|
|
import type { FormatConfig, valueType } from './utils';
|
2019-01-09 20:38:09 +08:00
|
|
|
|
2024-01-26 15:27:02 +08:00
|
|
|
interface StatisticReactProps extends FormatConfig {
|
2019-01-09 20:38:09 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2019-01-09 20:38:09 +08:00
|
|
|
style?: React.CSSProperties;
|
|
|
|
value?: valueType;
|
|
|
|
valueStyle?: React.CSSProperties;
|
|
|
|
valueRender?: (node: React.ReactNode) => React.ReactNode;
|
|
|
|
title?: React.ReactNode;
|
|
|
|
prefix?: React.ReactNode;
|
|
|
|
suffix?: React.ReactNode;
|
2020-10-23 10:09:31 +08:00
|
|
|
loading?: boolean;
|
2020-06-06 15:25:47 +08:00
|
|
|
onMouseEnter?: React.MouseEventHandler<HTMLDivElement>;
|
|
|
|
onMouseLeave?: React.MouseEventHandler<HTMLDivElement>;
|
2019-01-09 20:38:09 +08:00
|
|
|
}
|
|
|
|
|
2024-01-26 15:27:02 +08:00
|
|
|
export type StatisticProps = HTMLAriaDataAttributes & StatisticReactProps;
|
|
|
|
|
2024-06-04 18:43:38 +08:00
|
|
|
const Statistic: React.FC<StatisticProps> = (props) => {
|
2019-01-09 20:38:09 +08:00
|
|
|
const {
|
2022-12-25 22:54:56 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
2019-01-09 20:38:09 +08:00
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2019-01-09 20:38:09 +08:00
|
|
|
style,
|
|
|
|
valueStyle,
|
|
|
|
value = 0,
|
|
|
|
title,
|
|
|
|
valueRender,
|
|
|
|
prefix,
|
|
|
|
suffix,
|
2022-09-30 17:44:24 +08:00
|
|
|
loading = false,
|
2024-01-26 15:27:02 +08:00
|
|
|
/* --- FormatConfig starts --- */
|
|
|
|
formatter,
|
|
|
|
precision,
|
2022-09-30 17:44:24 +08:00
|
|
|
decimalSeparator = '.',
|
|
|
|
groupSeparator = ',',
|
2024-01-26 15:27:02 +08:00
|
|
|
/* --- FormatConfig starts --- */
|
|
|
|
onMouseEnter,
|
|
|
|
onMouseLeave,
|
|
|
|
...rest
|
2019-01-09 20:38:09 +08:00
|
|
|
} = props;
|
2022-12-25 22:54:56 +08:00
|
|
|
|
2023-07-06 21:54:54 +08:00
|
|
|
const { getPrefixCls, direction, statistic } =
|
|
|
|
React.useContext<ConfigConsumerProps>(ConfigContext);
|
2022-12-25 22:54:56 +08:00
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('statistic', customizePrefixCls);
|
|
|
|
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
|
2022-12-25 22:54:56 +08:00
|
|
|
|
|
|
|
const valueNode: React.ReactNode = (
|
2022-09-30 17:44:24 +08:00
|
|
|
<StatisticNumber
|
|
|
|
decimalSeparator={decimalSeparator}
|
|
|
|
groupSeparator={groupSeparator}
|
2022-12-25 22:54:56 +08:00
|
|
|
prefixCls={prefixCls}
|
2024-01-26 15:27:02 +08:00
|
|
|
formatter={formatter}
|
|
|
|
precision={precision}
|
2022-09-30 17:44:24 +08:00
|
|
|
value={value}
|
|
|
|
/>
|
|
|
|
);
|
2022-12-25 22:54:56 +08:00
|
|
|
|
2020-09-06 13:07:39 +08:00
|
|
|
const cls = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
2023-07-06 21:54:54 +08:00
|
|
|
statistic?.className,
|
2020-09-06 13:07:39 +08:00
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-03-16 12:02:52 +08:00
|
|
|
hashId,
|
2023-12-14 14:58:53 +08:00
|
|
|
cssVarCls,
|
2020-09-06 13:07:39 +08:00
|
|
|
);
|
2022-12-25 22:54:56 +08:00
|
|
|
|
2024-01-26 15:27:02 +08:00
|
|
|
const restProps = pickAttrs(rest, { aria: true, data: true });
|
|
|
|
|
2023-11-14 09:42:37 +08:00
|
|
|
return wrapCSSVar(
|
2023-07-06 21:54:54 +08:00
|
|
|
<div
|
2024-01-26 15:27:02 +08:00
|
|
|
{...restProps}
|
2023-07-06 21:54:54 +08:00
|
|
|
className={cls}
|
|
|
|
style={{ ...statistic?.style, ...style }}
|
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
onMouseLeave={onMouseLeave}
|
|
|
|
>
|
2019-01-09 20:38:09 +08:00
|
|
|
{title && <div className={`${prefixCls}-title`}>{title}</div>}
|
2022-06-02 11:36:03 +08:00
|
|
|
<Skeleton paragraph={false} loading={loading} className={`${prefixCls}-skeleton`}>
|
2020-10-23 10:09:31 +08:00
|
|
|
<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>
|
2022-03-16 12:02:52 +08:00
|
|
|
</div>,
|
2019-01-09 20:38:09 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Statistic.displayName = 'Statistic';
|
|
|
|
}
|
|
|
|
|
2022-12-25 22:54:56 +08:00
|
|
|
export default Statistic;
|