@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 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/constants/index.ts"],"names":[],"mappings":";;;AAAa,QAAA,YAAY,GAAG,IAAI,KAAK,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACrC,QAAA,iBAAiB,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;AACzC,QAAA,cAAc,GAEvB;IACF,GAAG,EAAE,KAAK;IACV,GAAG,EAAE,OAAO;IACZ,GAAG,EAAE,MAAM;IACX,GAAG,EAAE,MAAM;IACX,GAAG,EAAE,QAAQ;IACb,GAAG,EAAE,QAAQ;IACb,GAAG,EAAE,QAAQ;IACb,GAAG,EAAE,QAAQ;IACb,GAAG,EAAE,QAAQ;IACb,GAAG,EAAE,QAAQ;IACb,GAAG,EAAE,QAAQ;IACb,GAAG,EAAE,QAAQ;IACb,GAAG,EAAE,QAAQ;IACb,GAAG,EAAE,QAAQ;IACb,CAAC,EAAE,SAAS;IACZ,CAAC,EAAE,SAAS;IACZ,CAAC,EAAE,SAAS;IACZ,CAAC,EAAE,SAAS;CACb,CAAC"}
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./components/AnimatedDigit"), exports);
|
|
18
|
+
__exportStar(require("./components/AnimatedRollingNumber"), exports);
|
|
19
|
+
var AnimatedRollingNumber_1 = require("./components/AnimatedRollingNumber");
|
|
20
|
+
exports.default = AnimatedRollingNumber_1.AnimatedRollingNumber;
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6DAA2C;AAC3C,qEAAmD;AACnD,4EAA2E;AAE3E,kBAAe,6CAAqB,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
export declare function formatCompactNumber(value: number, fixedDecimal?: number, fixedOnlyForCompact?: boolean, useGrouping?: boolean, locales?: Intl.LocalesArgument): string;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a number into a compact string representation (e.g., 1K, 1.5M).
|
|
4
|
+
*
|
|
5
|
+
* @param value - The numeric value to format.
|
|
6
|
+
* @param fixedDecimal - Number of decimal places to show in compact notation. Defaults to 1.
|
|
7
|
+
* @param fixedOnlyForCompact - If true, only applies decimal fixing for compact notation. Defaults to false.
|
|
8
|
+
* @param useGrouping - Whether to include separator for thousands separators .
|
|
9
|
+
* @param locales - The locale to use for number formatting. Defaults to "en-US".
|
|
10
|
+
* @returns The formatted string representation of the number.
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.formatCompactNumber = formatCompactNumber;
|
|
14
|
+
function formatCompactNumber(value, fixedDecimal, fixedOnlyForCompact, useGrouping, locales) {
|
|
15
|
+
if (fixedDecimal === void 0) { fixedDecimal = 1; }
|
|
16
|
+
if (fixedOnlyForCompact === void 0) { fixedOnlyForCompact = false; }
|
|
17
|
+
if (useGrouping === void 0) { useGrouping = false; }
|
|
18
|
+
if (locales === void 0) { locales = "en-US"; }
|
|
19
|
+
var absValue = Math.abs(value);
|
|
20
|
+
if (absValue >= 1000000000000) {
|
|
21
|
+
return ((value / 1000000000000).toLocaleString(locales, {
|
|
22
|
+
maximumFractionDigits: fixedDecimal,
|
|
23
|
+
minimumFractionDigits: fixedDecimal,
|
|
24
|
+
useGrouping: useGrouping,
|
|
25
|
+
}) + "T");
|
|
26
|
+
}
|
|
27
|
+
else if (absValue >= 1000000000) {
|
|
28
|
+
return ((value / 1000000000).toLocaleString(locales, {
|
|
29
|
+
maximumFractionDigits: fixedDecimal,
|
|
30
|
+
minimumFractionDigits: fixedDecimal,
|
|
31
|
+
useGrouping: useGrouping,
|
|
32
|
+
}) + "B");
|
|
33
|
+
}
|
|
34
|
+
else if (absValue >= 1000000) {
|
|
35
|
+
return ((value / 1000000).toLocaleString(locales, {
|
|
36
|
+
maximumFractionDigits: fixedDecimal,
|
|
37
|
+
minimumFractionDigits: fixedDecimal,
|
|
38
|
+
useGrouping: useGrouping,
|
|
39
|
+
}) + "M");
|
|
40
|
+
}
|
|
41
|
+
else if (absValue >= 1000) {
|
|
42
|
+
return ((value / 1000).toLocaleString(locales, {
|
|
43
|
+
maximumFractionDigits: fixedDecimal,
|
|
44
|
+
minimumFractionDigits: fixedDecimal,
|
|
45
|
+
useGrouping: useGrouping,
|
|
46
|
+
}) + "K");
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
return value.toFixed(fixedOnlyForCompact ? 0 : fixedDecimal);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;AAEH,kDA2CC;AA3CD,SAAgB,mBAAmB,CACjC,KAAa,EACb,YAAwB,EACxB,mBAAoC,EACpC,WAA4B,EAC5B,OAAuC;IAHvC,6BAAA,EAAA,gBAAwB;IACxB,oCAAA,EAAA,2BAAoC;IACpC,4BAAA,EAAA,mBAA4B;IAC5B,wBAAA,EAAA,iBAAuC;IAEvC,IAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACjC,IAAI,QAAQ,IAAI,aAAiB,EAAE,CAAC;QAClC,OAAO,CACL,CAAC,KAAK,GAAG,aAAiB,CAAC,CAAC,cAAc,CAAC,OAAO,EAAE;YAClD,qBAAqB,EAAE,YAAY;YACnC,qBAAqB,EAAE,YAAY;YACnC,WAAW,aAAA;SACZ,CAAC,GAAG,GAAG,CACT,CAAC;IACJ,CAAC;SAAM,IAAI,QAAQ,IAAI,UAAa,EAAE,CAAC;QACrC,OAAO,CACL,CAAC,KAAK,GAAG,UAAa,CAAC,CAAC,cAAc,CAAC,OAAO,EAAE;YAC9C,qBAAqB,EAAE,YAAY;YACnC,qBAAqB,EAAE,YAAY;YACnC,WAAW,aAAA;SACZ,CAAC,GAAG,GAAG,CACT,CAAC;IACJ,CAAC;SAAM,IAAI,QAAQ,IAAI,OAAS,EAAE,CAAC;QACjC,OAAO,CACL,CAAC,KAAK,GAAG,OAAS,CAAC,CAAC,cAAc,CAAC,OAAO,EAAE;YAC1C,qBAAqB,EAAE,YAAY;YACnC,qBAAqB,EAAE,YAAY;YACnC,WAAW,aAAA;SACZ,CAAC,GAAG,GAAG,CACT,CAAC;IACJ,CAAC;SAAM,IAAI,QAAQ,IAAI,IAAK,EAAE,CAAC;QAC7B,OAAO,CACL,CAAC,KAAK,GAAG,IAAK,CAAC,CAAC,cAAc,CAAC,OAAO,EAAE;YACtC,qBAAqB,EAAE,YAAY;YACnC,qBAAqB,EAAE,YAAY;YACnC,WAAW,aAAA;SACZ,CAAC,GAAG,GAAG,CACT,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,OAAO,KAAK,CAAC,OAAO,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC;IAC/D,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ohos-ports/react-native-animated-rolling-numbers",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "2.0.0-beta.1",
|
|
4
|
+
"main": "src/index.ts",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "tsc"
|
|
7
|
+
},
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/ohos-ports/ohos-ports.git",
|
|
11
|
+
"directory": "ports/react-native-animated-rolling-numbers/2.0.0"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/BouarourMohammed/react-native-animated-rolling-numbers.git#readme",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"react-native",
|
|
16
|
+
"animated-numbers",
|
|
17
|
+
"rolling-numbers",
|
|
18
|
+
"animated-digits",
|
|
19
|
+
"animated-number",
|
|
20
|
+
"reanimated",
|
|
21
|
+
"numbers",
|
|
22
|
+
"react",
|
|
23
|
+
"react-native-rolling-numbers",
|
|
24
|
+
"react-native-animated-numbers",
|
|
25
|
+
"digits"
|
|
26
|
+
],
|
|
27
|
+
"author": "Bouarour Mohammed",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"description": "🚀 The Animated Digits component for React Native seamlessly blends a sophisticated number rotation effect with dynamic value updates, creating an engaging and interactive experience that enhances your user interfaces with a touch of elegance and excitement.",
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/react": "^18.3.5",
|
|
32
|
+
"@types/react-native": "^0.73.0",
|
|
33
|
+
"typescript": "^5.5.4"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"react": ">=18.0.0",
|
|
37
|
+
"@ohos-ports/react-native": "^0.87.1-beta.3",
|
|
38
|
+
"@ohos-ports/react-native-reanimated": "^4.5.3-beta.1"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
# React Native Animated Rolling numbers
|
|
2
|
+
|
|
3
|
+
✨ `react-native-animated-rolling-numbers` is a React Native component that animates numbers and compact notations (K, M, B, T) with customizable styles and animations. It's designed to create smooth, eye-catching transitions between numeric values.
|
|
4
|
+
|
|
5
|
+
[](https://github.com/user-attachments/assets/48b10447-5978-4a51-9715-229631a939d5)
|
|
6
|
+
|
|
7
|
+
The example shown in the video is available [here](example/example.md).
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- ✨ Supports animated transitions for numeric values and symbols (e.g., commas, dots).
|
|
12
|
+
- 🎨 Customizable digit styles and animation configurations.
|
|
13
|
+
- 📏 Handles compact number formatting (e.g., 1K, 1M, etc.).
|
|
14
|
+
- 🔧 Easily customizable via props.
|
|
15
|
+
- 🌀 Supports reduce-motion and easing functions for animations.
|
|
16
|
+
- 🔢 Designed for both regular numbers and formatted numbers with signs (positive/negative) and commas.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install react-native-animated-rolling-numbers
|
|
22
|
+
# or
|
|
23
|
+
yarn add react-native-animated-rolling-numbers
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
> [!IMPORTANT]
|
|
27
|
+
> This library relies on react-native-reanimated. You must install react-native-reanimated separately in your project. This is a peer dependency.
|
|
28
|
+
|
|
29
|
+
Make sure you have `react-native-reanimated` installed in your project. Follow the official installation guide here: [react-native-reanimated installation](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/getting-started/#installation).
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
Basic Example
|
|
34
|
+
|
|
35
|
+
```javascript
|
|
36
|
+
import React, { useState } from "react";
|
|
37
|
+
import { View, StyleSheet, Button } from "react-native";
|
|
38
|
+
import { AnimatedRollingNumber } from "react-native-animated-rolling-numbers";
|
|
39
|
+
import { Easing } from "react-native-reanimated";
|
|
40
|
+
|
|
41
|
+
const App = () => {
|
|
42
|
+
const [value, setValue] = useState(1200);
|
|
43
|
+
return (
|
|
44
|
+
<View style={styles.container}>
|
|
45
|
+
<AnimatedRollingNumber
|
|
46
|
+
value={value}
|
|
47
|
+
showPlusSign
|
|
48
|
+
showMinusSign
|
|
49
|
+
useGrouping
|
|
50
|
+
enableCompactNotation
|
|
51
|
+
compactToFixed={2}
|
|
52
|
+
textStyle={styles.digits}
|
|
53
|
+
spinningAnimationConfig={{ duration: 500, easing: Easing.bounce }}
|
|
54
|
+
/>
|
|
55
|
+
<Button
|
|
56
|
+
onPress={() => setValue(value + Math.floor(Math.random() * 1000))}
|
|
57
|
+
title="Increase"
|
|
58
|
+
/>
|
|
59
|
+
<Button
|
|
60
|
+
onPress={() => setValue(value - Math.floor(Math.random() * 1000))}
|
|
61
|
+
title="Decrease"
|
|
62
|
+
/>
|
|
63
|
+
</View>
|
|
64
|
+
);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const styles = StyleSheet.create({
|
|
68
|
+
container: {
|
|
69
|
+
flex: 1,
|
|
70
|
+
justifyContent: "center",
|
|
71
|
+
alignItems: "center",
|
|
72
|
+
},
|
|
73
|
+
digits: {
|
|
74
|
+
fontSize: 32,
|
|
75
|
+
fontWeight: "bold",
|
|
76
|
+
paddingHorizontal: 2,
|
|
77
|
+
color: "#4A90E2",
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
export default App;
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
The example shown in the video is available [here](example/example.md).
|
|
85
|
+
|
|
86
|
+
## Props
|
|
87
|
+
|
|
88
|
+
### `value: number`
|
|
89
|
+
|
|
90
|
+
The numeric value to be displayed and animated.
|
|
91
|
+
|
|
92
|
+
### `showSign?: boolean`
|
|
93
|
+
|
|
94
|
+
Whether to show the sign of the number (e.g., "+" or "-"). Defaults to `false`.
|
|
95
|
+
|
|
96
|
+
### `showMinusSign?: boolean`
|
|
97
|
+
|
|
98
|
+
Whether to show the minus sign for negative numbers. Defaults to `true`.
|
|
99
|
+
|
|
100
|
+
### `showPlusSign?: boolean`
|
|
101
|
+
|
|
102
|
+
Whether to show the plus sign for positive numbers. Defaults to `false`.
|
|
103
|
+
|
|
104
|
+
### `toFixed?: number`
|
|
105
|
+
|
|
106
|
+
Number of decimal places to show in normal notation. defaults to `undefined`.
|
|
107
|
+
|
|
108
|
+
### `useGrouping?: boolean`
|
|
109
|
+
|
|
110
|
+
Whether to include commas/points in the number formatting (e.g.,1000987 -> 1,000,987). Defaults to `false`.
|
|
111
|
+
|
|
112
|
+
### `formattedText?: string`
|
|
113
|
+
|
|
114
|
+
Custom formatted text to display instead of the numeric value.
|
|
115
|
+
|
|
116
|
+
### `locale?: Intl.LocalesArgument`
|
|
117
|
+
|
|
118
|
+
The locale to use for number formatting. Defaults to `"en-US"`.
|
|
119
|
+
|
|
120
|
+
### `enableCompactNotation?: boolean`
|
|
121
|
+
|
|
122
|
+
Enable compact notation for large numbers (e.g., 1K, 1M, 1B, 1T). Defaults to `false`.
|
|
123
|
+
|
|
124
|
+
### `compactToFixed?: number`
|
|
125
|
+
|
|
126
|
+
Number of decimal places to show in compact notation. Defaults to `undefined`.
|
|
127
|
+
|
|
128
|
+
### `fixedOnlyForCompact?: boolean`
|
|
129
|
+
|
|
130
|
+
If true, only applies decimal precision for compact notation (K/M/B/T). Defaults to `true`.
|
|
131
|
+
|
|
132
|
+
Example:
|
|
133
|
+
|
|
134
|
+
- If `fixedOnlyForCompact` is `true` and `compactToFixed` is `2`, the number `1500` will be displayed as `1.50K`, but the number `999` will be displayed as `999` (no decimal places).
|
|
135
|
+
- If `fixedOnlyForCompact` is `false` and `compactToFixed` is `2`, the number `1500` will be displayed as `1.50K`, and the number `999` will be displayed as `999.00`.
|
|
136
|
+
|
|
137
|
+
### `containerStyle?: StyleProp<ViewStyle>`
|
|
138
|
+
|
|
139
|
+
Style for the container wrapping the animated digits.
|
|
140
|
+
|
|
141
|
+
### `digitContainerStyle?: StyleProp<ViewStyle>`
|
|
142
|
+
|
|
143
|
+
Style for the container wrapping each animated digit.
|
|
144
|
+
|
|
145
|
+
### `textProps?: React.ComponentProps<typeof Text>`
|
|
146
|
+
|
|
147
|
+
Props for the main text component.
|
|
148
|
+
|
|
149
|
+
### `numberTextProps?: React.ComponentProps<typeof Text>`
|
|
150
|
+
|
|
151
|
+
Props for the numeric text components.
|
|
152
|
+
|
|
153
|
+
### `commaTextProps?: React.ComponentProps<typeof Text>`
|
|
154
|
+
|
|
155
|
+
Props for the comma text component.
|
|
156
|
+
|
|
157
|
+
### `dotTextProps?: React.ComponentProps<typeof Text>`
|
|
158
|
+
|
|
159
|
+
Props for the decimal point text component.
|
|
160
|
+
|
|
161
|
+
### `compactNotationTextProps?: React.ComponentProps<typeof Text>`
|
|
162
|
+
|
|
163
|
+
Props for the compact notation text components (K, M, B, T).
|
|
164
|
+
|
|
165
|
+
### `signTextProps?: React.ComponentProps<typeof Text>`
|
|
166
|
+
|
|
167
|
+
Props for the sign text component (if used).
|
|
168
|
+
|
|
169
|
+
### `textStyle?: StyleProp<TextStyle>`
|
|
170
|
+
|
|
171
|
+
Style for the main text component.
|
|
172
|
+
|
|
173
|
+
### `numberStyle?: StyleProp<TextStyle>`
|
|
174
|
+
|
|
175
|
+
Style for the numeric text components.
|
|
176
|
+
|
|
177
|
+
### `commaStyle?: StyleProp<TextStyle>`
|
|
178
|
+
|
|
179
|
+
Style for the comma text component.
|
|
180
|
+
|
|
181
|
+
### `dotStyle?: StyleProp<TextStyle>`
|
|
182
|
+
|
|
183
|
+
Style for the decimal point text component.
|
|
184
|
+
|
|
185
|
+
### `compactNotationStyle?: StyleProp<TextStyle>`
|
|
186
|
+
|
|
187
|
+
Style for the compact notation text components.
|
|
188
|
+
|
|
189
|
+
### `signStyle?: StyleProp<TextStyle>`
|
|
190
|
+
|
|
191
|
+
Style for the sign text component.
|
|
192
|
+
|
|
193
|
+
### `spinningAnimationConfig?: object`
|
|
194
|
+
|
|
195
|
+
Configuration for the digit animation. Supports:
|
|
196
|
+
|
|
197
|
+
- `duration: number`: Duration of the animation in milliseconds.
|
|
198
|
+
- `reduceMotion: boolean`: Whether to reduce motion for accessibility.
|
|
199
|
+
- `easing: EasingFunction`: Custom easing function for the animation.
|
|
200
|
+
|
|
201
|
+
### `animationCallback?: AnimationCallback`
|
|
202
|
+
|
|
203
|
+
Callback function that is invoked when the animation completes.
|
|
204
|
+
|
|
205
|
+
### `animateToNewValue?: (newValue: number, variant?: DigitVariant) => number`
|
|
206
|
+
|
|
207
|
+
Custom function to animate the value change. Defaults to a bounce animation.
|
|
208
|
+
|
|
209
|
+
If you find this project useful and would like to support its ongoing development, consider buying me a coffee! Your support helps keep the creativity brewing and allows me to continue improving and maintaining this project. Thank you! ☕💖
|
|
210
|
+
|
|
211
|
+
<a href="https://buymeacoffee.com/bouarourmohammed" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import React, { useEffect, useMemo } from "react";
|
|
2
|
+
import {
|
|
3
|
+
type StyleProp,
|
|
4
|
+
StyleSheet,
|
|
5
|
+
Text,
|
|
6
|
+
type TextStyle,
|
|
7
|
+
type ViewStyle,
|
|
8
|
+
} from "@ohos-ports/react-native";
|
|
9
|
+
import Animated, {
|
|
10
|
+
type AnimationCallback,
|
|
11
|
+
Easing,
|
|
12
|
+
type EasingFunction,
|
|
13
|
+
type EasingFunctionFactory,
|
|
14
|
+
FadeIn,
|
|
15
|
+
LinearTransition,
|
|
16
|
+
ReduceMotion,
|
|
17
|
+
runOnJS,
|
|
18
|
+
useAnimatedStyle,
|
|
19
|
+
useSharedValue,
|
|
20
|
+
withTiming,
|
|
21
|
+
} from "@ohos-ports/react-native-reanimated";
|
|
22
|
+
import { COMPACT_NOTATIONS, DIGIT_VARIANTS, NUMBER_ARRAY } from "../constants";
|
|
23
|
+
|
|
24
|
+
export type DigitVariant = "sign" | "number" | "compact" | "dot" | "comma";
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Properties for the AnimatedDigit component.
|
|
28
|
+
*/
|
|
29
|
+
export interface AnimatedDigitProps
|
|
30
|
+
extends Omit<React.ComponentProps<typeof Animated.View>, "style"> {
|
|
31
|
+
/** The string value to be displayed and animated. */
|
|
32
|
+
value: string;
|
|
33
|
+
/** The height of each digit or notation, used for animation calculations. */
|
|
34
|
+
height: number;
|
|
35
|
+
/** Style for the container wrapping the animated text. */
|
|
36
|
+
containerStyle?: StyleProp<ViewStyle>;
|
|
37
|
+
/** Props for the primary text component used in the component. */
|
|
38
|
+
textProps?: Omit<React.ComponentProps<typeof Text>, "style">;
|
|
39
|
+
/** Props for the number text components. */
|
|
40
|
+
numberTextProps?: Omit<React.ComponentProps<typeof Text>, "style">;
|
|
41
|
+
/** Props for the comma text component, if used. */
|
|
42
|
+
commaTextProps?: Omit<React.ComponentProps<typeof Text>, "style">;
|
|
43
|
+
/** Props for the dot (decimal point) text component, if used. */
|
|
44
|
+
dotTextProps?: Omit<React.ComponentProps<typeof Text>, "style">;
|
|
45
|
+
/** Props for the compact notation text components (K, M, B, T). */
|
|
46
|
+
compactNotationTextProps?: Omit<React.ComponentProps<typeof Text>, "style">;
|
|
47
|
+
/** Props for the sign text component, if used. */
|
|
48
|
+
signTextProps?: Omit<React.ComponentProps<typeof Text>, "style">;
|
|
49
|
+
/** Style for the primary text component. */
|
|
50
|
+
textStyle?: StyleProp<TextStyle>;
|
|
51
|
+
/** Style for the number text components. */
|
|
52
|
+
numberStyle?: StyleProp<TextStyle>;
|
|
53
|
+
/** Style for the comma text component, if used. */
|
|
54
|
+
commaStyle?: StyleProp<TextStyle>;
|
|
55
|
+
/** Style for the dot (decimal point) text component, if used. */
|
|
56
|
+
dotStyle?: StyleProp<TextStyle>;
|
|
57
|
+
/** Style for the compact notation text components (K, M, B, T). */
|
|
58
|
+
compactNotationStyle?: StyleProp<TextStyle>;
|
|
59
|
+
/** Style for the sign text component, if used. */
|
|
60
|
+
signStyle?: StyleProp<TextStyle>;
|
|
61
|
+
/** Configuration for the animation moving the digits up and down. Defaults to a bounce animation with 500ms duration. */
|
|
62
|
+
spinningAnimationConfig?: {
|
|
63
|
+
duration?: number;
|
|
64
|
+
reduceMotion?: ReduceMotion;
|
|
65
|
+
easing?: EasingFunction | EasingFunctionFactory;
|
|
66
|
+
};
|
|
67
|
+
/** Callback function invoked when the animation completes. */
|
|
68
|
+
animationCallback?: AnimationCallback;
|
|
69
|
+
/** Custom function to animate the value change. Defaults to a bounce animation. */
|
|
70
|
+
animateToNewValue?: (newValue: number, variant?: DigitVariant) => number;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* AnimatedDigit is a React component that animates numbers and compact notations (K, M, B, T)
|
|
75
|
+
* with customizable styles and animations.
|
|
76
|
+
*/
|
|
77
|
+
export const AnimatedDigit: React.FC<AnimatedDigitProps> = ({
|
|
78
|
+
value,
|
|
79
|
+
height,
|
|
80
|
+
containerStyle,
|
|
81
|
+
spinningAnimationConfig,
|
|
82
|
+
animationCallback = () => {},
|
|
83
|
+
animateToNewValue = (newValue, variant) =>
|
|
84
|
+
withTiming(
|
|
85
|
+
-newValue,
|
|
86
|
+
spinningAnimationConfig || {
|
|
87
|
+
duration: variant === "number" ? 600 : 100,
|
|
88
|
+
easing: Easing.elastic(1),
|
|
89
|
+
},
|
|
90
|
+
(finished, current) => runOnJS(animationCallback)?.(finished, current)
|
|
91
|
+
),
|
|
92
|
+
textProps,
|
|
93
|
+
commaTextProps,
|
|
94
|
+
dotTextProps,
|
|
95
|
+
compactNotationTextProps,
|
|
96
|
+
signTextProps,
|
|
97
|
+
textStyle,
|
|
98
|
+
numberStyle,
|
|
99
|
+
commaStyle,
|
|
100
|
+
dotStyle,
|
|
101
|
+
signStyle,
|
|
102
|
+
compactNotationStyle,
|
|
103
|
+
numberTextProps,
|
|
104
|
+
...rest
|
|
105
|
+
}) => {
|
|
106
|
+
const isNumeric = !isNaN(Number(value));
|
|
107
|
+
const animatedValue = useSharedValue(isNumeric ? Number(value) : 0);
|
|
108
|
+
const animatedStyle = useAnimatedStyle(
|
|
109
|
+
() => ({
|
|
110
|
+
transform: [{ translateY: animatedValue.value * (height || 0) }],
|
|
111
|
+
}),
|
|
112
|
+
[height]
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const othersTextStyle = useMemo(
|
|
116
|
+
() => ({
|
|
117
|
+
".": dotStyle,
|
|
118
|
+
",": commaStyle,
|
|
119
|
+
"-": signStyle,
|
|
120
|
+
"+": signStyle,
|
|
121
|
+
}),
|
|
122
|
+
[signStyle, dotStyle, commaStyle]
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
const othersTextProps = useMemo(
|
|
126
|
+
() => ({
|
|
127
|
+
".": dotTextProps,
|
|
128
|
+
",": commaTextProps,
|
|
129
|
+
"-": signTextProps,
|
|
130
|
+
"+": signTextProps,
|
|
131
|
+
}),
|
|
132
|
+
[signTextProps, dotTextProps, commaTextProps]
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
const resolvedPadding = useMemo(
|
|
136
|
+
() =>
|
|
137
|
+
(StyleSheet.flatten(compactNotationStyle)?.fontSize ||
|
|
138
|
+
StyleSheet.flatten(textStyle)?.fontSize ||
|
|
139
|
+
14) * 0.1,
|
|
140
|
+
[textStyle, compactNotationStyle]
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
useEffect(() => {
|
|
144
|
+
const newValue = !isNumeric
|
|
145
|
+
? COMPACT_NOTATIONS.includes(value.toUpperCase())
|
|
146
|
+
? COMPACT_NOTATIONS.indexOf(value.toUpperCase())
|
|
147
|
+
: 0
|
|
148
|
+
: Number(value);
|
|
149
|
+
animatedValue.value = animateToNewValue(newValue, DIGIT_VARIANTS[value]);
|
|
150
|
+
}, [value]);
|
|
151
|
+
|
|
152
|
+
return useMemo(
|
|
153
|
+
() => (
|
|
154
|
+
<Animated.View
|
|
155
|
+
layout={LinearTransition}
|
|
156
|
+
entering={FadeIn}
|
|
157
|
+
style={[
|
|
158
|
+
{ alignItems: "center", justifyContent: "center" },
|
|
159
|
+
containerStyle,
|
|
160
|
+
{ overflow: "hidden" },
|
|
161
|
+
]}
|
|
162
|
+
{...rest}
|
|
163
|
+
>
|
|
164
|
+
<Animated.View style={[styles.digitContainer, animatedStyle]}>
|
|
165
|
+
{isNumeric ? (
|
|
166
|
+
NUMBER_ARRAY.map((_, index) => (
|
|
167
|
+
<Text
|
|
168
|
+
key={index}
|
|
169
|
+
style={[
|
|
170
|
+
textStyle,
|
|
171
|
+
numberStyle,
|
|
172
|
+
{
|
|
173
|
+
position: index === 0 ? "relative" : "absolute",
|
|
174
|
+
top: height * index,
|
|
175
|
+
},
|
|
176
|
+
]}
|
|
177
|
+
{...textProps}
|
|
178
|
+
{...numberTextProps}
|
|
179
|
+
>
|
|
180
|
+
{index}
|
|
181
|
+
</Text>
|
|
182
|
+
))
|
|
183
|
+
) : COMPACT_NOTATIONS.includes(value.toUpperCase()) ? (
|
|
184
|
+
COMPACT_NOTATIONS.map((char, index) => (
|
|
185
|
+
<Text
|
|
186
|
+
key={index}
|
|
187
|
+
style={[
|
|
188
|
+
textStyle,
|
|
189
|
+
compactNotationStyle,
|
|
190
|
+
{
|
|
191
|
+
paddingHorizontal: index === 0 ? resolvedPadding : 0,
|
|
192
|
+
position: index === 0 ? "relative" : "absolute",
|
|
193
|
+
top: height * index,
|
|
194
|
+
},
|
|
195
|
+
]}
|
|
196
|
+
{...textProps}
|
|
197
|
+
{...compactNotationTextProps}
|
|
198
|
+
>
|
|
199
|
+
{char}
|
|
200
|
+
</Text>
|
|
201
|
+
))
|
|
202
|
+
) : (
|
|
203
|
+
<Text
|
|
204
|
+
style={[
|
|
205
|
+
textStyle,
|
|
206
|
+
othersTextStyle?.[value as keyof typeof othersTextStyle],
|
|
207
|
+
]}
|
|
208
|
+
{...textProps}
|
|
209
|
+
{...othersTextProps?.[value as keyof typeof othersTextProps]}
|
|
210
|
+
>
|
|
211
|
+
{value}
|
|
212
|
+
</Text>
|
|
213
|
+
)}
|
|
214
|
+
</Animated.View>
|
|
215
|
+
</Animated.View>
|
|
216
|
+
),
|
|
217
|
+
__DEV__
|
|
218
|
+
? [
|
|
219
|
+
value,
|
|
220
|
+
height,
|
|
221
|
+
containerStyle,
|
|
222
|
+
textProps,
|
|
223
|
+
numberTextProps,
|
|
224
|
+
compactNotationTextProps,
|
|
225
|
+
othersTextProps,
|
|
226
|
+
textStyle,
|
|
227
|
+
numberStyle,
|
|
228
|
+
compactNotationStyle,
|
|
229
|
+
othersTextStyle,
|
|
230
|
+
]
|
|
231
|
+
: [height, value]
|
|
232
|
+
);
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
const styles = StyleSheet.create({
|
|
236
|
+
digitContainer: {
|
|
237
|
+
alignItems: "center",
|
|
238
|
+
overflow: "visible",
|
|
239
|
+
justifyContent: "center",
|
|
240
|
+
},
|
|
241
|
+
});
|