Accept comma as decimal separator in numeric inputs

iOS decimal keyboards in many locales only offer a comma; type=number
inputs report an empty value for it, snapping the field to 0. Numeric
fields (weight, speed, etc.) are now text inputs with inputMode=decimal
backed by a shared NumField component that normalizes ',' to '.' and
keeps partial input like "33," alive while typing.

Fixes #13
This commit is contained in:
Duarte Santos committed 2026-07-22 00:03:48 +02:00
1 parent c05b0756ec
commit 30fd3a84a1
5 files changed
+43 -10

No files matched your search

+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "opengym-frontend",
"private": true,
"version": "1.1.1",
"version": "1.2.1",
"type": "module",
"scripts": {
"dev": "vite",
+31
View File
@@ -0,0 +1,31 @@
import { useRef, useState } from 'react'
// Numeric input that accepts "," as decimal separator — iOS decimal keypads in many
// locales only offer a comma, and type="number" reports "" for it (value snaps to 0).
// Keeps a local string draft while focused so partial input like "33," survives typing.
export default function NumField({ value, onChange, decimal = true, ...rest }) {
const [draft, setDraft] = useState(null)
const committed = useRef(null)
// external change (e.g. −/+ buttons) while a draft is showing → drop the draft
if (draft !== null && committed.current !== value) { setDraft(null); committed.current = null }
const commit = raw => {
let s = raw.replace(/,/g, '.').replace(/[^0-9.]/g, '')
const i = s.indexOf('.')
if (i !== -1) s = decimal ? s.slice(0, i + 1) + s.slice(i + 1).replace(/\./g, '') : s.slice(0, i)
const n = s === '' || s === '.' ? 0 : Math.max(0, parseFloat(s))
committed.current = n
setDraft(s)
onChange(n)
}
return (
<input
type="text"
inputMode={decimal ? 'decimal' : 'numeric'}
value={draft ?? (value ?? '')}
onFocus={e => e.target.select()}
onChange={e => commit(e.target.value)}
onBlur={() => { setDraft(null); committed.current = null }}
{...rest}
/>
)
}
+3 -7
View File
@@ -1,16 +1,12 @@
import NumField from './NumField.jsx'
// Number field with −/+ buttons; tapping the field selects its content so typing replaces.
export default function Stepper({ value, step = 1, onChange, decimal = true, className = '', label }) {
const set = v => onChange(Math.max(0, Math.round((v || 0) * 100) / 100))
const inner = (
<div className={'step ' + className}>
<button onClick={() => set((+value || 0) - step)}>−</button>
<input
type="number"
inputMode={decimal ? 'decimal' : 'numeric'}
value={value}
onFocus={e => e.target.select()}
onChange={e => onChange(e.target.value === '' ? 0 : Math.max(0, parseFloat(e.target.value) || 0))}
/>
<NumField value={value} decimal={decimal} onChange={onChange} />
<button onClick={() => set((+value || 0) + step)}>+</button>
</div>
)
+2 -2
View File
@@ -8,6 +8,7 @@ import { fmtNum, fmtDate, todayISO, DAYN } from '../lib/format.js'
import { beep, vibrate } from '../lib/sound.js'
import { t } from '../lib/i18n.js'
import Media from '../components/Media.jsx'
import NumField from '../components/NumField.jsx'
import { startFlow, exercisePicker, exConfigSheet, exerciseDetailSheet, topWeightSheet, finishWorkout, workoutCompleteSheet, confirmSheet } from '../sheets.jsx'
/* ---------- start chooser (no active workout) ---------- */
@@ -63,8 +64,7 @@ function ExerciseBlock({ entryIdx, compact, onToggle, onField, onBumpAll, onAddS
const cell = (s, i, col, cls) => (
<div className={'step ' + cls}>
<button onClick={() => onField(i, col.f, Math.max(0, Math.round(((s[col.f] || 0) - col.step) * 100) / 100))}>−</button>
<input type="number" inputMode={col.dec ? 'decimal' : 'numeric'} value={s[col.f] ?? ''} onFocus={e => e.target.select()}
onChange={e => onField(i, col.f, e.target.value === '' ? 0 : Math.max(0, (col.dec ? parseFloat(e.target.value) : Math.round(parseFloat(e.target.value))) || 0))} />
<NumField decimal={col.dec} value={s[col.f] ?? ''} onChange={v => onField(i, col.f, v)} />
<button onClick={() => onField(i, col.f, Math.max(0, Math.round(((s[col.f] || 0) + col.step) * 100) / 100))}>+</button>
</div>
)