From e7c737fe0c70a75c11e87600db0238f6dfa3ce75 Mon Sep 17 00:00:00 2001 From: Roland Date: Fri, 19 Jun 2026 15:07:29 -0600 Subject: [PATCH] Allow arithmetic expressions in inventory number fields MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A user can type a calculation into any numeric inventory field (number, quantity magnitude, calories) and have it evaluated in place — e.g. weigh a 400 g box of nails, enter '400 - 20', and Tab/Enter swaps in 380. The result feeds dependent logic like count-by-weight automatically. - evaluateExpression() (units.js) reuses the scoped mathjs instance, guarded by a strict regex so only bare arithmetic (digits, + - * / ( ) and spaces) is evaluated; anything with a letter, a plain number, or unparseable input is left untouched. Float noise is rounded off. - FieldCell evaluates on blur (Tab/click-away) and on Enter: in a numeric field the first Enter resolves a pending expression, a second submits the row. - NUMERIC_TYPES is exported once from units.js and shared by the table and cell editors. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/src/components/inventory/FieldCell.js | 40 +++++++++++++++--- .../components/inventory/InventoryTable.js | 2 +- .../inventory/InventoryTable.test.js | 42 +++++++++++++++++++ app/src/components/inventory/units.js | 32 ++++++++++++++ app/src/components/inventory/units.test.js | 29 ++++++++++++- 5 files changed, 138 insertions(+), 7 deletions(-) diff --git a/app/src/components/inventory/FieldCell.js b/app/src/components/inventory/FieldCell.js index eececae7..c3493499 100644 --- a/app/src/components/inventory/FieldCell.js +++ b/app/src/components/inventory/FieldCell.js @@ -1,6 +1,6 @@ import React from "react"; import {Input, Select} from "semantic-ui-react"; -import {ALL_UNITS, UNIT_GROUPS} from "./units"; +import {ALL_UNITS, evaluateExpression, NUMERIC_TYPES, UNIT_GROUPS} from "./units"; // Renders the correct editor for a single field/value, wiring up keyboard navigation. The `inputRef` is attached // to the primary so the parent table can focus the first cell of a new row. @@ -45,13 +45,40 @@ export function FieldCell({field, value, unitValue, onChange, onUnitChange, onEn } }, [autoFocus]); + const numeric = NUMERIC_TYPES.includes(field.type); + + // Evaluate an arithmetic expression in this field and swap in the result (no-op for a plain number). Routed + // through onChange so dependent logic (e.g. count-by-weight) recomputes from the resolved value. + const resolveExpression = () => { + const evaluated = evaluateExpression(value); + // Compare against the raw value: evaluateExpression returns its input unchanged when there's no expression, + // so a null/undefined backing value (an unfilled item field) compares equal and never spuriously fires. + if (evaluated !== value) { + onChange(evaluated); + return true; + } + return false; + }; + const handleKeyDown = (e) => { - if (e.key === 'Enter' && onEnter) { + if (e.key !== 'Enter') { + return; + } + // In a numeric field, the first Enter resolves a pending expression ("400 - 20" -> "380"); a second Enter + // (now a plain number) submits the row as usual. + if (numeric && resolveExpression()) { + e.preventDefault(); + return; + } + if (onEnter) { e.preventDefault(); onEnter(); } }; + // Numeric fields accept arithmetic, so they are text inputs that evaluate on blur (Tab/click-away) and Enter. + const onBlur = numeric ? resolveExpression : undefined; + const common = { value: value ?? '', onChange: (e, data) => onChange(data ? data.value : e.target.value), @@ -81,20 +108,23 @@ export function FieldCell({field, value, unitValue, onChange, onUnitChange, onEn } if (field.type === 'number' || field.type === 'calories') { - // `calories` is a number (kcal per unit) that the Summary's ration estimate detects by type. - return ; + // `calories` is a number (kcal per unit) that the Summary's ration estimate detects by type. Text (not + // number) input so arithmetic expressions can be typed; evaluated on blur/Enter. + return ; } if (field.type === 'quantity') { const unitOptions = ALL_UNITS.map(u => ({key: u, value: u, text: u})); return onChange(e.target.value)} onKeyDown={handleKeyDown} + onBlur={onBlur} aria-label={field.label} label={