Select an item's value when its cell is clicked for editing
Some checks are pending
CodeQL / Analyze (push) Waiting to run
Controller Tests / controller-tests (push) Waiting to run
Jest Tests / jest-tests (push) Waiting to run

Clicking a cell in the Items table now focuses that field's input and selects
its contents, so the user can type a replacement value immediately. FieldCell
gains an `autoFocus` prop (focus + select on mount via a merged ref that still
supports the draft row's first-field ref); InventoryTable tracks which cell was
clicked and flags the matching FieldCell.

Also add a FieldSchemaEditor test covering the select-options comma fix.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Roland 2026-06-19 13:47:32 -06:00
parent 6dd8e43635
commit 447e4a40df
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
4 changed files with 102 additions and 8 deletions

View file

@ -13,7 +13,38 @@ import {ALL_UNITS, UNIT_GROUPS} from "./units";
// onUnitChange(unit) - quantity unit changed
// onEnter() - Enter pressed (submit row)
// inputRef - ref attached to the primary input
export function FieldCell({field, value, unitValue, onChange, onUnitChange, onEnter, inputRef, listId}) {
// autoFocus - on mount, focus the input and select its contents (for rapid inline edits)
export function FieldCell({field, value, unitValue, onChange, onUnitChange, onEnter, inputRef, listId, autoFocus}) {
// Merge the parent's `inputRef` (used to focus the draft row's first field) with a local ref we use to
// focus + select this cell's contents when the user clicks it to edit.
const localRef = React.useRef(null);
const setRef = React.useCallback((node) => {
localRef.current = node;
if (typeof inputRef === 'function') {
inputRef(node);
} else if (inputRef) {
inputRef.current = node;
}
}, [inputRef]);
React.useEffect(() => {
if (!autoFocus) {
return;
}
const node = localRef.current;
// Semantic's Input exposes the DOM <input> via `inputRef`; selecting its contents lets the user type over
// the value immediately. Other editors (e.g. Select) just take focus.
const dom = node && node.inputRef && node.inputRef.current;
if (dom) {
dom.focus();
if (dom.select) {
dom.select();
}
} else if (node && node.focus) {
node.focus();
}
}, [autoFocus]);
const handleKeyDown = (e) => {
if (e.key === 'Enter' && onEnter) {
e.preventDefault();
@ -34,6 +65,7 @@ export function FieldCell({field, value, unitValue, onChange, onUnitChange, onEn
// Allow clearing.
return <Select
fluid search clearable selection
ref={setRef}
name={field.key}
options={options}
placeholder={field.label}
@ -45,12 +77,12 @@ export function FieldCell({field, value, unitValue, onChange, onUnitChange, onEn
}
if (field.type === 'date') {
return <Input {...common} type='date' fluid ref={inputRef}/>;
return <Input {...common} type='date' fluid ref={setRef}/>;
}
if (field.type === 'number' || field.type === 'calories') {
// `calories` is a number (kcal per unit) that the Summary's ration estimate detects by type.
return <Input {...common} type='number' fluid ref={inputRef}/>;
return <Input {...common} type='number' fluid ref={setRef}/>;
}
if (field.type === 'quantity') {
@ -58,7 +90,7 @@ export function FieldCell({field, value, unitValue, onChange, onUnitChange, onEn
return <Input
type='number'
fluid
ref={inputRef}
ref={setRef}
name={field.key}
value={value ?? ''}
onChange={(e) => onChange(e.target.value)}
@ -79,11 +111,11 @@ export function FieldCell({field, value, unitValue, onChange, onUnitChange, onEn
if (field.type === 'location') {
// Native datalist autocomplete fed by all inventories' locations (basement, attic, ...). Using a datalist
// (rather than a Dropdown) keeps it a real text input, so the Tab/Enter row-entry flow still works.
return <Input {...common} fluid ref={inputRef} list={listId}/>;
return <Input {...common} fluid ref={setRef} list={listId}/>;
}
// text / fallback (supports a datalist via listId, e.g. catalog name suggestions on the Name field).
return <Input {...common} fluid ref={inputRef} list={listId}/>;
return <Input {...common} fluid ref={setRef} list={listId}/>;
}
// The list of selectable unit groups, re-exported for the field editor.

View file

@ -0,0 +1,35 @@
import React from "react";
import {fireEvent, render, screen} from "@testing-library/react";
import {FieldSchemaEditor} from "./FieldSchemaEditor";
const FIELDS = [
{key: 'name', label: 'Name', type: 'text', order: 0},
{key: 'category', label: 'Category', type: 'select', options: [], order: 1},
];
describe('FieldSchemaEditor select options', () => {
test('typing a comma is preserved while editing (does not get swallowed)', () => {
render(<FieldSchemaEditor fields={FIELDS} open={true} onClose={jest.fn()} onSave={jest.fn()}/>);
const optionsInput = screen.getByPlaceholderText('comma,separated,options');
// Typing the comma after the first option must not be stripped — otherwise a second option can't be started.
fireEvent.change(optionsInput, {target: {value: 'screws,'}});
expect(optionsInput.value).toBe('screws,');
fireEvent.change(optionsInput, {target: {value: 'screws,nails'}});
expect(optionsInput.value).toBe('screws,nails');
});
test('options are trimmed and emptied-out on save', async () => {
const onSave = jest.fn();
render(<FieldSchemaEditor fields={FIELDS} open={true} onClose={jest.fn()} onSave={onSave}/>);
const optionsInput = screen.getByPlaceholderText('comma,separated,options');
fireEvent.change(optionsInput, {target: {value: 'screws, nails ,'}});
fireEvent.click(screen.getByText('Save Fields'));
const savedFields = onSave.mock.calls[0][0];
const category = savedFields.find(f => f.key === 'category');
expect(category.options).toEqual(['screws', 'nails']);
});
});

View file

@ -53,6 +53,7 @@ export function InventoryTable({slug, fields, items, locations, catalog, search,
const [edits, setEdits] = useState({}); // itemId -> edited copy
const [selected, setSelected] = useState(new Set());
const [sort, setSort] = useState(null); // {key, dir: 'asc'|'desc'} or null for entry order
const [focusCell, setFocusCell] = useState(null); // {id, key}: the cell to focus + select on entering edit
const firstInputRef = useRef(null);
const hasLocationField = fields.some(f => f.type === 'location');
@ -96,6 +97,7 @@ export function InventoryTable({slug, fields, items, locations, catalog, search,
setEdits({});
setSelected(new Set());
setSort(null);
setFocusCell(null);
}, [slug, fields]);
// Expired = any date-type field strictly before the start of today (shared with the mobile view).
@ -158,7 +160,10 @@ export function InventoryTable({slug, fields, items, locations, catalog, search,
focusFirst();
};
const startEdit = (item) => setEdits(prev => ({...prev, [item.id]: {...item}}));
const startEdit = (item, key) => {
setEdits(prev => ({...prev, [item.id]: {...item}}));
setFocusCell({id: item.id, key}); // the FieldCell for this key auto-focuses and selects its contents
};
const setEditValue = (id, key, value) =>
setEdits(prev => ({...prev, [id]: {...prev[id], [key]: value}}));
@ -235,8 +240,9 @@ export function InventoryTable({slug, fields, items, locations, catalog, search,
onUnitChange={v => setEditValue(item.id, `${f.key}_unit`, v)}
onEnter={() => saveEdit(item.id)}
listId={listIdFor(f)}
autoFocus={focusCell && focusCell.id === item.id && focusCell.key === f.key}
/>
: <span onClick={() => startEdit(item)} style={{cursor: 'pointer'}}>
: <span onClick={() => startEdit(item, f.key)} style={{cursor: 'pointer'}}>
{formatValue(item, f)}
</span>}
</TableCell>)}

View file

@ -55,6 +55,27 @@ describe('InventoryTable keyboard entry', () => {
const editInputs = screen.getAllByLabelText('Name').map(i => i.value);
expect(editInputs).toContain('Salt');
});
test('clicking a cell focuses its input with the contents selected', () => {
const items = [{id: 1, name: 'Salt', count: '5'}];
render(<InventoryTable slug='food-storage' fields={FIELDS} items={items} locations={[]} onChange={jest.fn()}/>);
fireEvent.click(screen.getByText('Salt'));
const input = screen.getAllByLabelText('Name').find(i => i.value === 'Salt');
// The clicked cell's input is focused and its whole value is selected, ready to be typed over.
expect(document.activeElement).toBe(input);
expect(input.selectionStart).toBe(0);
expect(input.selectionEnd).toBe('Salt'.length);
});
test('clicking a different field focuses that field, not the first one', () => {
const items = [{id: 1, name: 'Salt', count: '5'}];
render(<InventoryTable slug='food-storage' fields={FIELDS} items={items} locations={[]} onChange={jest.fn()}/>);
fireEvent.click(screen.getByText('5')); // the Count cell
const countInput = screen.getAllByLabelText('Count').find(i => i.value === '5');
expect(document.activeElement).toBe(countInput);
});
});
describe('InventoryTable sorting', () => {