Skip to content
Snippets Groups Projects
Commit 6421508a authored by Dima Rifai's avatar Dima Rifai
Browse files

Issue #395 - Update Validation for Inputs that should be numeric to resolve...

Issue #395 - Update Validation for Inputs that should be numeric to resolve browser comptibility issues
parent 10042991
No related branches found
No related tags found
1 merge request!506Issue #395 - Update Validation for Inputs that should be numeric to resolve...
Pipeline #112310 failed
...@@ -36,11 +36,34 @@ const SearchRanges: React.FC<SearchRangesProps> = ({ ...@@ -36,11 +36,34 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
setLastTimeUnitValue, setLastTimeUnitValue,
} = rangesProps; } = rangesProps;
const setNumericValue = (value: number, fieldSetter: Function) => { // Validate and update numeric values
if (!Number.isNaN(value) && value > 0) { const handleNumericInput = (
fieldSetter(value); value: string,
} else { fieldSetter: Function,
allowDecimal: boolean = false
) => {
// If decimals are allowed, preserve the decimal point in the input value
let cleanedValue = allowDecimal
? value.replace(/[^0-9.]/g, "") // Allow numbers and decimal point
: value.replace(/[^0-9]/g, ""); // Only allow numbers
// If the decimal is allowed, ensure there is only one decimal point
if (allowDecimal && cleanedValue.split(".").length > 2) {
// Remove extra decimals, keeping only the first one
cleanedValue = cleanedValue.slice(0, cleanedValue.indexOf(".") + 1) +
cleanedValue.split(".").slice(1).join(""); // Remove all decimals after the first one
}
// Limit the value to a maximum of 15 digits
if (cleanedValue.length > 15) {
cleanedValue = cleanedValue.slice(0, 15);
}
// If the cleaned value is empty, set it to undefined
if (cleanedValue === "") {
fieldSetter(undefined); fieldSetter(undefined);
} else {
fieldSetter(cleanedValue); // Invalid input, set to undefined
} }
}; };
...@@ -81,35 +104,35 @@ const SearchRanges: React.FC<SearchRangesProps> = ({ ...@@ -81,35 +104,35 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
))} ))}
</SelectContent> </SelectContent>
</Select> </Select>
{rangeSelectKey === "lastBlocks" && ( {rangeSelectKey === "lastBlocks" && (
<div className="flex items-center"> <div className="flex items-center">
<div className="flex flex-col w-full"> <div className="flex flex-col w-full">
<Input <Input
className="w-1/2 border-0 border-b-2 bg-theme" className="w-1/2 border-0 border-b-2 bg-theme"
type="number" type="text" // Use type="text" to allow custom validation
value={lastBlocksValue || ""} value={lastBlocksValue || ""}
onChange={(e) => onChange={(e) =>
setNumericValue(Number(e.target.value), setLastBlocksValue) handleNumericInput(e.target.value,setLastBlocksValue)
} }
placeholder={"Last"} placeholder={"Last"}
min="0"
/> />
</div> </div>
</div> </div>
)} )}
{rangeSelectKey === "lastTime" && ( {rangeSelectKey === "lastTime" && (
<> <>
<div className="flex items-center justify-center"> <div className="flex items-center justify-center">
<div className="flex flex-col w-full mr-2"> <div className="flex flex-col w-full mr-2">
<Input <Input
type="number" type="text"
className="bg-theme border-0 border-b-2 text-text" className="bg-theme border-0 border-b-2 text-text"
value={lastTimeUnitValue || ""} value={lastTimeUnitValue || ""}
onChange={(e) => onChange={(e) =>
setNumericValue(Number(e.target.value), setLastTimeUnitValue) handleNumericInput(e.target.value,setLastTimeUnitValue,true)
} }
placeholder={"Last"} placeholder={"Last"}
min="0"
/> />
</div> </div>
<Select onValueChange={setTimeUnitSelectKey}> <Select onValueChange={setTimeUnitSelectKey}>
...@@ -139,36 +162,36 @@ const SearchRanges: React.FC<SearchRangesProps> = ({ ...@@ -139,36 +162,36 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
</div> </div>
</> </>
)} )}
{rangeSelectKey === "blockRange" && ( {rangeSelectKey === "blockRange" && (
<div className="flex items-center"> <div className="flex items-center">
<div className="flex flex-col w-full mr-2"> <div className="flex flex-col w-full mr-2">
<Input <Input
type="number" type="text"
className="bg-theme border-0 border-b-2" className="bg-theme border-0 border-b-2"
data-testid="from-block-input" data-testid="from-block-input"
value={fromBlock || ""} value={fromBlock || ""}
onChange={(e) => onChange={(e) =>
setNumericValue(Number(e.target.value), setFromBlock) handleNumericInput(e.target.value,setFromBlock)
} }
placeholder="From" placeholder="From"
min="0"
/> />
</div> </div>
<div className="flex flex-col w-full"> <div className="flex flex-col w-full">
<Input <Input
className="bg-theme border-0 border-b-2" className="bg-theme border-0 border-b-2"
data-testid="headblock-number" data-testid="headblock-number"
type="number" type="text"
value={toBlock || ""} value={toBlock || ""}
onChange={(e) => onChange={(e) =>
setNumericValue(Number(e.target.value), setToBlock) handleNumericInput(e.target.value,setToBlock)
} }
placeholder={"To"} placeholder={"To"}
min={0}
/> />
</div> </div>
</div> </div>
)} )}
{rangeSelectKey === "timeRange" && ( {rangeSelectKey === "timeRange" && (
<div className="flex flex-col mt-5"> <div className="flex flex-col mt-5">
<div className="flex flex-col w-full mb-4"> <div className="flex flex-col w-full mb-4">
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment