Skip to content

Commit

Permalink
React hook form - Support editing with scientific notation
Browse files Browse the repository at this point in the history
  • Loading branch information
thangqp committed Nov 21, 2023
1 parent 4352bcf commit 67c266c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/components/react-hook-form/numbers/float-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ const FloatInput = (props) => {
if (['-', '.'].includes(sanitizedValue)) {
return sanitizedValue;
}

// support editing exponential format
if (
sanitizedValue &&
(sanitizedValue.includes('e') || sanitizedValue.includes('E'))
) {
return sanitizedValue;
}

return sanitizedValue === null || isNaN(sanitizedValue)
? ''
: sanitizedValue;
Expand All @@ -32,6 +41,12 @@ const FloatInput = (props) => {
if (tmp.endsWith('.') || tmp.endsWith('0')) {
return tmp;
}

// support editing exponential format
if (tmp.includes('e') || tmp.includes('E')) {
return tmp;
}

return parseFloat(tmp) || null;
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/react-hook-form/numbers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export const isIntegerNumber = (val) => {
};

export const isFloatNumber = (val) => {
return /^-?[0-9]*[.,]?[0-9]*$/.test(val);
return /^-?[0-9]*[.,]?[0-9]*([eE][-+]?[0-9]*)?$/.test(val);
};

0 comments on commit 67c266c

Please sign in to comment.