import React from "react"; import {Input, Select} from "semantic-ui-react"; import {ALL_UNITS, 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. // // Props: // field - {key, label, type, unit?, options?} // value - the current item value for field.key // unitValue- the current unit (for quantity fields; stored under `${key}_unit`) // onChange(value) - value changed // 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}) { const handleKeyDown = (e) => { if (e.key === 'Enter' && onEnter) { e.preventDefault(); onEnter(); } }; const common = { value: value ?? '', onChange: (e, data) => onChange(data ? data.value : e.target.value), onKeyDown: handleKeyDown, 'aria-label': field.label, name: field.key, }; if (field.type === 'select') { const options = (field.options || []).map(o => ({key: o, value: o, text: o})); // 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 ; } if (field.type === 'quantity') { const unitOptions = ALL_UNITS.map(u => ({key: u, value: u, text: u})); return onChange(e.target.value)} onKeyDown={handleKeyDown} aria-label={field.label} label={; } // text / fallback (supports a datalist via listId, e.g. catalog name suggestions on the Name field). return ; } // The list of selectable unit groups, re-exported for the field editor. export {UNIT_GROUPS};