mirror of
https://github.com/spliit-app/spliit.git
synced 2026-03-06 12:36:11 +01:00
Fix decimal separator issue in numeric form fields (#115)
* Revert 5b65b8f, fix comma issue with type="text" and onChange
* Fix comma issue in "paid for" input
* Run prettier autoformat
* Allow only digits and dots in currency inputs
* Fix behaviour in paidFor field
* Fix duplicated onChange prop
---------
Co-authored-by: Sebastien Castiel <sebastien@castiel.me>
This commit is contained in:
@@ -56,6 +56,17 @@ export type Props = {
|
|||||||
runtimeFeatureFlags: RuntimeFeatureFlags
|
runtimeFeatureFlags: RuntimeFeatureFlags
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const enforceCurrencyPattern = (value: string) =>
|
||||||
|
value
|
||||||
|
// replace first comma with #
|
||||||
|
.replace(/[.,]/, '#')
|
||||||
|
// remove all other commas
|
||||||
|
.replace(/[.,]/g, '')
|
||||||
|
// change back # to dot
|
||||||
|
.replace(/#/, '.')
|
||||||
|
// remove all non-numeric and non-dot characters
|
||||||
|
.replace(/[^\d.]/g, '')
|
||||||
|
|
||||||
export function ExpenseForm({
|
export function ExpenseForm({
|
||||||
group,
|
group,
|
||||||
expense,
|
expense,
|
||||||
@@ -210,18 +221,22 @@ export function ExpenseForm({
|
|||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="amount"
|
name="amount"
|
||||||
render={({ field }) => (
|
render={({ field: { onChange, ...field } }) => (
|
||||||
<FormItem className="sm:order-3">
|
<FormItem className="sm:order-3">
|
||||||
<FormLabel>Amount</FormLabel>
|
<FormLabel>Amount</FormLabel>
|
||||||
<div className="flex items-baseline gap-2">
|
<div className="flex items-baseline gap-2">
|
||||||
<span>{group.currency}</span>
|
<span>{group.currency}</span>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Input
|
<Input
|
||||||
|
{...field}
|
||||||
className="text-base max-w-[120px]"
|
className="text-base max-w-[120px]"
|
||||||
type="number"
|
type="text"
|
||||||
inputMode="decimal"
|
inputMode="decimal"
|
||||||
step={0.01}
|
step={0.01}
|
||||||
placeholder="0.00"
|
placeholder="0.00"
|
||||||
|
onChange={(event) =>
|
||||||
|
onChange(enforceCurrencyPattern(event.target.value))
|
||||||
|
}
|
||||||
onClick={(e) => e.currentTarget.select()}
|
onClick={(e) => e.currentTarget.select()}
|
||||||
{...field}
|
{...field}
|
||||||
/>
|
/>
|
||||||
@@ -428,7 +443,7 @@ export function ExpenseForm({
|
|||||||
),
|
),
|
||||||
)}
|
)}
|
||||||
className="text-base w-[80px] -my-2"
|
className="text-base w-[80px] -my-2"
|
||||||
type="number"
|
type="text"
|
||||||
disabled={
|
disabled={
|
||||||
!field.value?.some(
|
!field.value?.some(
|
||||||
({ participant }) =>
|
({ participant }) =>
|
||||||
@@ -448,7 +463,9 @@ export function ExpenseForm({
|
|||||||
? {
|
? {
|
||||||
participant: id,
|
participant: id,
|
||||||
shares:
|
shares:
|
||||||
event.target.value,
|
enforceCurrencyPattern(
|
||||||
|
event.target.value,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
: p,
|
: p,
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -51,8 +51,7 @@ export const expenseFormSchema = z
|
|||||||
[
|
[
|
||||||
z.number(),
|
z.number(),
|
||||||
z.string().transform((value, ctx) => {
|
z.string().transform((value, ctx) => {
|
||||||
const normalizedValue = value.replace(/,/g, '.')
|
const valueAsNumber = Number(value)
|
||||||
const valueAsNumber = Number(normalizedValue)
|
|
||||||
if (Number.isNaN(valueAsNumber))
|
if (Number.isNaN(valueAsNumber))
|
||||||
ctx.addIssue({
|
ctx.addIssue({
|
||||||
code: z.ZodIssueCode.custom,
|
code: z.ZodIssueCode.custom,
|
||||||
|
|||||||
Reference in New Issue
Block a user