Allow arithmetic expressions in inventory number fields

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) <noreply@anthropic.com>
This commit is contained in:
Roland 2026-06-19 15:07:29 -06:00
parent 305c73921e
commit e7c737fe0c
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
5 changed files with 138 additions and 7 deletions

View file

@ -124,6 +124,48 @@ describe('InventoryTable count by weight', () => {
expect(within(row).getByLabelText('Count').value).toBe('');
});
test('a math expression in a weight evaluates on blur and recomputes the count', () => {
render(<InventoryTable slug='s' fields={FIELDS} items={[]} locations={[]} onChange={jest.fn()}/>);
fireEvent.change(screen.getByLabelText('Unit Weight'), {target: {value: '5'}});
const total = screen.getByLabelText('Total Weight');
fireEvent.change(total, {target: {value: '400 - 20'}}); // weigh a 400 g box, subtract the 20 g box
fireEvent.blur(total);
expect(screen.getByLabelText('Total Weight').value).toBe('380');
expect(screen.getByLabelText('Count').value).toBe('76'); // 380 / 5
});
test('Enter resolves a weight expression before the row can be submitted', () => {
const onChange = jest.fn();
render(<InventoryTable slug='s' fields={FIELDS} items={[]} locations={[]} onChange={onChange}/>);
fireEvent.change(screen.getByLabelText('Unit Weight'), {target: {value: '5'}});
const total = screen.getByLabelText('Total Weight');
fireEvent.change(total, {target: {value: '400 - 20'}});
// First Enter resolves the expression; the row is NOT submitted yet.
fireEvent.keyDown(total, {key: 'Enter'});
expect(onChange).not.toHaveBeenCalled();
expect(screen.getByLabelText('Total Weight').value).toBe('380');
// Second Enter (now a plain number) submits the resolved row with the recomputed count.
fireEvent.keyDown(screen.getByLabelText('Total Weight'), {key: 'Enter'});
const item = onChange.mock.calls[0][0][0];
expect(item.total_weight).toBe('380');
expect(item.count).toBe('76');
});
test('Enter submits an existing row from an unfilled numeric cell (null backing value)', () => {
const onChange = jest.fn();
// count is absent (undefined) — an item field that was never filled in.
const items = [{id: 1, name: 'Salt'}];
render(<InventoryTable slug='s' fields={FIELDS} items={items} locations={[]} onChange={onChange}/>);
fireEvent.click(screen.getByText('Salt')); // enter edit mode
const row = screen.getByDisplayValue('Salt').closest('tr');
// A null/undefined value must not be mistaken for an unresolved expression; Enter submits immediately.
fireEvent.keyDown(within(row).getByLabelText('Count'), {key: 'Enter'});
expect(onChange).toHaveBeenCalledTimes(1);
});
test('reducing a weight below one item shows 0, not the stale count', () => {
const items = [{id: 1, name: 'Screws', unit_weight: '5', unit_weight_unit: 'g',
total_weight: '1000', total_weight_unit: 'g', count: '200'}];