diff --git a/frontend/src/components/ui.jsx b/frontend/src/components/ui.jsx index 66fdc18..6299e4b 100644 --- a/frontend/src/components/ui.jsx +++ b/frontend/src/components/ui.jsx @@ -18,12 +18,18 @@ import Icon from './Icon.jsx' /* ============================ text ============================ */ +// Width of a numeric string in `ch`, for a field sized to its own digits (NumberField's +// `fit`): with tabular numerals every digit is exactly one ch, a decimal point roughly half. +export const numWidthCh = s => Math.max(1, [...s].reduce((n, c) => n + (c === '.' ? 0.5 : 1), 0)) + // Numeric input accepting "," 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. // `nullable` is for fields where "nothing entered" and 0 mean different things (RIR: a // logged 0 is a set taken to failure). Those clear back to null instead of snapping to 0. -export function NumberField({ value, onChange, decimal = true, nullable = false, className = '', ...rest }) { +// `fit` sizes the field to the digits on screen instead of filling its cell — for the big +// weight read-out, where the unit sits right beside the number. +export function NumberField({ value, onChange, decimal = true, nullable = false, fit = false, className = '', ...rest }) { const [draft, setDraft] = useState(null) const committed = useRef(null) // null and undefined are the same "empty" here — a nullable field's key is dropped once cleared. @@ -37,12 +43,16 @@ export function NumberField({ value, onChange, decimal = true, nullable = false, setDraft(s) onChange(n) } + const shown = draft ?? (value ?? '') return ( e.target.select()} onChange={e => commit(e.target.value)} onBlur={() => { setDraft(null); committed.current = null }} diff --git a/frontend/src/components/ui.test.jsx b/frontend/src/components/ui.test.jsx index 0c89d86..80db7fc 100644 --- a/frontend/src/components/ui.test.jsx +++ b/frontend/src/components/ui.test.jsx @@ -1,8 +1,8 @@ // @vitest-environment happy-dom -import React, { act } from 'react' +import React, { act, useState } from 'react' import { createRoot } from 'react-dom/client' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import { Slider, Stepper, SLIDER_GRAB_PX } from './ui.jsx' +import { Slider, Stepper, NumberField, numWidthCh, SLIDER_GRAB_PX } from './ui.jsx' globalThis.IS_REACT_ACT_ENVIRONMENT = true @@ -106,3 +106,41 @@ describe('Stepper', () => { expect(onChange).toHaveBeenCalledWith(11) }) }) + +describe('NumberField fit', () => { + // A field that hugs its digits, for the big weight read-out where the unit sits right beside + // the number. Digits are one ch each under tabular numerals; a decimal point is narrower. + it('measures digits as one ch and a decimal point as half', () => { + expect(numWidthCh('82')).toBe(2) + expect(numWidthCh('82.5')).toBe(3.5) + expect(numWidthCh('')).toBe(1) + }) + + // The width follows what is on screen while typing (the draft), not the last committed + // number — otherwise clearing the field would leave a 2ch-wide box around a lone caret and + // the digits typed next would be clipped until the parent caught up. + it('tracks the draft on screen while typing, and stays unsized without fit', () => { + const type = (el, value) => { + Object.getOwnPropertyDescriptor(el.constructor.prototype, 'value').set.call(el, value) + el.dispatchEvent(new Event('input', { bubbles: true })) + } + function Harness({ fit }) { + const [v, setV] = useState(70) + return + } + act(() => root.render()) + const input = host.querySelector('input') + expect(input.style.width).toBe('2ch') + + act(() => type(input, '')) + expect(input.value).toBe('') + expect(input.style.width).toBe('1ch') + + act(() => type(input, '82,5')) + expect(input.value).toBe('82.5') + expect(input.style.width).toBe('3.5ch') + + act(() => root.render()) + expect(host.querySelector('input').style.width).toBe('') + }) +}) diff --git a/frontend/src/index.css b/frontend/src/index.css index a8bdf41..da086b7 100644 --- a/frontend/src/index.css +++ b/frontend/src/index.css @@ -1144,6 +1144,12 @@ body.chips-dragging *{cursor:inherit} font-variant-numeric:tabular-nums;line-height:1; } .bw-read .u{font-size:19px;color:var(--label-2);font-weight:400;letter-spacing:-.01em} +.bw-read .num{ + display:inline-block;vertical-align:baseline;background:none;border:none;outline:none; + padding:0;margin:0;color:inherit;font:inherit;letter-spacing:inherit; + font-variant-numeric:tabular-nums;text-align:center;cursor:text; + -moz-appearance:textfield;appearance:textfield; +} .bwin{display:flex;align-items:baseline;justify-content:center;gap:7px;margin:16px 0} .bwin input{ width:150px;background:none;border:none;outline:none;text-align:right; diff --git a/frontend/src/sheets.jsx b/frontend/src/sheets.jsx index 2c5ae7f..128bf1c 100644 --- a/frontend/src/sheets.jsx +++ b/frontend/src/sheets.jsx @@ -162,7 +162,8 @@ export const starterPlanSheet = () => ui().openSheet(close => Math.max(W_LO, Math.min(W_HI, Math.round((x || 0) * 10) / 10)) const sv = Math.max(W_LO, Math.min(W_HI, value)) const onSlide = v => setValue(clamp(v)) + const onType = v => setValue(v) + return <>
-
{fmtNum(value)} {unit}
+
diff --git a/frontend/src/sheets.weight-input.test.jsx b/frontend/src/sheets.weight-input.test.jsx new file mode 100644 index 0000000..4ced7c6 --- /dev/null +++ b/frontend/src/sheets.weight-input.test.jsx @@ -0,0 +1,100 @@ +// @vitest-environment happy-dom +import React, { act } from 'react' +import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import { createRoot } from 'react-dom/client' +import { useStore } from './store/useStore.js' +import { useUI } from './store/useUI.js' +import { bwSheet } from './sheets.jsx' + +// The big weight read-out (body weight, goal, top weight after an exercise) used to be text: +// the only ways to a number were the slider, ±0.1 and the ±0.5/±1 chips, so 72.3 → 85.7 was a +// drag you then corrected tap by tap. It is a field now. These pin the two things that make +// typing into it usable — a half-typed value is not clamped back at you, and the controls +// around it carry on from what you typed — plus the ceiling: the slider stops at 300 kg, a +// typed weight does not. +const mounted = [] + +function type(el, value) { + Object.getOwnPropertyDescriptor(el.constructor.prototype, 'value').set.call(el, value) + el.dispatchEvent(new Event('input', { bubbles: true })) +} + +function renderBw() { + bwSheet() + const sheet = useUI.getState().sheets.at(-1) + const host = document.createElement('div') + document.body.appendChild(host) + const root = createRoot(host) + mounted.push(root) + act(() => root.render(sheet.render(() => useUI.getState().closeSheet(sheet.id)))) + const input = host.querySelector('.bw-read input') + const slider = host.querySelector('.sld') + const button = label => [...host.querySelectorAll('button')].find(b => (b.getAttribute('aria-label') || b.textContent.trim()) === label) + return { host, input, slider, button } +} + +describe('weight read-out is a typable field', () => { + beforeEach(() => { + globalThis.IS_REACT_ACT_ENVIRONMENT = true + useUI.setState({ sheets: [] }) + useStore.setState(s => ({ S: { ...s.S, unit: 'kg', bodyweight: [] } })) + document.body.innerHTML = '' + }) + + afterEach(() => { + act(() => { mounted.splice(0).forEach(root => root.unmount()) }) + }) + + it('shows the current weight in a decimal field with the unit beside it', () => { + const { input, host } = renderBw() + expect(input).toBeTruthy() + expect(input.value).toBe('70') + expect(input.getAttribute('inputmode')).toBe('decimal') + expect(input.getAttribute('aria-label')).toBe('Weight (kg)') + expect(host.querySelector('.bw-read .u').textContent.trim()).toBe('kg') + }) + + it('keeps a half-typed value on screen instead of clamping it, and moves the slider with it', () => { + const { input, slider } = renderBw() + act(() => type(input, '')) + expect(input.value).toBe('') + + act(() => type(input, '8')) + expect(input.value).toBe('8') + + act(() => type(input, '82,5')) + expect(input.value).toBe('82.5') + expect(slider.getAttribute('aria-valuenow')).toBe('82.5') + }) + + it('steps on from the typed value and saves what ends up on screen', () => { + const { input, button } = renderBw() + act(() => type(input, '82.5')) + act(() => button('plus 0.1').click()) + expect(input.value).toBe('82.6') + + act(() => button('Save').click()) + const bw = useStore.getState().S.bodyweight + expect(bw).toHaveLength(1) + expect(bw[0].w).toBe(82.6) + expect(useUI.getState().sheets).toHaveLength(0) + }) + + it('lets a typed weight sit past the slider ceiling rather than cutting it off', () => { + const { input, slider, button } = renderBw() + act(() => type(input, '320')) + expect(input.value).toBe('320') + expect(slider.getAttribute('aria-valuenow')).toBe('300') + + act(() => button('Save').click()) + expect(useStore.getState().S.bodyweight[0].w).toBe(320) + }) + + it('still refuses to save an empty field', () => { + const { input, button } = renderBw() + act(() => type(input, '')) + act(() => button('Save').click()) + expect(useStore.getState().S.bodyweight).toHaveLength(0) + expect(useUI.getState().sheets).toHaveLength(1) + }) +})