Skip to content
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

Improve currency inputs #128

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/components/ui/currency-input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as React from "react"
import { cn } from "@/lib/utils"

export interface InputProps
extends React.InputHTMLAttributes<HTMLInputElement> {}

const CurrencyInput = React.forwardRef<HTMLInputElement, InputProps>(
({ className, ...props }, ref) => {
const { onChange } = props

/**
* Override onChange to handle currency formatting
*
* - If the value begins with $, remove it
* - If the value contains a comma, remove it
*
* @param e - React.ChangeEvent<HTMLInputElement>
*/
const customOnChange = (e: React.ChangeEvent<HTMLInputElement>) => {
let val = e.target.value
if (val.startsWith("$")) {
val = val.slice(1)
}
if (val.includes(",")) {
val = val.replace(/,/g, "")
}

// call the original onChange
e.target.value = val
onChange && onChange(e)
}

return (
<input
className={cn(
"flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
className
)}
ref={ref}
{...props}
onChange={customOnChange}
/>
)
}
)
CurrencyInput.displayName = "CurrencyInput"

export { CurrencyInput }
11 changes: 6 additions & 5 deletions src/pages/activity/components/activity-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
FormMessage,
} from '@/components/ui/form';
import { Input } from '@/components/ui/input';
import { CurrencyInput } from '@/components/ui/currency-input';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
import {
Select,
Expand Down Expand Up @@ -263,7 +264,7 @@ const CashActivityFields = ({ currentAccountCurrency }: CashActivityFieldsProps)
<FormItem>
<FormLabel>Fee</FormLabel>
<FormControl>
<Input type="number" inputMode="decimal" placeholder="Fee" {...field} />
<CurrencyInput placeholder="Fee" {...field} />
</FormControl>
<FormMessage />
</FormItem>
Expand All @@ -277,7 +278,7 @@ const CashActivityFields = ({ currentAccountCurrency }: CashActivityFieldsProps)
<FormItem>
<FormLabel>Amount</FormLabel>
<FormControl>
<Input type="number" inputMode="decimal" placeholder="Amount" {...field} />
<CurrencyInput placeholder="Amount" {...field} />
</FormControl>
<FormMessage />
</FormItem>
Expand Down Expand Up @@ -353,7 +354,7 @@ const AssetActivityFields = ({ defaultAssetId }: AssetActivityFieldsProps) => {
<FormItem>
<FormLabel>Price</FormLabel>
<FormControl>
<Input type="number" inputMode="decimal" placeholder="Price" {...field} />
<CurrencyInput placeholder="Price" {...field} />
</FormControl>
<FormMessage />
</FormItem>
Expand All @@ -366,7 +367,7 @@ const AssetActivityFields = ({ defaultAssetId }: AssetActivityFieldsProps) => {
<FormItem>
<FormLabel>Fee</FormLabel>
<FormControl>
<Input type="number" inputMode="decimal" placeholder="Fee" {...field} />
<CurrencyInput placeholder="Fee" {...field} />
</FormControl>
<FormMessage />
</FormItem>
Expand Down Expand Up @@ -406,7 +407,7 @@ const DividendActivityFields = ({ defaultAssetId }: DividendActivityFieldsProps)
<FormItem>
<FormLabel>Dividend Amount</FormLabel>
<FormControl>
<Input type="number" inputMode="decimal" placeholder="Dividend Amount" {...field} />
<CurrencyInput placeholder="Dividend Amount" {...field} />
</FormControl>
<FormMessage />
</FormItem>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/settings/goals/components/goal-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function GoalForm({ defaultValues, onSuccess = () => {} }: GoalFormlProps
<FormItem>
<FormLabel>Target amount</FormLabel>
<FormControl>
<Input type="number" inputMode="decimal" placeholder="Target amount" {...field} />
<CurrencyInput placeholder="Target amount" {...field} />
</FormControl>
<FormMessage />
</FormItem>
Expand Down