Skip to content

Commit

Permalink
feat(mobile): add displayPositiveSign to <AmountFormat/> (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
duongdev committed Aug 5, 2024
1 parent 51a4816 commit 59a33af
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions apps/mobile/components/common/amount-format.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { cn } from '@/lib/utils'
import { useDefaultCurrency } from '@/stores/user-settings/hooks'
import { type VariantProps, cva } from 'class-variance-authority'
import { useMemo } from 'react'
import { Text } from '../ui/text'

const amountVariants = cva('font-bold shrink-0', {
const amountVariants = cva('shrink-0 font-bold', {
variants: {
size: {
xl: 'text-4xl',
Expand Down Expand Up @@ -36,6 +37,7 @@ type AmountFormatProps = {
currency?: string
className?: string
displayNegativeSign?: boolean
displayPositiveSign?: boolean
displayPositiveColor?: boolean
} & VariantProps<typeof amountVariants>

Expand All @@ -45,9 +47,21 @@ export function AmountFormat({
className,
size,
displayNegativeSign,
displayPositiveSign,
displayPositiveColor,
}: AmountFormatProps) {
const defaultCurrency = useDefaultCurrency()

const sign = useMemo(() => {
if (amount < 0) {
return displayNegativeSign ? '-' : ''
}
if (amount > 0) {
return displayPositiveSign ? '+' : ''
}
return ''
}, [amount, displayNegativeSign, displayPositiveSign])

return (
<Text
className={cn(
Expand All @@ -60,8 +74,8 @@ export function AmountFormat({
className,
)}
>
{(displayNegativeSign ? amount : Math.abs(amount)).toLocaleString() ||
'0.00'}{' '}
{sign}
{Math.abs(amount).toLocaleString()}{' '}
<Text className={cn(currencyVariants({ size }))}>
{currency || defaultCurrency}
</Text>
Expand Down

0 comments on commit 59a33af

Please sign in to comment.