Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 2x 2x 2x 2x 2x 2x 7x 7x 7x 7x 7x 7x 7x 7x 1x 6x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 6x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 6x 7x 7x 7x 7x 7x 7x 7x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 6x 6x 6x 6x 12x 12x 12x 6x 6x 6x 1x 1x 1x 1x 6x 6x 7x 7x 7x 7x | /**
* ItemForm - Multi-field form for creating/editing inventory items
*
* @module dialogs/ItemFormDialog/ItemForm
* @description
* Renders form fields for item creation/editing:
* supplier (Autocomplete) → name → code → quantity → price → reason (create only).
*
* Each field connects to RHF control with validation errors displayed inline.
* Reason dropdown only visible in create mode (when !initial?.id).
* Uses shared form state from useItemForm hook.
*/
import {
Box,
TextField,
Autocomplete,
FormControl,
InputLabel,
Select,
MenuItem,
Alert,
Tooltip,
} from '@mui/material';
import { useTranslation } from 'react-i18next';
import type { SupplierOption } from '../../../../api/analytics/types';
import type { UpsertItemForm } from '../../../../api/inventory/validation';
import type { InventoryRow } from '../../../../api/inventory';
import type { UseItemFormReturn } from './useItemForm';
/**
* ItemForm - Render all form fields
*
* @param state - Complete form state and handlers from useItemForm
* @param initial - Initial item data for checking edit mode
*
* @enterprise
* - Error banner at top for generic form errors
* - Supplier Autocomplete fully controlled to prevent desync
* - Code field read-only with helpful tooltip
* - Quantity and price with numeric constraints
* - Reason dropdown only shown in create mode (no initial?.id)
*/
export function ItemForm({
state,
initial,
}: {
state: UseItemFormReturn;
initial?: InventoryRow | null;
}) {
const { t } = useTranslation(['common', 'inventory', 'errors']);
const CREATE_REASON_OPTIONS = [
{ value: 'INITIAL_STOCK', i18nKey: 'stockReasons.initial_stock' },
{ value: 'MANUAL_UPDATE', i18nKey: 'stockReasons.manual_update' },
] as const;
return (
<Box sx={{ display: 'grid', gap: 2, mt: 1 }}>
{/* Error banner for non-field errors */}
{state.formError && <Alert severity="error">{state.formError}</Alert>}
{/* Supplier - Controlled Autocomplete */}
<Autocomplete<SupplierOption, false, false, false>
options={state.suppliers}
value={state.supplierValue}
onChange={(_, opt) => {
state.setSupplierValue(opt);
// Update RHF with supplier ID
const nextSupplierId: UpsertItemForm['supplierId'] =
opt ? (opt.id as UpsertItemForm['supplierId']) : ('' as UpsertItemForm['supplierId']);
state.setValue('supplierId', nextSupplierId, { shouldValidate: true });
}}
getOptionLabel={(o) => o.label}
isOptionEqualToValue={(a, b) => String(a.id) === String(b.id)}
renderInput={(p) => (
<TextField
{...p}
label={t('inventory:table.supplier', 'Supplier')}
error={!!state.formState.errors.supplierId}
helperText={typeof state.formState.errors.supplierId?.message === 'string'
? state.formState.errors.supplierId.message
: ''}
/>
)}
/>
{/* Name field */}
<TextField
label={t('inventory:table.name', 'Item')}
{...state.register('name')}
error={!!state.formState.errors.name}
helperText={typeof state.formState.errors.name?.message === 'string'
? state.formState.errors.name.message
: ''}
/>
{/* Code field - Read-only with tooltip */}
<Tooltip title={t('inventory:fields.codeReadOnlyHint', 'Optional for now')}>
<TextField
label={t('inventory:table.code', 'Code / SKU')}
{...state.register('code')}
InputProps={{ readOnly: true }}
error={!!state.formState.errors.code}
helperText={typeof state.formState.errors.code?.message === 'string'
? state.formState.errors.code.message
: ''}
/>
</Tooltip>
{/* Quantity field */}
<TextField
label={t('inventory:table.quantity', 'Initial Stock')}
type="number"
slotProps={{ htmlInput: { min: 0 } }}
{...state.register('quantity', { valueAsNumber: true })}
error={!!state.formState.errors.quantity}
helperText={typeof state.formState.errors.quantity?.message === 'string'
? state.formState.errors.quantity.message
: ''}
/>
{/* Price field */}
<TextField
label={t('inventory:table.price', 'Price')}
type="number"
slotProps={{ htmlInput: { min: 0, step: 0.01 } }}
{...state.register('price', { valueAsNumber: true })}
error={!!state.formState.errors.price}
helperText={typeof state.formState.errors.price?.message === 'string'
? state.formState.errors.price.message
: ''}
InputProps={{
startAdornment: <span style={{ marginRight: '8px' }}>€</span>,
}}
/>
{/* Reason dropdown - Create mode only */}
{!initial?.id && (
<FormControl error={!!state.formState.errors.reason}>
<InputLabel id="reason-label">
{t('inventory:fields.reasonLabel', 'Reason')}
</InputLabel>
<Select
labelId="reason-label"
label={t('inventory:fields.reasonLabel', 'Reason')}
value={state.watch('reason') ?? 'INITIAL_STOCK'}
onChange={(e) =>
state.setValue('reason', e.target.value as UpsertItemForm['reason'], {
shouldValidate: true,
})
}
>
{/* Render all reason options */}
{CREATE_REASON_OPTIONS.map((opt) => (
<MenuItem key={opt.value} value={opt.value}>
{t(`inventory:${opt.i18nKey}`, opt.value)}
</MenuItem>
))}
</Select>
{state.formState.errors.reason?.message && (
<Box sx={{ mt: 0.5, color: 'error.main', fontSize: 12 }}>
{typeof state.formState.errors.reason.message === 'string'
? state.formState.errors.reason.message
: ''}
</Box>
)}
</FormControl>
)}
</Box>
);
}
|