@@ -63,6 +63,15 @@ export interface BigNumberFieldProps {
6363 formatOptions ?: FormatOptions ;
6464 /** Additional props for the input element. */
6565 inputProps ?: React . InputHTMLAttributes < HTMLInputElement > ;
66+ /** A function that returns an error message if a given value is invalid.
67+ * Return a string to denote invalid.*/
68+ validate ?: ( value : BigNumber | null ) => true | null | undefined | string ;
69+ /** Flag to enable field errors, alternative to `message`
70+ * This will show the validation errors from browser, or custom error in case `validate` is setup on Field.
71+ */
72+ showFieldError ?: boolean ;
73+ /** ClassName for field error message. */
74+ fieldErrorClassName ?: string ;
6675}
6776
6877// Default format configuration
@@ -124,6 +133,12 @@ export function useBigNumberField(props: BigNumberFieldProps) {
124133 // State to track if the input is currently formatted
125134 const [ isFormatted , setIsFormatted ] = useState < boolean > ( false ) ;
126135
136+ // State to track input's validation
137+ const [ validationResult , setValidationResult ] = useState < {
138+ isInvalid : boolean ;
139+ validationError ?: string ;
140+ } > ( { isInvalid : false } ) ;
141+
127142 // State for the numeric value
128143 const [ numberValue , setNumberValue ] = useState < BigNumber | null > ( ( ) => {
129144 try {
@@ -231,6 +246,7 @@ export function useBigNumberField(props: BigNumberFieldProps) {
231246 setInputValue ( newValue . toString ( ) ) ;
232247 setIsFormatted ( false ) ;
233248 onChange ?.( newValue ) ;
249+ setValidationResult ( getValidationResult ( newValue ) ) ;
234250 } ;
235251
236252 const decrement = ( ) => {
@@ -252,6 +268,7 @@ export function useBigNumberField(props: BigNumberFieldProps) {
252268 setInputValue ( newValue . toString ( ) ) ;
253269 setIsFormatted ( false ) ;
254270 onChange ?.( newValue ) ;
271+ setValidationResult ( getValidationResult ( newValue ) ) ;
255272 } ;
256273
257274 // Helper function to escape special characters in regex
@@ -415,6 +432,8 @@ export function useBigNumberField(props: BigNumberFieldProps) {
415432 } else if ( inputValue !== "" && inputValue !== "-" ) {
416433 setInputValue ( "" ) ;
417434 }
435+
436+ setValidationResult ( getValidationResult ( ) ) ;
418437 } ;
419438
420439 // Handle keyboard events
@@ -552,6 +571,7 @@ export function useBigNumberField(props: BigNumberFieldProps) {
552571 readOnly : isReadOnly ,
553572 required : props . isRequired ,
554573 placeholder : props . placeholder ,
574+ "aria-invalid" : validationResult ?. isInvalid ,
555575 ...getAriaAttributes ( ) ,
556576 ...props . inputProps ,
557577 } ) ;
@@ -561,6 +581,32 @@ export function useBigNumberField(props: BigNumberFieldProps) {
561581 htmlFor : id ,
562582 } ) ;
563583
584+ // Field Error Render props
585+ const getValidationResult = ( value ?: BigNumber ) => {
586+ const fieldErrorProps = {
587+ isInvalid : false ,
588+ validationError : "" ,
589+ } ;
590+
591+ if (
592+ props . isRequired &&
593+ ( value ? value . toString ( ) : inputValue ) . trim ( ) === ""
594+ ) {
595+ fieldErrorProps . isInvalid = true ;
596+ fieldErrorProps . validationError = "Please fill out this field." ;
597+ }
598+
599+ const validate = props . validate ;
600+ if ( validate ) {
601+ const result = validate ( value ?? numberValue ) ;
602+ if ( typeof result === `string` ) {
603+ fieldErrorProps . isInvalid = true ;
604+ fieldErrorProps . validationError = result ;
605+ }
606+ }
607+ return fieldErrorProps ;
608+ } ;
609+
564610 // Increment button props
565611 const getIncrementButtonProps = ( ) => ( {
566612 type : "button" as const ,
@@ -603,6 +649,7 @@ export function useBigNumberField(props: BigNumberFieldProps) {
603649 groupProps : getGroupProps ( ) ,
604650 descriptionProps : getDescriptionProps ( ) ,
605651 errorMessageProps : getErrorMessageProps ( ) ,
652+ validationResult,
606653 inputValue,
607654 numberValue,
608655 canIncrement : canIncrement ( ) ,
0 commit comments