Provide recently updated data for the validate function in the useValidation hook (#43320)
* Allow providing recently updated data for the validate function in the useValidation hook * set newData parameter as optional
This commit is contained in:
parent
ebb6b0fe54
commit
245fea22ac
|
@ -0,0 +1,4 @@
|
||||||
|
Significance: minor
|
||||||
|
Type: add
|
||||||
|
|
||||||
|
Allow providing recently updated data for the validate function in the useValidation hook
|
|
@ -167,7 +167,7 @@ export function Edit( {
|
||||||
dateOnSaleFromGmtValidationError && 'has-error'
|
dateOnSaleFromGmtValidationError && 'has-error'
|
||||||
}
|
}
|
||||||
help={ dateOnSaleFromGmtValidationError as string }
|
help={ dateOnSaleFromGmtValidationError as string }
|
||||||
onBlur={ validateDateOnSaleFromGmt }
|
onBlur={ () => validateDateOnSaleFromGmt() }
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -190,7 +190,7 @@ export function Edit( {
|
||||||
.toISOString()
|
.toISOString()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
onBlur={ validateDateOnSaleToGmt }
|
onBlur={ () => validateDateOnSaleToGmt() }
|
||||||
className={
|
className={
|
||||||
dateOnSaleToGmtValidationError && 'has-error'
|
dateOnSaleToGmtValidationError && 'has-error'
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ export type ValidatorResponse = Promise< ValidationError >;
|
||||||
|
|
||||||
export type Validator< T > = (
|
export type Validator< T > = (
|
||||||
initialValue?: T,
|
initialValue?: T,
|
||||||
newData?: Partial< T >
|
newData?: Record< string, unknown >
|
||||||
) => ValidatorResponse;
|
) => ValidatorResponse;
|
||||||
|
|
||||||
export type ValidationContextProps< T > = {
|
export type ValidationContextProps< T > = {
|
||||||
|
@ -12,7 +12,10 @@ export type ValidationContextProps< T > = {
|
||||||
validator: Validator< T >
|
validator: Validator< T >
|
||||||
): React.Ref< HTMLElement >;
|
): React.Ref< HTMLElement >;
|
||||||
unRegisterValidator( validatorId: string ): void;
|
unRegisterValidator( validatorId: string ): void;
|
||||||
validateField( name: string ): ValidatorResponse;
|
validateField(
|
||||||
|
name: string,
|
||||||
|
newData?: Record< string, unknown >
|
||||||
|
): ValidatorResponse;
|
||||||
validateAll( newData?: Partial< T > ): Promise< ValidationErrors >;
|
validateAll( newData?: Partial< T > ): Promise< ValidationErrors >;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -33,11 +33,13 @@ export function useValidation< T >(
|
||||||
ref,
|
ref,
|
||||||
error: context.errors[ validatorId ],
|
error: context.errors[ validatorId ],
|
||||||
isValidating,
|
isValidating,
|
||||||
async validate() {
|
async validate( newData?: Record< string, unknown > ) {
|
||||||
setIsValidating( true );
|
setIsValidating( true );
|
||||||
return context.validateField( validatorId ).finally( () => {
|
return context
|
||||||
setIsValidating( false );
|
.validateField( validatorId, newData )
|
||||||
} );
|
.finally( () => {
|
||||||
|
setIsValidating( false );
|
||||||
|
} );
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue