@ohos-ports/react-native-animated-rolling-numbers 0.0.0-stage → 2.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/example/example.md +233 -0
- package/lib/components/AnimatedDigit.d.ts +54 -0
- package/lib/components/AnimatedDigit.js +142 -0
- package/lib/components/AnimatedDigit.js.map +1 -0
- package/lib/components/AnimatedRollingNumber.d.ts +72 -0
- package/lib/components/AnimatedRollingNumber.js +110 -0
- package/lib/components/AnimatedRollingNumber.js.map +1 -0
- package/lib/constants/index.d.ts +5 -0
- package/lib/constants/index.js +26 -0
- package/lib/constants/index.js.map +1 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +21 -0
- package/lib/index.js.map +1 -0
- package/lib/utils/index.d.ts +11 -0
- package/lib/utils/index.js +52 -0
- package/lib/utils/index.js.map +1 -0
- package/package.json +38 -4
- package/readme.md +211 -0
- package/src/components/AnimatedDigit.tsx +241 -0
- package/src/components/AnimatedRollingNumber.tsx +216 -0
- package/src/constants/index.ts +24 -0
- package/src/index.ts +5 -0
- package/src/utils/index.ts +55 -0
- package/tsconfig.json +16 -0
- package/README.md +0 -3
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import React, { useCallback, useMemo, useState } from "react";
|
|
2
|
+
import {
|
|
3
|
+
StyleSheet,
|
|
4
|
+
type LayoutChangeEvent,
|
|
5
|
+
type StyleProp,
|
|
6
|
+
type ViewStyle,
|
|
7
|
+
type TextStyle,
|
|
8
|
+
View,
|
|
9
|
+
Text,
|
|
10
|
+
I18nManager,
|
|
11
|
+
} from "@ohos-ports/react-native";
|
|
12
|
+
import Animated, {
|
|
13
|
+
type AnimationCallback,
|
|
14
|
+
type EasingFunction,
|
|
15
|
+
type EasingFunctionFactory,
|
|
16
|
+
LinearTransition,
|
|
17
|
+
ReduceMotion,
|
|
18
|
+
} from "@ohos-ports/react-native-reanimated";
|
|
19
|
+
import { AnimatedDigit, type DigitVariant } from "./AnimatedDigit";
|
|
20
|
+
import { formatCompactNumber } from "../utils";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Properties for the AnimatedRollingNumber component.
|
|
24
|
+
*/
|
|
25
|
+
export interface AnimatedRollingNumberProps
|
|
26
|
+
extends Omit<React.ComponentProps<typeof Animated.View>, "style"> {
|
|
27
|
+
/** The numeric value to be displayed and animated. */
|
|
28
|
+
value: number;
|
|
29
|
+
/** Whether to show the minus sign. */
|
|
30
|
+
showMinusSign?: boolean;
|
|
31
|
+
/** Whether to show the plus sign. */
|
|
32
|
+
showPlusSign?: boolean;
|
|
33
|
+
/** Number of decimal places to show in normal notation. */
|
|
34
|
+
toFixed?: number;
|
|
35
|
+
/** Whether to include commas in the number formatting (e.g., 1,000). */
|
|
36
|
+
useGrouping?: boolean;
|
|
37
|
+
/** Custom formatted text to display instead of the numeric value. */
|
|
38
|
+
formattedText?: string;
|
|
39
|
+
/** The locale to use for number formatting. Defaults to "en-US". */
|
|
40
|
+
locale?: Intl.LocalesArgument;
|
|
41
|
+
/** Whether to enable compact notation (e.g., 1K, 1M). */
|
|
42
|
+
enableCompactNotation?: boolean;
|
|
43
|
+
/** Number of decimal places to show in compact notation. */
|
|
44
|
+
compactToFixed?: number;
|
|
45
|
+
/** If true, only applies decimal fixing for compact notation. only for the notation that has M/K/B/T. in the end */
|
|
46
|
+
fixedOnlyForCompact?: boolean;
|
|
47
|
+
/** Style for the container wrapping the digits. */
|
|
48
|
+
containerStyle?: StyleProp<ViewStyle>;
|
|
49
|
+
/** Style for the container wrapping the animated text. */
|
|
50
|
+
digitContainerStyle?: StyleProp<ViewStyle>;
|
|
51
|
+
/** Props for the primary text component used in the component. */
|
|
52
|
+
textProps?: Omit<React.ComponentProps<typeof Text>, "style">;
|
|
53
|
+
/** Props for the number text components. */
|
|
54
|
+
numberTextProps?: Omit<React.ComponentProps<typeof Text>, "style">;
|
|
55
|
+
/** Props for the comma text component, if used. */
|
|
56
|
+
commaTextProps?: Omit<React.ComponentProps<typeof Text>, "style">;
|
|
57
|
+
/** Props for the dot (decimal point) text component, if used. */
|
|
58
|
+
dotTextProps?: Omit<React.ComponentProps<typeof Text>, "style">;
|
|
59
|
+
/** Props for the compact notation text components (K, M, B, T). */
|
|
60
|
+
compactNotationTextProps?: Omit<React.ComponentProps<typeof Text>, "style">;
|
|
61
|
+
/** Props for the sign text component, if used. */
|
|
62
|
+
signTextProps?: Omit<React.ComponentProps<typeof Text>, "style">;
|
|
63
|
+
/** Style for the primary text component. */
|
|
64
|
+
textStyle?: StyleProp<TextStyle>;
|
|
65
|
+
/** Style for the number text components. */
|
|
66
|
+
numberStyle?: StyleProp<TextStyle>;
|
|
67
|
+
/** Style for the comma text component, if used. */
|
|
68
|
+
commaStyle?: StyleProp<TextStyle>;
|
|
69
|
+
/** Style for the dot (decimal point) text component, if used. */
|
|
70
|
+
dotStyle?: StyleProp<TextStyle>;
|
|
71
|
+
/** Style for the compact notation text components (K, M, B, T). */
|
|
72
|
+
compactNotationStyle?: StyleProp<TextStyle>;
|
|
73
|
+
/** Style for the sign text component, if used. */
|
|
74
|
+
signStyle?: StyleProp<TextStyle>;
|
|
75
|
+
/** Configuration for the animation moving the digits up and down. Defaults to a bounce animation with 500ms duration. */
|
|
76
|
+
spinningAnimationConfig?: {
|
|
77
|
+
duration?: number;
|
|
78
|
+
reduceMotion?: ReduceMotion;
|
|
79
|
+
easing?: EasingFunction | EasingFunctionFactory;
|
|
80
|
+
};
|
|
81
|
+
/** Callback function invoked when the animation completes. */
|
|
82
|
+
animationCallback?: AnimationCallback;
|
|
83
|
+
/** Custom function to animate the value change. Defaults to a bounce animation. */
|
|
84
|
+
animateToNewValue?: (newValue: number, variant?: DigitVariant) => number;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* AnimatedDigits is a React component that formats and animates numeric values,
|
|
89
|
+
* optionally using compact notation (K, M, B, T) and custom styling.
|
|
90
|
+
*/
|
|
91
|
+
export const AnimatedRollingNumber: React.FC<AnimatedRollingNumberProps> = ({
|
|
92
|
+
value,
|
|
93
|
+
showMinusSign = true,
|
|
94
|
+
showPlusSign = false,
|
|
95
|
+
toFixed,
|
|
96
|
+
useGrouping = false,
|
|
97
|
+
formattedText,
|
|
98
|
+
locale = "en-US",
|
|
99
|
+
enableCompactNotation,
|
|
100
|
+
compactToFixed,
|
|
101
|
+
containerStyle,
|
|
102
|
+
fixedOnlyForCompact = true,
|
|
103
|
+
digitContainerStyle,
|
|
104
|
+
numberTextProps,
|
|
105
|
+
commaTextProps,
|
|
106
|
+
dotTextProps,
|
|
107
|
+
compactNotationTextProps,
|
|
108
|
+
signTextProps,
|
|
109
|
+
textStyle,
|
|
110
|
+
numberStyle,
|
|
111
|
+
commaStyle,
|
|
112
|
+
dotStyle,
|
|
113
|
+
compactNotationStyle,
|
|
114
|
+
signStyle,
|
|
115
|
+
textProps,
|
|
116
|
+
spinningAnimationConfig,
|
|
117
|
+
animationCallback,
|
|
118
|
+
animateToNewValue,
|
|
119
|
+
...rest
|
|
120
|
+
}) => {
|
|
121
|
+
const [height, setHeight] = useState(0);
|
|
122
|
+
const numberValue = value < 0 && !showMinusSign ? Math.abs(value) : value;
|
|
123
|
+
const formattedNumber = useMemo(
|
|
124
|
+
() =>
|
|
125
|
+
formattedText ||
|
|
126
|
+
(enableCompactNotation &&
|
|
127
|
+
formatCompactNumber(
|
|
128
|
+
numberValue,
|
|
129
|
+
compactToFixed || toFixed,
|
|
130
|
+
fixedOnlyForCompact,
|
|
131
|
+
useGrouping,
|
|
132
|
+
locale
|
|
133
|
+
)) ||
|
|
134
|
+
numberValue.toLocaleString(locale, {
|
|
135
|
+
useGrouping,
|
|
136
|
+
minimumFractionDigits: toFixed,
|
|
137
|
+
maximumFractionDigits: toFixed,
|
|
138
|
+
}),
|
|
139
|
+
[
|
|
140
|
+
formattedText,
|
|
141
|
+
useGrouping,
|
|
142
|
+
value,
|
|
143
|
+
compactToFixed,
|
|
144
|
+
enableCompactNotation,
|
|
145
|
+
showMinusSign,
|
|
146
|
+
showPlusSign,
|
|
147
|
+
fixedOnlyForCompact,
|
|
148
|
+
locale,
|
|
149
|
+
toFixed,
|
|
150
|
+
]
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
const number = useMemo(() => {
|
|
154
|
+
const numberWithSign = (
|
|
155
|
+
(showPlusSign && value >= 0 ? "+" : "") + formattedNumber
|
|
156
|
+
).split("");
|
|
157
|
+
if (I18nManager.isRTL) {
|
|
158
|
+
return numberWithSign.reverse();
|
|
159
|
+
}
|
|
160
|
+
return numberWithSign;
|
|
161
|
+
}, [formattedNumber, value, showPlusSign]);
|
|
162
|
+
|
|
163
|
+
const handleLayout = useCallback(
|
|
164
|
+
(e: LayoutChangeEvent) => {
|
|
165
|
+
if (e.nativeEvent.layout.height !== height) {
|
|
166
|
+
setHeight(e.nativeEvent.layout.height);
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
[height]
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
return (
|
|
173
|
+
<Animated.View
|
|
174
|
+
layout={LinearTransition}
|
|
175
|
+
style={[styles.container, containerStyle]}
|
|
176
|
+
{...rest}
|
|
177
|
+
>
|
|
178
|
+
<View style={styles.innerContainer} onLayout={handleLayout}>
|
|
179
|
+
{number.map((char, index) => (
|
|
180
|
+
<AnimatedDigit
|
|
181
|
+
key={formattedNumber.length - index - 1}
|
|
182
|
+
value={char}
|
|
183
|
+
height={height}
|
|
184
|
+
containerStyle={digitContainerStyle}
|
|
185
|
+
textStyle={textStyle}
|
|
186
|
+
numberTextProps={numberTextProps}
|
|
187
|
+
commaTextProps={commaTextProps}
|
|
188
|
+
dotTextProps={dotTextProps}
|
|
189
|
+
compactNotationTextProps={compactNotationTextProps}
|
|
190
|
+
signTextProps={signTextProps}
|
|
191
|
+
numberStyle={numberStyle}
|
|
192
|
+
commaStyle={commaStyle}
|
|
193
|
+
dotStyle={dotStyle}
|
|
194
|
+
compactNotationStyle={compactNotationStyle}
|
|
195
|
+
signStyle={signStyle}
|
|
196
|
+
spinningAnimationConfig={spinningAnimationConfig}
|
|
197
|
+
animationCallback={animationCallback}
|
|
198
|
+
animateToNewValue={animateToNewValue}
|
|
199
|
+
/>
|
|
200
|
+
))}
|
|
201
|
+
</View>
|
|
202
|
+
</Animated.View>
|
|
203
|
+
);
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const styles = StyleSheet.create({
|
|
207
|
+
container: {
|
|
208
|
+
overflow: "hidden",
|
|
209
|
+
alignItems: "center",
|
|
210
|
+
justifyContent: "center",
|
|
211
|
+
},
|
|
212
|
+
innerContainer: {
|
|
213
|
+
flexDirection: "row",
|
|
214
|
+
alignItems: "center",
|
|
215
|
+
},
|
|
216
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export const NUMBER_ARRAY = new Array(10).fill(0);
|
|
2
|
+
export const COMPACT_NOTATIONS = ["K", "M", "B", "T"];
|
|
3
|
+
export const DIGIT_VARIANTS: {
|
|
4
|
+
[key: string]: "dot" | "comma" | "sign" | "number" | "compact";
|
|
5
|
+
} = {
|
|
6
|
+
".": "dot",
|
|
7
|
+
",": "comma",
|
|
8
|
+
"-": "sign",
|
|
9
|
+
"+": "sign",
|
|
10
|
+
"0": "number",
|
|
11
|
+
"1": "number",
|
|
12
|
+
"2": "number",
|
|
13
|
+
"3": "number",
|
|
14
|
+
"4": "number",
|
|
15
|
+
"5": "number",
|
|
16
|
+
"6": "number",
|
|
17
|
+
"7": "number",
|
|
18
|
+
"8": "number",
|
|
19
|
+
"9": "number",
|
|
20
|
+
K: "compact",
|
|
21
|
+
M: "compact",
|
|
22
|
+
B: "compact",
|
|
23
|
+
T: "compact",
|
|
24
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a number into a compact string representation (e.g., 1K, 1.5M).
|
|
3
|
+
*
|
|
4
|
+
* @param value - The numeric value to format.
|
|
5
|
+
* @param fixedDecimal - Number of decimal places to show in compact notation. Defaults to 1.
|
|
6
|
+
* @param fixedOnlyForCompact - If true, only applies decimal fixing for compact notation. Defaults to false.
|
|
7
|
+
* @param useGrouping - Whether to include separator for thousands separators .
|
|
8
|
+
* @param locales - The locale to use for number formatting. Defaults to "en-US".
|
|
9
|
+
* @returns The formatted string representation of the number.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export function formatCompactNumber(
|
|
13
|
+
value: number,
|
|
14
|
+
fixedDecimal: number = 1,
|
|
15
|
+
fixedOnlyForCompact: boolean = false,
|
|
16
|
+
useGrouping: boolean = false,
|
|
17
|
+
locales: Intl.LocalesArgument = "en-US"
|
|
18
|
+
): string {
|
|
19
|
+
const absValue = Math.abs(value);
|
|
20
|
+
if (absValue >= 1_000_000_000_000) {
|
|
21
|
+
return (
|
|
22
|
+
(value / 1_000_000_000_000).toLocaleString(locales, {
|
|
23
|
+
maximumFractionDigits: fixedDecimal,
|
|
24
|
+
minimumFractionDigits: fixedDecimal,
|
|
25
|
+
useGrouping,
|
|
26
|
+
}) + "T"
|
|
27
|
+
);
|
|
28
|
+
} else if (absValue >= 1_000_000_000) {
|
|
29
|
+
return (
|
|
30
|
+
(value / 1_000_000_000).toLocaleString(locales, {
|
|
31
|
+
maximumFractionDigits: fixedDecimal,
|
|
32
|
+
minimumFractionDigits: fixedDecimal,
|
|
33
|
+
useGrouping,
|
|
34
|
+
}) + "B"
|
|
35
|
+
);
|
|
36
|
+
} else if (absValue >= 1_000_000) {
|
|
37
|
+
return (
|
|
38
|
+
(value / 1_000_000).toLocaleString(locales, {
|
|
39
|
+
maximumFractionDigits: fixedDecimal,
|
|
40
|
+
minimumFractionDigits: fixedDecimal,
|
|
41
|
+
useGrouping,
|
|
42
|
+
}) + "M"
|
|
43
|
+
);
|
|
44
|
+
} else if (absValue >= 1_000) {
|
|
45
|
+
return (
|
|
46
|
+
(value / 1_000).toLocaleString(locales, {
|
|
47
|
+
maximumFractionDigits: fixedDecimal,
|
|
48
|
+
minimumFractionDigits: fixedDecimal,
|
|
49
|
+
useGrouping,
|
|
50
|
+
}) + "K"
|
|
51
|
+
);
|
|
52
|
+
} else {
|
|
53
|
+
return value.toFixed(fixedOnlyForCompact ? 0 : fixedDecimal);
|
|
54
|
+
}
|
|
55
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es5",
|
|
4
|
+
"lib": ["dom", "es2015"],
|
|
5
|
+
"allowJs": true,
|
|
6
|
+
"jsx": "react",
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"moduleResolution": "node",
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"outDir": "./lib",
|
|
12
|
+
"rootDir": "./src",
|
|
13
|
+
"sourceMap": true
|
|
14
|
+
},
|
|
15
|
+
"exclude": ["node_modules", "lib"]
|
|
16
|
+
}
|
package/README.md
DELETED