From b8ed920cee9ad57829e83975f639c262e46e78a5 Mon Sep 17 00:00:00 2001 From: baiwusanyu-c <740132583@qq.com> Date: Fri, 22 Dec 2023 17:02:17 +0800 Subject: [PATCH] chore: complete basic function --- components/Statistic/src/index.svelte | 71 ++++++++++++++++++++++++--- components/Statistic/src/types.d.ts | 39 ++++++++++++++- preset/src/shortcuts/index.ts | 12 +++-- preset/src/shortcuts/src/statistic.ts | 8 +++ 4 files changed, 118 insertions(+), 12 deletions(-) create mode 100644 preset/src/shortcuts/src/statistic.ts diff --git a/components/Statistic/src/index.svelte b/components/Statistic/src/index.svelte index abb08d34..80268248 100644 --- a/components/Statistic/src/index.svelte +++ b/components/Statistic/src/index.svelte @@ -2,15 +2,70 @@ import { getPrefixCls } from '@ikun-ui/utils'; import { clsx } from 'clsx'; import type { KStatisticProps } from "./types"; - + import { isFunction, isNumber } from "baiwusanyu-utils"; + + export let decimalSeparator: KStatisticProps["decimalSeparator"] = '.'; + export let groupSeparator: KStatisticProps["groupSeparator"] = ','; + export let precision: KStatisticProps["precision"] = 0; + export let formatter: KStatisticProps["formatter"] = undefined; + export let value: KStatisticProps["value"] = 0; + export let prefix: KStatisticProps["prefix"] = undefined; + export let suffix: KStatisticProps["suffix"] = undefined; + export let title: KStatisticProps["title"] = undefined; + export let valueStyle: KStatisticProps["valueStyle"] = undefined; export let cls: KStatisticProps["cls"] = undefined; export let attrs: KStatisticProps["attrs"] = {}; - - const prefixCls = getPrefixCls('statistic'); - $: cnames = clsx(prefixCls, { - [`${prefixCls}--base`]: true - }, cls); + + $: displayValue = () => { + if (isFunction(formatter)) return formatter!(value) + + if (!isNumber(value)) return value + + let [integer, decimal = ''] = String(value).split('.') + decimal = decimal + .padEnd(precision, '0') + .slice(0, precision > 0 ? precision : 0) + integer = integer.replace(/\B(?=(\d{3})+(?!\d))/g, groupSeparator) + return [integer, decimal].join(decimal ? decimalSeparator : '') + } + + const namespaceCls = getPrefixCls('statistic'); + $: cnames = clsx(namespaceCls, cls); + $: headCls = clsx(`${namespaceCls}__head`); + $: contentCls =clsx(`${namespaceCls}__content`); + $: prefixCls = clsx(`${namespaceCls}__prefix`); + $: valueCls = clsx(`${namespaceCls}__value`); + $: suffixCls = clsx(`${namespaceCls}__suffix`); -
- \ No newline at end of file +