sodistorehome configurtaion minsoc error message disappeared after a valid input

This commit is contained in:
Yinyin Liu 2025-12-15 14:31:48 +01:00
parent 5102678d35
commit 9dda68e084
1 changed files with 38 additions and 62 deletions

View File

@ -152,67 +152,33 @@ function SodistoreHomeConfiguration(props: SodistoreHomeConfigurationProps) {
const handleChange = (e) => { const handleChange = (e) => {
const { name, value } = e.target; const { name, value } = e.target;
switch (name) { if (name === 'minimumSoC') {
// case 'minimumSoC': const numValue = parseFloat(value);
// if (
// /[^0-9.]/.test(value) ||
// isNaN(parseFloat(value)) ||
// parseFloat(value) > 30 ||
// parseFloat(value) < 10
// ) {
// SetErrorForField(name, true);
// } else {
// SetErrorForField(name, false);
// }
// break;
case 'minimumSoC': {
const numValue = parseFloat(value);
// Check for invalid characters or non-numeric // invalid characters or not a number
if (/[^0-9.]/.test(value) || isNaN(numValue)) { if (/[^0-9.]/.test(value) || isNaN(numValue)) {
SetErrorForField(name, { SetErrorForField(name, 'Invalid number format');
hasError: true, } else {
message: 'Invalid number format',
});
break;
}
// Define device-based ranges
const minsocRanges = { const minsocRanges = {
3: { min: 10, max: 30 }, 3: { min: 10, max: 30 },
4: { min: 5, max: 100 }, 4: { min: 5, max: 100 },
}; };
// Fallback range if device not listed
const { min, max } = minsocRanges[device] || { min: 10, max: 30 }; const { min, max } = minsocRanges[device] || { min: 10, max: 30 };
if (numValue < min || numValue > max) { if (numValue < min || numValue > max) {
SetErrorForField(name, { SetErrorForField(name, `Value should be between ${min}-${max}%`);
hasError: true,
message: `Value should be between ${min}-${max}%`,
});
} else { } else {
SetErrorForField(name, { hasError: false, message: '' }); // ✅ valid → clear error
SetErrorForField(name, '');
} }
break;
} }
case 'gridSetPoint':
if (/[^0-9.]/.test(value) || isNaN(parseFloat(value))) {
SetErrorForField(name, true);
} else {
SetErrorForField(name, false);
}
break;
default:
break;
} }
setFormValues({ setFormValues(prev => ({
...formValues, ...prev,
[name]: value [name]: value,
}); }));
}; };
const handleTimeChargeDischargeChange = (name: string, value: any) => { const handleTimeChargeDischargeChange = (name: string, value: any) => {
@ -367,27 +333,37 @@ function SodistoreHomeConfiguration(props: SodistoreHomeConfigurationProps) {
<div style={{ marginBottom: '5px' }}> <div style={{ marginBottom: '5px' }}>
{/*<TextField*/}
{/* label={*/}
{/* <FormattedMessage*/}
{/* id="minimum_soc "*/}
{/* defaultMessage="Minimum SoC (%)"*/}
{/* />*/}
{/* }*/}
{/* name="minimumSoC"*/}
{/* value={formValues.minimumSoC}*/}
{/* onChange={handleChange}*/}
{/* helperText={*/}
{/* errors.minimumSoC ? (*/}
{/* <span style={{ color: 'red' }}>*/}
{/* Value should be between {device === 4 ? '5100' : '1030'}%*/}
{/* </span>*/}
{/* ) : (*/}
{/* ''*/}
{/* )*/}
{/* }*/}
{/* fullWidth*/}
{/*/>*/}
<TextField <TextField
label={ label="Minimum SoC (%)"
<FormattedMessage
id="minimum_soc "
defaultMessage="Minimum SoC (%)"
/>
}
name="minimumSoC" name="minimumSoC"
value={formValues.minimumSoC} value={formValues.minimumSoC}
onChange={handleChange} onChange={handleChange}
helperText={ error={Boolean(errors.minimumSoC)}
errors.minimumSoC ? ( helperText={errors.minimumSoC}
<span style={{ color: 'red' }}>
Value should be between {device === 4 ? '5100' : '1030'}%
</span>
) : (
''
)
}
fullWidth fullWidth
/> />
</div> </div>
<div style={{ marginBottom: '5px' }}> <div style={{ marginBottom: '5px' }}>