web: Update nullable number handling

This commit is contained in:
2025-06-27 11:37:50 -05:00
parent fff70cce67
commit 53489c1638
11 changed files with 84 additions and 44 deletions

View File

@@ -43,6 +43,9 @@ export const generateUniqueName = (container: any, prefix: string): string => {
export const parseNumber = (
valueStr: string,
): [number, ValidationError | null] => {
if (valueStr === "") {
return [0, { message: "Field must not be blank" }];
}
const valueFloat = parseFloat(valueStr);
if (isNaN(valueFloat)) {
return [0, { message: `"${valueStr}" is not a valid number` }];
@@ -51,6 +54,13 @@ export const parseNumber = (
}
};
export const parseNullableNumber = (
valueStr: string,
): [number | null, ValidationError | null] => {
if (valueStr === "") return [null, null];
return parseNumber(valueStr);
};
export const parseBool = (
valueStr: string,
): [boolean, ValidationError | null] => {
@@ -93,9 +103,12 @@ export const changeNumberData = (
field: string,
newValueStr: string,
container: { [key: string]: any },
nullable: boolean = false,
): [{ [key: string]: any }, ValidationError | null] => {
// Parse value
const [newValueFloat, err] = parseNumber(newValueStr);
const [newValueFloat, err] = nullable
? parseNullableNumber(newValueStr)
: parseNumber(newValueStr);
if (err) return [container, err];
// Build the new object
@@ -211,6 +224,8 @@ export const changeData = (
return changeBusRefData(fieldName, newValueStr, container, scenario);
case "number":
return changeNumberData(fieldName, newValueStr, container);
case "number?":
return changeNumberData(fieldName, newValueStr, container, true);
case "number[T]":
return changeNumberVecTData(
fieldName,