-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add count up #1642
feat: add count up #1642
Changes from 3 commits
5f33b40
7c856fe
ac46aba
6dcfe4b
4ab1e6f
9726aa6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,11 @@ | |
|
||
import { Grid, Skeleton, Typography } from '@mui/material'; | ||
import React, { useMemo } from 'react'; | ||
import CountUp from 'react-countup'; | ||
|
||
import { useCurrency } from '../hooks'; | ||
import { ASSETS_AS_CURRENCY_LIST } from '../util/currencyList'; | ||
import { amountToHuman, fixFloatingPoint } from '../util/utils'; | ||
import { amountToHuman, countDecimalPlaces, fixFloatingPoint } from '../util/utils'; | ||
|
||
interface Props { | ||
amount?: BN | null; | ||
|
@@ -27,6 +28,7 @@ | |
textColor?: string; | ||
height?: number; | ||
width?: string; | ||
withCountUp?: boolean; | ||
} | ||
|
||
export function nFormatter (num: number, decimalPoint: number) { | ||
|
@@ -54,7 +56,7 @@ | |
|
||
const DECIMAL_POINTS_FOR_CRYPTO_AS_CURRENCY = 4; | ||
|
||
function FormatPrice ({ amount, commify, decimalPoint = 2, decimals, fontSize, fontWeight, height, lineHeight = 1, mt = '0px', num, price, sign, skeletonHeight = 15, textAlign = 'left', textColor, width = '90px' }: Props): React.ReactElement<Props> { | ||
function FormatPrice ({ amount, commify, decimalPoint = 2, decimals, fontSize, fontWeight, height, lineHeight = 1, mt = '0px', num, price, sign, skeletonHeight = 15, textAlign = 'left', textColor, width = '90px', withCountUp }: Props): React.ReactElement<Props> { | ||
const currency = useCurrency(); | ||
|
||
const total = useMemo(() => { | ||
|
@@ -91,7 +93,17 @@ | |
lineHeight={lineHeight} | ||
sx={{ color: textColor }} | ||
> | ||
{sign || currency?.sign || ''}{ commify ? fixFloatingPoint(total as number, _decimalPoint, true) : nFormatter(total as number, _decimalPoint)} | ||
{withCountUp | ||
? <CountUp | ||
decimals={_decimalPoint} | ||
duration={1} | ||
end={parseFloat(String(total))} | ||
prefix={sign || currency?.sign || ''} | ||
/> | ||
Comment on lines
+97
to
+102
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Based on the gathered information, I can now provide the final response regarding the accessibility concerns of the CountUp component. Add accessibility attributes to CountUp implementation The current CountUp implementation lacks proper accessibility considerations. While the codebase shows awareness of reduced motion preferences in other components, the numeric animation needs similar treatment. Please update the implementation to:
<span aria-atomic="true" aria-live="polite">
<CountUp
decimals={_decimalPoint}
duration={prefersReducedMotion ? 0 : 1}
end={parseFloat(String(total))}
prefix={sign || currency?.sign || ''}
/>
</span>
This will ensure:
🔗 Analysis chainVerify accessibility impact Please ensure that the CountUp animation:
Let me check the content of these files to understand the existing accessibility implementations. Let me check if there are any existing ARIA attributes or accessibility considerations in the FormatPrice component, and also check the react-countup package usage. Let me check the TotalBalancePieChart component to see how CountUp is implemented there, and also verify the package.json for the react-countup version and any related accessibility packages. 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Search for existing accessibility considerations in the codebase
rg -l 'prefers-reduced-motion|aria-live|aria-atomic'
Length of output: 170 Script: #!/bin/bash
# Check content of files with accessibility considerations
echo "=== VaadinIcon.tsx ==="
cat packages/extension-polkagate/src/components/VaadinIcon.tsx
echo -e "\n=== useAnimateOnce.ts ==="
cat packages/extension-polkagate/src/hooks/useAnimateOnce.ts
# Also check if CountUp component has any accessibility considerations
rg -A 5 "CountUp" packages/extension-polkagate/src/components/FormatPrice.tsx
Length of output: 4728 Script: #!/bin/bash
# Check for any ARIA attributes in FormatPrice
rg -A 2 'aria-|role=' packages/extension-polkagate/src/components/FormatPrice.tsx
# Check if react-countup is used elsewhere with accessibility considerations
rg -l "react-countup"
# Look for any test files related to FormatPrice
fd "FormatPrice.*.test" || fd "FormatPrice.*.spec"
Length of output: 307 Script: #!/bin/bash
# Check CountUp implementation in TotalBalancePieChart
cat packages/extension-polkagate/src/fullscreen/homeFullScreen/partials/TotalBalancePieChart.tsx
# Check package.json for react-countup version and accessibility-related packages
cat package.json | grep -A 5 -B 5 "react-countup"
Length of output: 11484 |
||
: <> | ||
{sign || currency?.sign || ''}{ commify ? fixFloatingPoint(total as number, _decimalPoint, true) : nFormatter(total as number, _decimalPoint)} | ||
</> | ||
} | ||
</Typography> | ||
: <Skeleton | ||
animation='wave' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,12 @@ function countLeadingZerosInFraction (numStr: string) { | |
return 0; | ||
} | ||
|
||
export function countDecimalPlaces (n: number) { | ||
const match = n.toString().match(/\.(\d+)/); | ||
|
||
return match ? match[1].length : 0; | ||
} | ||
Comment on lines
+53
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add type safety and handle edge cases. The function implementation could be improved with better type safety and edge case handling: -export function countDecimalPlaces (n: number) {
+export function countDecimalPlaces (n: number): number {
+ if (!Number.isFinite(n)) {
+ return 0;
+ }
+
+ // Handle scientific notation
+ const str = n.toString().toLowerCase();
+ if (str.includes('e')) {
+ const [_, exp] = str.split('e');
+ return Math.max(0, -Number(exp));
+ }
+
const match = n.toString().match(/\.(\d+)/);
return match ? match[1].length : 0;
} Also consider adding JSDoc documentation to describe the function's purpose and behavior: /**
* Counts the number of decimal places in a given number.
* @param n - The number to analyze
* @returns The count of decimal places (0 for integers, NaN, or Infinity)
* @example
* countDecimalPlaces(1.23) // returns 2
* countDecimalPlaces(1) // returns 0
* countDecimalPlaces(1e-10) // returns 10
*/ |
||
|
||
export function fixFloatingPoint (_number: number | string, decimalDigit = FLOATING_POINT_DIGIT, commify?: boolean, dynamicDecimal?: boolean): string { | ||
const MAX_DECIMAL_POINTS = 6; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unused import
countDecimalPlaces
The
countDecimalPlaces
utility is imported but never used in this component.📝 Committable suggestion
🧰 Tools
🪛 GitHub Check: build
[failure] 12-12:
'countDecimalPlaces' is declared but its value is never read.