feat(currency): centralise money formatting + curated currency picker
New `src/lib/utils/currency.ts` is the single source of truth for display formatting (`formatCurrency`) and the supported-currency catalog (`SUPPORTED_CURRENCIES`, 10 codes covering the marina market). New shared components: - `<CurrencyInput>` — number input with leading symbol prefix and decimal inputMode, raw number value out via onChange. - `<CurrencySelect>` — Select dropdown over `SUPPORTED_CURRENCIES` with symbol + code + label per row, replaces the free-text 3-letter inputs that let reps type "EURO" or "$$$" into a 3-char ISO column. Threaded through every money input + display: - Forms: berth (price/currency), expense (amount/currency), invoice (currency Select + line-items unit-price + step-3 review totals). - Reads: berth-card / berth-columns / invoice-card / expense-card / dashboard KPIs / dashboard revenue-forecast / portal-invoices page. Each had its own ad-hoc `Intl.NumberFormat` wrapper with slightly different fallbacks; collapsed onto the shared helper. `InvoiceLineItems` gained a `currency` prop so the unit-price input prefix and the subtotal use the parent invoice's currency rather than hard-coded `en-US` formatting. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
66
src/components/shared/currency-input.tsx
Normal file
66
src/components/shared/currency-input.tsx
Normal file
@@ -0,0 +1,66 @@
|
||||
'use client';
|
||||
|
||||
import * as React from 'react';
|
||||
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { currencySymbol } from '@/lib/utils/currency';
|
||||
|
||||
interface CurrencyInputProps
|
||||
extends Omit<React.ComponentProps<'input'>, 'value' | 'onChange' | 'type'> {
|
||||
/** Controlled raw numeric value. `null` / `undefined` render empty. */
|
||||
value: number | string | null | undefined;
|
||||
/** Fires with a raw number (or `null` if cleared). */
|
||||
onChange: (value: number | null) => void;
|
||||
/** ISO currency code; renders as a leading symbol prefix. */
|
||||
currency?: string;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Numeric input pre-decorated with a currency symbol. The display
|
||||
* value is the raw number the user typed (we don't fight the keystroke
|
||||
* cadence by re-formatting on every key) — formatted display lives in
|
||||
* read-only contexts via `formatCurrency()`. This keeps form behaviour
|
||||
* predictable while still scoping the input to a money field via the
|
||||
* symbol prefix and the `decimal` inputMode.
|
||||
*/
|
||||
export const CurrencyInput = React.forwardRef<HTMLInputElement, CurrencyInputProps>(
|
||||
({ value, onChange, currency = 'USD', className, ...props }, ref) => {
|
||||
const symbol = currencySymbol(currency);
|
||||
|
||||
const display =
|
||||
value === null || value === undefined || value === '' ? '' : String(value);
|
||||
|
||||
return (
|
||||
<div className="relative">
|
||||
<span
|
||||
className="pointer-events-none absolute left-3 top-1/2 -translate-y-1/2 text-sm text-muted-foreground"
|
||||
aria-hidden
|
||||
>
|
||||
{symbol}
|
||||
</span>
|
||||
<Input
|
||||
ref={ref}
|
||||
type="number"
|
||||
inputMode="decimal"
|
||||
step="0.01"
|
||||
min="0"
|
||||
value={display}
|
||||
onChange={(e) => {
|
||||
const raw = e.target.value;
|
||||
if (raw === '') {
|
||||
onChange(null);
|
||||
return;
|
||||
}
|
||||
const n = Number(raw);
|
||||
onChange(Number.isFinite(n) ? n : null);
|
||||
}}
|
||||
className={cn('pl-9 tabular-nums', className)}
|
||||
{...props}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
},
|
||||
);
|
||||
CurrencyInput.displayName = 'CurrencyInput';
|
||||
48
src/components/shared/currency-select.tsx
Normal file
48
src/components/shared/currency-select.tsx
Normal file
@@ -0,0 +1,48 @@
|
||||
'use client';
|
||||
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select';
|
||||
import { SUPPORTED_CURRENCIES } from '@/lib/utils/currency';
|
||||
|
||||
interface CurrencySelectProps {
|
||||
value: string | undefined;
|
||||
onValueChange: (value: string) => void;
|
||||
disabled?: boolean;
|
||||
id?: string;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Select dropdown over the curated `SUPPORTED_CURRENCIES` list. Replaces
|
||||
* the free-text `<Input maxLength={3}>` price-currency spots so reps
|
||||
* can't typo "EURO" or "$$$" into a 3-letter ISO column.
|
||||
*/
|
||||
export function CurrencySelect({
|
||||
value,
|
||||
onValueChange,
|
||||
disabled,
|
||||
id,
|
||||
className,
|
||||
}: CurrencySelectProps) {
|
||||
return (
|
||||
<Select value={value} onValueChange={onValueChange} disabled={disabled}>
|
||||
<SelectTrigger id={id} className={className}>
|
||||
<SelectValue placeholder="Select currency" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{SUPPORTED_CURRENCIES.map((c) => (
|
||||
<SelectItem key={c.code} value={c.code}>
|
||||
<span className="tabular-nums mr-2">{c.symbol}</span>
|
||||
<span className="font-medium">{c.code}</span>
|
||||
<span className="text-muted-foreground ml-2 text-xs">{c.label}</span>
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user