diff --git a/app/src/components/inventory/FieldCell.js b/app/src/components/inventory/FieldCell.js index dd249df7..eececae7 100644 --- a/app/src/components/inventory/FieldCell.js +++ b/app/src/components/inventory/FieldCell.js @@ -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 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 ; } if (field.type === 'number' || field.type === 'calories') { // `calories` is a number (kcal per unit) that the Summary's ration estimate detects by type. - return ; + return ; } if (field.type === 'quantity') { @@ -58,7 +90,7 @@ export function FieldCell({field, value, unitValue, onChange, onUnitChange, onEn return 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 ; + return ; } // text / fallback (supports a datalist via listId, e.g. catalog name suggestions on the Name field). - return ; + return ; } // The list of selectable unit groups, re-exported for the field editor. diff --git a/app/src/components/inventory/FieldSchemaEditor.test.js b/app/src/components/inventory/FieldSchemaEditor.test.js new file mode 100644 index 00000000..98253ee7 --- /dev/null +++ b/app/src/components/inventory/FieldSchemaEditor.test.js @@ -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(); + 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(); + 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']); + }); +}); diff --git a/app/src/components/inventory/InventoryTable.js b/app/src/components/inventory/InventoryTable.js index d75ae9c0..45a17b75 100644 --- a/app/src/components/inventory/InventoryTable.js +++ b/app/src/components/inventory/InventoryTable.js @@ -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} /> - : startEdit(item)} style={{cursor: 'pointer'}}> + : startEdit(item, f.key)} style={{cursor: 'pointer'}}> {formatValue(item, f)} } )} diff --git a/app/src/components/inventory/InventoryTable.test.js b/app/src/components/inventory/InventoryTable.test.js index d49b26fe..77fe3a0c 100644 --- a/app/src/components/inventory/InventoryTable.test.js +++ b/app/src/components/inventory/InventoryTable.test.js @@ -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(); + + 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(); + + 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', () => {