Skip to content

Commit c39b28d

Browse files
committed
add validtor prop callback to validate input before updating
1 parent 3650677 commit c39b28d

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/InputNumber.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ export interface InputNumberProps<T extends ValueType = ValueType>
9292
value: T | undefined,
9393
info: { userTyping: boolean; input: string; prevValue: string },
9494
) => string;
95+
/** Validate an input string before processing */
96+
validator?: (input: string) => boolean;
9597
/** Syntactic sugar of `formatter`. Config precision of display. */
9698
precision?: number;
9799
/** Syntactic sugar of `formatter`. Config decimal separator of display. */
@@ -137,6 +139,7 @@ const InternalInputNumber = React.forwardRef(
137139
classNames,
138140
stringMode,
139141

142+
validator,
140143
parser,
141144
formatter,
142145
precision,
@@ -393,6 +396,12 @@ const InternalInputNumber = React.forwardRef(
393396

394397
// >>> Collect input value
395398
const collectInputValue = (inputStr: string) => {
399+
400+
// validate string
401+
if(validator){
402+
if(!validator(inputStr)) return;
403+
}
404+
396405
recordCursor();
397406

398407
// Update inputValue in case input can not parse as number
@@ -492,8 +501,8 @@ const InternalInputNumber = React.forwardRef(
492501
if (value !== undefined) {
493502
// Reset back with controlled value first
494503
setInputValue(decimalValue, false);
495-
} else {
496-
// Format value
504+
} else if (!formatValue.isNaN()) {
505+
// Reset input back since no validate value
497506
setInputValue(formatValue, false);
498507
}
499508
};

0 commit comments

Comments
 (0)