diff --git a/src/components/pages/production/recording/form/RecordingForm.tsx b/src/components/pages/production/recording/form/RecordingForm.tsx index f387d45e..c8adef19 100644 --- a/src/components/pages/production/recording/form/RecordingForm.tsx +++ b/src/components/pages/production/recording/form/RecordingForm.tsx @@ -40,6 +40,9 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { const [selectedStocks, setSelectedStocks] = useState([]); const [selectedDepletions, setSelectedDepletions] = useState([]); + // Track which average weight field is being edited to prevent auto-calculation override + const [editingAverageIndex, setEditingAverageIndex] = useState(null); + // State for Location search and selection const [locationSearchValue, setLocationSearchValue] = useState(''); const [selectedLocation, setSelectedLocation] = useState(null); @@ -231,20 +234,28 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { } }, [formik.values.record_datetime]); - // Auto-calculate average weight when weight or qty changes + // Auto-calculate average weight when weight or qty changes (but not when editing average weight manually) useEffect(() => { - if (formik.values.body_weights) { - const updatedBodyWeights = formik.values.body_weights.map((weight) => ({ - ...weight, - average_weight: - weight.qty > 0 && weight.weight > 0 - ? Math.round(weight.weight / weight.qty) - : 0, - })); + if (formik.values.body_weights && editingAverageIndex === null) { + const updatedBodyWeights = formik.values.body_weights.map((weight, idx) => { + // Skip auto-calculation for the field being manually edited + if (idx === editingAverageIndex) { + return weight; + } + + return { + ...weight, + average_weight: + weight.qty > 0 && weight.weight > 0 + ? Math.round(weight.weight / weight.qty) + : 0, + }; + }); // Only update if values are different to avoid infinite loops const hasChanges = updatedBodyWeights.some( (updated, idx) => + idx !== editingAverageIndex && // Skip the field being edited updated.average_weight !== (formik.values.body_weights[idx]?.average_weight || 0) ); @@ -256,6 +267,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { }, [ formik.values.body_weights?.map((w) => w.weight), formik.values.body_weights?.map((w) => w.qty), + editingAverageIndex, // Include editing index in dependencies ]); // EVENT HANDLERS - Body Weights @@ -332,10 +344,18 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { }; const handleAverageWeightChangeWrapper = (idx: number) => (e: React.ChangeEvent) => { + // Set focus state to prevent auto-calculation override + setEditingAverageIndex(idx); + const value = parseFloat(e.target.value.replace(/[^\d,.-]/g, '').replace(/,/g, '')) || 0; handleAverageWeightChange(idx, value); }; + const handleAverageWeightBlur = () => { + // Clear focus state when user leaves the field to re-enable auto-calculation + setEditingAverageIndex(null); + }; + const removeBodyWeight = (idx: number) => { const updatedBodyWeights = formik.values.body_weights?.filter( (_, i) => i !== idx @@ -707,7 +727,10 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { name={`body_weights.${idx}.average_weight`} value={bw.average_weight || 0} onChange={handleAverageWeightChangeWrapper(idx)} - onBlur={formik.handleBlur} + onBlur={(e) => { + handleAverageWeightBlur(); + formik.handleBlur(e); + }} maskType='weight' weightUnit='gram' decimals={2}