Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

NumberField: 8.5 typed into a whole-number field is 8, not 85

Duarte Santosd88ecbb0e6cf
diff --git a/frontend/src/components/ui.jsx b/frontend/src/components/ui.jsx
index 1b10e9103..28e292b71 100644
--- a/frontend/src/components/ui.jsx
+++ b/frontend/src/components/ui.jsx
@@ -45,8 +45,11 @@ export function NumberField({ value, onChange, decimal = true, nullable = false,
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 === '.' ? (nullable ? null : 0) : Math.max(0, parseFloat(s))
+ if (i !== -1) s = s.slice(0, i + 1) + s.slice(i + 1).replace(/\./g, '')
+ // A whole-number field keeps what was typed after a separator on screen until blur and
+ // counts only the part before it: cutting the separator off at each keystroke read 8.5 as 85.
+ const whole = !decimal && i !== -1 ? s.slice(0, i) : s
+ const n = whole === '' || whole === '.' ? (nullable ? null : 0) : Math.max(0, parseFloat(whole))
committed.current = n
setDraft(s)
onChange(n)
diff --git a/frontend/src/components/ui.test.jsx b/frontend/src/components/ui.test.jsx
index 96053f4fe..2dc5576d2 100644
--- a/frontend/src/components/ui.test.jsx
+++ b/frontend/src/components/ui.test.jsx
@@ -226,6 +226,31 @@ describe('NumberField fit', () => {
})
})
+describe('NumberField whole numbers', () => {
+ // Typed one key at a time, "8.5" in a reps field used to lose the "." on the second key and
+ // then read the 5 as a second digit: 85 reps.
+ it('takes 8.5 typed key by key as 8, not 85, and shows the whole number after blur', () => {
+ const changes = []
+ function Harness() {
+ const [v, setV] = useState(10)
+ return <NumberField decimal={false} value={v} onChange={n => { changes.push(n); setV(n) }} />
+ }
+ act(() => root.render(<Harness />))
+ const input = host.querySelector('input')
+ const type = value => act(() => {
+ Object.getOwnPropertyDescriptor(input.constructor.prototype, 'value').set.call(input, value)
+ input.dispatchEvent(new Event('input', { bubbles: true }))
+ })
+ type('8'); type(input.value + '.'); type(input.value + '5')
+ expect(changes).toEqual([8, 8, 8])
+ expect(input.value).toBe('8.5')
+ act(() => input.dispatchEvent(new FocusEvent('focusout', { bubbles: true })))
+ expect(input.value).toBe('8')
+ type('8,5')
+ expect(changes.at(-1)).toBe(8)
+ })
+})
+
describe('NumberField select on tap', () => {
// In WebKit the release of the tap that focused the field moves the caret and drops the
// focus-time selection, so typing 65 over 62.5 read 6265.5. happy-dom cannot move a caret;