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.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
Number fields: a tap in Safari selects the value, so typing replaces it
Olivier7783d74fb3e8
diff --git a/frontend/src/components/ui.jsx b/frontend/src/components/ui.jsxindex fca6fa8c2..1b10e9103 100644--- a/frontend/src/components/ui.jsx+++ b/frontend/src/components/ui.jsx@@ -35,6 +35,11 @@ export const numWidthCh = s => Math.max(1, [...s].reduce((n, c) => n + (c === '.export function NumberField({ value, onChange, decimal = true, nullable = false, fit = false, className = '', ...rest }) {const [draft, setDraft] = useState(null)const committed = useRef(null)+ // Focus selects the number so typing replaces it. In WebKit (Safari, the iOS app) the release+ // of the tap that focused the field then moves the caret to the finger and drops that+ // selection: 65 typed over 62.5 read 6265.5. The first mouseup after focus keeps its default+ // off; a later tap on the focused field still places the caret to edit one digit.+ const justFocused = useRef(false)// null and undefined are the same "empty" here — a nullable field's key is dropped once cleared.if (draft !== null && (committed.current ?? null) !== (value ?? null)) { setDraft(null); committed.current = null }const commit = raw => {@@ -56,9 +61,10 @@ export function NumberField({ value, onChange, decimal = true, nullable = false,// `fit` hugs the digits on screen — a big read-out with its unit sitting right beside// it — where the default fills whatever cell the field is in.style={fit ? { width: numWidthCh(String(shown)) + 'ch' } : undefined}- onFocus={e => e.target.select()}+ onFocus={e => { justFocused.current = true; e.target.select() }}+ onMouseUp={e => { if (justFocused.current) { justFocused.current = false; e.preventDefault() } }}onChange={e => commit(e.target.value)}- onBlur={() => { setDraft(null); committed.current = null }}+ onBlur={() => { setDraft(null); committed.current = null; justFocused.current = false }}{...rest}/>)diff --git a/frontend/src/components/ui.test.jsx b/frontend/src/components/ui.test.jsxindex 488110bc3..96053f4fe 100644--- a/frontend/src/components/ui.test.jsx+++ b/frontend/src/components/ui.test.jsx@@ -225,3 +225,40 @@ describe('NumberField fit', () => {expect(host.querySelector('input').style.width).toBe('')})})++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;+ // what it can show is that the release of that first tap is kept from doing so.+ const mouseup = el => {+ const e = new MouseEvent('mouseup', { bubbles: true, cancelable: true })+ act(() => el.dispatchEvent(e))+ return e+ }+ const mount = () => {+ act(() => root.render(<NumberField value={62.5} onChange={() => {}} />))+ return host.querySelector('input')+ }++ it('keeps the selection through the release of the tap that focused it', () => {+ const input = mount()+ act(() => input.focus())+ expect(input.selectionStart).toBe(0)+ expect(input.selectionEnd).toBe(4)+ expect(mouseup(input).defaultPrevented).toBe(true)+ })++ it('lets a later tap on the focused field place the caret', () => {+ const input = mount()+ act(() => input.focus())+ mouseup(input)+ expect(mouseup(input).defaultPrevented).toBe(false)+ })++ it('forgets the focus once the field is left', () => {+ const input = mount()+ act(() => input.focus())+ act(() => input.blur())+ expect(mouseup(input).defaultPrevented).toBe(false)+ })+})