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