diff --git a/src/components/DeviationList.js b/src/components/DeviationList.js index 6fb3409..83ef0ca 100644 --- a/src/components/DeviationList.js +++ b/src/components/DeviationList.js @@ -15,7 +15,7 @@ const DeviationList = ({

{t.deviationsFromPlan}

{deviations.map((dev, index) => ( -
+
onDeviationChange(index, 'isAdditional', e.target.checked)} className="h-4 w-4 rounded border-gray-300 text-sky-600 focus:ring-sky-500" /> -
diff --git a/src/components/NumericInput.js b/src/components/NumericInput.js index 9184f3c..2cc98dd 100644 --- a/src/components/NumericInput.js +++ b/src/components/NumericInput.js @@ -1,13 +1,42 @@ import React from 'react'; -const NumericInput = ({ value, onChange, increment, min = -Infinity, max = Infinity, placeholder, unit }) => { +const NumericInput = ({ + value, + onChange, + increment, + min = -Infinity, + max = Infinity, + placeholder, + unit, + align = 'right' // 'left', 'center', 'right' +}) => { + // Determine decimal places based on increment + const getDecimalPlaces = () => { + const inc = String(increment || '1'); + const decimalIndex = inc.indexOf('.'); + if (decimalIndex === -1) return 0; + return inc.length - decimalIndex - 1; + }; + + const decimalPlaces = getDecimalPlaces(); + + // Format value for display + const formatValue = (val) => { + const num = Number(val); + if (isNaN(num)) return val; + return num.toFixed(decimalPlaces); + }; + const updateValue = (direction) => { const numIncrement = parseFloat(increment) || 1; - let numValue = parseFloat(value) || 0; + let numValue = Number(value); + if (isNaN(numValue)) { + numValue = min !== -Infinity ? min : 0; + } numValue += direction * numIncrement; numValue = Math.max(min, numValue); numValue = Math.min(max, numValue); - const finalValue = String(Math.round(numValue * 100) / 100); + const finalValue = formatValue(numValue); onChange(finalValue); }; @@ -20,16 +49,38 @@ const NumericInput = ({ value, onChange, increment, min = -Infinity, max = Infin const handleChange = (e) => { const val = e.target.value; + // Only allow valid numbers or empty string if (val === '' || /^-?\d*\.?\d*$/.test(val)) { + // Prevent object values + if (typeof val === 'object') return; onChange(val); } }; + const handleBlur = (e) => { + const val = e.target.value; + if (val !== '' && !isNaN(Number(val))) { + // Format the value when user finishes editing + onChange(formatValue(val)); + } + }; + + // Get alignment class + const getAlignmentClass = () => { + switch (align) { + case 'left': return 'text-left'; + case 'center': return 'text-center'; + case 'right': return 'text-right'; + default: return 'text-right'; + } + }; + return (
@@ -38,12 +89,15 @@ const NumericInput = ({ value, onChange, increment, min = -Infinity, max = Infin value={value} onChange={handleChange} onKeyDown={handleKeyDown} + onBlur={handleBlur} placeholder={placeholder} - className="p-2 border-t border-b w-full text-sm text-center" + style={{ width: '4em', minWidth: '4em', maxWidth: '8em' }} + className={`p-2 border-t border-b text-sm ${getAlignmentClass()}`} /> diff --git a/src/components/Settings.js b/src/components/Settings.js index 71169bb..b89adcc 100644 --- a/src/components/Settings.js +++ b/src/components/Settings.js @@ -55,7 +55,7 @@ const Settings = ({
-
+
- - + -
-
+
- - + -