Improve readability and add a condition to invalidity for Input

This commit is contained in:
Connor Bryan 2018-06-22 17:21:55 -05:00
parent 9004cee42c
commit 2e0dd57d8b
2 changed files with 5 additions and 4 deletions

View File

@ -50,7 +50,7 @@ class Input extends React.Component<Props, State> {
}
// Show an error with no value only after blurring.
if (!hasValue && showInvalidWithoutValue && !validDueToBlur) {
if (!isStateless && !validDueToBlur && !hasValue && showInvalidWithoutValue) {
validClass = 'invalid';
}

View File

@ -130,20 +130,21 @@ export const validNumber = (num: number) => isFinite(num) && num >= 0;
export const validPositiveNumber = (num: number) => validNumber(num) && num !== 0;
export const validDecimal = (input: string, decimal: number) => {
const decimals: RegExpMatchArray | null = input.match(/\./g);
const arr = input.split('.');
// Only a single decimal can exist.
if (decimals && decimals.length > 1) {
if (arr.length > 2) {
return false;
}
const arr = input.split('.');
const fractionPortion = arr[1];
if (!fractionPortion || fractionPortion.length === 0) {
return true;
}
const decimalLength = fractionPortion.length;
return decimalLength <= decimal;
};