|
| 1 | +import { Statistic } from "antd"; |
| 2 | +import { RecordConstructorToView } from "barda-core"; |
| 3 | +import { Section, sectionNames } from "barda-design"; |
| 4 | +import { BoolControl } from "comps/controls/boolControl"; |
| 5 | +import { NumberControl, RangeControl, StringControl } from "comps/controls/codeControl"; |
| 6 | +import { ButtonEventHandlerControl } from "comps/controls/eventHandlerControl"; |
| 7 | +import { IconControl } from "comps/controls/iconControl"; |
| 8 | +import { styleControl } from "comps/controls/styleControl"; |
| 9 | +import { StatisticCardStyle, StatisticCardStyleType } from "comps/controls/styleControlConstants"; |
| 10 | +import { withDefault } from "comps/generators"; |
| 11 | +import { UICompBuilder } from "comps/generators/uiCompBuilder"; |
| 12 | +import { NameConfig, NameConfigHidden, withExposingConfigs } from "comps/generators/withExposing"; |
| 13 | +import { hiddenPropertyView } from "comps/utils/propertyUtils"; |
| 14 | +import { trans } from "i18n"; |
| 15 | +import React, { Suspense } from "react"; |
| 16 | +import styled from "styled-components"; |
| 17 | +import { hasIcon } from "../utils"; |
| 18 | + |
| 19 | +const StatisticCardWrapper = styled.div<{ |
| 20 | + $style: StatisticCardStyleType; |
| 21 | + $clickable: boolean; |
| 22 | +}>` |
| 23 | + width: 100%; |
| 24 | + height: 100%; |
| 25 | + padding: 16px; |
| 26 | + background: ${(props) => props.$style.background}; |
| 27 | + border-radius: ${(props) => props.$style.radius}; |
| 28 | + border: 1px solid ${(props) => props.$style.border}; |
| 29 | + cursor: ${(props) => (props.$clickable ? "pointer" : "default")}; |
| 30 | + transition: all 0.2s; |
| 31 | +
|
| 32 | + ${(props) => |
| 33 | + props.$clickable && |
| 34 | + ` |
| 35 | + &:hover { |
| 36 | + ${props.$style.hoverBackground ? `background: ${props.$style.hoverBackground} !important;` : ""} |
| 37 | + box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); |
| 38 | + } |
| 39 | + `} |
| 40 | +
|
| 41 | + .statistic-card-content { |
| 42 | + display: flex; |
| 43 | + align-items: flex-start; |
| 44 | + gap: 16px; |
| 45 | + } |
| 46 | +
|
| 47 | + .statistic-card-icon { |
| 48 | + width: 48px; |
| 49 | + height: 48px; |
| 50 | + border-radius: 8px; |
| 51 | + display: flex; |
| 52 | + align-items: center; |
| 53 | + justify-content: center; |
| 54 | + flex-shrink: 0; |
| 55 | + background: ${(props) => props.$style.iconBackground}; |
| 56 | +
|
| 57 | + svg { |
| 58 | + width: 32px !important; |
| 59 | + height: 32px !important; |
| 60 | + color: #fff; |
| 61 | + } |
| 62 | +
|
| 63 | + img { |
| 64 | + width: 32px; |
| 65 | + height: 32px; |
| 66 | + object-fit: contain; |
| 67 | + } |
| 68 | + } |
| 69 | +
|
| 70 | + .statistic-card-info { |
| 71 | + flex: 1; |
| 72 | + min-width: 0; |
| 73 | + } |
| 74 | +
|
| 75 | + .ant-statistic-title { |
| 76 | + font-size: ${(props) => props.$style.titleFontSize_UNIT} !important; |
| 77 | + color: ${(props) => props.$style.titleColor} !important; |
| 78 | + margin-bottom: 8px; |
| 79 | + } |
| 80 | +
|
| 81 | + .ant-statistic-content { |
| 82 | + font-size: ${(props) => props.$style.valueFontSize_UNIT} !important; |
| 83 | + color: ${(props) => props.$style.valueColor} !important; |
| 84 | + } |
| 85 | +`; |
| 86 | + |
| 87 | +// 懒加载 CountUp 组件,只有在启用动画时才加载 |
| 88 | +const CountUp = React.lazy(() => import("react-countup")); |
| 89 | + |
| 90 | +const childrenMap = { |
| 91 | + title: withDefault(StringControl, trans("statisticCard.title")), |
| 92 | + value: withDefault(NumberControl, 0), |
| 93 | + prefix: withDefault(StringControl, ""), |
| 94 | + suffix: withDefault(StringControl, ""), |
| 95 | + precision: RangeControl.closed(0, 20, 0), |
| 96 | + icon: IconControl, |
| 97 | + enableAnimation: withDefault(BoolControl, false), |
| 98 | + onEvent: ButtonEventHandlerControl, |
| 99 | + style: styleControl(StatisticCardStyle), |
| 100 | +}; |
| 101 | + |
| 102 | +// 动画值组件 |
| 103 | +const AnimatedValue = (props: { |
| 104 | + value: number; |
| 105 | + enableAnimation: boolean; |
| 106 | + precision: number; |
| 107 | + valueStyle: React.CSSProperties; |
| 108 | +}) => { |
| 109 | + const formattedValue = props.precision > 0 ? props.value.toFixed(props.precision) : props.value; |
| 110 | + |
| 111 | + if (!props.enableAnimation) { |
| 112 | + return <>{formattedValue}</>; |
| 113 | + } |
| 114 | + |
| 115 | + return ( |
| 116 | + <Suspense fallback={<>{formattedValue}</>}> |
| 117 | + <CountUp |
| 118 | + start={0} |
| 119 | + end={props.value} |
| 120 | + duration={2} |
| 121 | + decimals={props.precision} |
| 122 | + separator="," |
| 123 | + style={props.valueStyle} |
| 124 | + /> |
| 125 | + </Suspense> |
| 126 | + ); |
| 127 | +}; |
| 128 | + |
| 129 | +const StatisticCardView = ( |
| 130 | + props: RecordConstructorToView<typeof childrenMap> & { $hasClickHandler?: boolean } |
| 131 | +) => { |
| 132 | + return ( |
| 133 | + <StatisticCardWrapper |
| 134 | + $style={props.style} |
| 135 | + $clickable={props.$hasClickHandler ?? false} |
| 136 | + onClick={() => { |
| 137 | + props.onEvent?.("click"); |
| 138 | + }} |
| 139 | + > |
| 140 | + <div className="statistic-card-content"> |
| 141 | + <div className="statistic-card-info"> |
| 142 | + <Statistic |
| 143 | + title={props.title} |
| 144 | + value={props.value} |
| 145 | + prefix={props.prefix || undefined} |
| 146 | + suffix={props.suffix || undefined} |
| 147 | + precision={props.precision} |
| 148 | + formatter={(value: string | number) => { |
| 149 | + const numericValue = typeof value === "number" ? value : Number(value) || 0; |
| 150 | + if (props.enableAnimation) { |
| 151 | + return ( |
| 152 | + <AnimatedValue |
| 153 | + value={numericValue} |
| 154 | + enableAnimation={props.enableAnimation} |
| 155 | + precision={props.precision} |
| 156 | + valueStyle={{ color: props.style.valueColor }} |
| 157 | + /> |
| 158 | + ); |
| 159 | + } |
| 160 | + // 应用精度格式化 |
| 161 | + return props.precision > 0 ? numericValue.toFixed(props.precision) : numericValue; |
| 162 | + }} |
| 163 | + valueStyle={{ color: props.style.valueColor }} |
| 164 | + /> |
| 165 | + </div> |
| 166 | + {hasIcon(props.icon) && <div className="statistic-card-icon">{props.icon}</div>} |
| 167 | + </div> |
| 168 | + </StatisticCardWrapper> |
| 169 | + ); |
| 170 | +}; |
| 171 | + |
| 172 | +let StatisticCardBasicComp = (function () { |
| 173 | + return new UICompBuilder( |
| 174 | + childrenMap, |
| 175 | + (props: RecordConstructorToView<typeof childrenMap>, dispatch: any, comp?: any) => { |
| 176 | + // 通过 comp.children 访问原始的 control 对象,判断是否绑定了 click 事件 |
| 177 | + const hasClickHandler = (comp?.children?.onEvent as any)?.isBind?.("click") ?? false; |
| 178 | + return <StatisticCardView {...props} $hasClickHandler={hasClickHandler} />; |
| 179 | + } |
| 180 | + ) |
| 181 | + .setPropertyViewFn((children) => ( |
| 182 | + <> |
| 183 | + <Section name={sectionNames.basic}> |
| 184 | + {children.title.propertyView({ |
| 185 | + label: trans("statisticCard.title"), |
| 186 | + tooltip: trans("statisticCard.titleTooltip"), |
| 187 | + })} |
| 188 | + {children.value.propertyView({ |
| 189 | + label: trans("statisticCard.value"), |
| 190 | + tooltip: trans("statisticCard.valueTooltip"), |
| 191 | + })} |
| 192 | + {children.prefix.propertyView({ |
| 193 | + label: trans("statisticCard.prefix"), |
| 194 | + tooltip: trans("statisticCard.prefixTooltip"), |
| 195 | + })} |
| 196 | + {children.suffix.propertyView({ |
| 197 | + label: trans("statisticCard.suffix"), |
| 198 | + tooltip: trans("statisticCard.suffixTooltip"), |
| 199 | + })} |
| 200 | + {children.precision.propertyView({ |
| 201 | + label: trans("statisticCard.precision"), |
| 202 | + tooltip: trans("statisticCard.precisionTooltip"), |
| 203 | + })} |
| 204 | + {children.icon.propertyView({ |
| 205 | + label: trans("statisticCard.icon"), |
| 206 | + tooltip: trans("statisticCard.iconTooltip"), |
| 207 | + })} |
| 208 | + {children.enableAnimation.propertyView({ |
| 209 | + label: trans("statisticCard.enableAnimation"), |
| 210 | + tooltip: trans("statisticCard.enableAnimationTooltip"), |
| 211 | + })} |
| 212 | + </Section> |
| 213 | + <Section name={sectionNames.interaction}>{children.onEvent.propertyView()}</Section> |
| 214 | + <Section name={sectionNames.layout}>{hiddenPropertyView(children)}</Section> |
| 215 | + <Section name={sectionNames.style}>{children.style.getPropertyView()}</Section> |
| 216 | + </> |
| 217 | + )) |
| 218 | + .build(); |
| 219 | +})(); |
| 220 | + |
| 221 | +export const StatisticCardComp = withExposingConfigs(StatisticCardBasicComp, [ |
| 222 | + NameConfigHidden, |
| 223 | +]); |
0 commit comments