Skip to content

Instantly share code, notes, and snippets.

@nielslange
Created February 24, 2023 10:46
Show Gist options
  • Select an option

  • Save nielslange/778826d999c02a104f1b9848fb27b26b to your computer and use it in GitHub Desktop.

Select an option

Save nielslange/778826d999c02a104f1b9848fb27b26b to your computer and use it in GitHub Desktop.
diff --git a/assets/js/base/components/cart-checkout/address-form/address-form.tsx b/assets/js/base/components/cart-checkout/address-form/address-form.tsx
index cec368a6c..966ffeb06 100644
--- a/assets/js/base/components/cart-checkout/address-form/address-form.tsx
+++ b/assets/js/base/components/cart-checkout/address-form/address-form.tsx
@@ -144,6 +144,29 @@ const AddressForm = ( {
id = id || instanceId;
+ const customValidationHandler = (
+ inputObject: HTMLInputElement,
+ fieldName: { key: string }
+ ) => {
+ if (
+ fieldName.key === 'postcode' &&
+ values.country &&
+ ! isPostcode( {
+ postcode: values.postcode,
+ country: values.country,
+ } )
+ ) {
+ inputObject.setCustomValidity(
+ __(
+ 'Please enter a valid postcode',
+ 'woo-gutenberg-products-block'
+ )
+ );
+ return false;
+ }
+ return true;
+ };
+
return (
<div id={ id } className="wc-block-components-address-form">
{ addressFormFields.map( ( field ) => {
@@ -214,32 +237,11 @@ const AddressForm = ( {
);
}
- const customValidationHandler = (
- inputObject: HTMLInputElement
- ) => {
- if (
- field.key === 'postcode' &&
- values.country &&
- ! isPostcode( {
- postcode: values.postcode,
- country: values.country,
- } )
- ) {
- inputObject.setCustomValidity(
- __(
- 'Please enter a valid postcode',
- 'woo-gutenberg-products-block'
- )
- );
- return false;
- }
- return true;
- };
-
return (
<ValidatedTextInput
key={ field.key }
id={ `${ id }-${ field.key }` }
+ fieldName={ field.key }
errorId={ errorId }
className={ `wc-block-components-address-form__${ field.key }` }
label={
diff --git a/packages/checkout/components/text-input/validated-text-input.tsx b/packages/checkout/components/text-input/validated-text-input.tsx
index fc8f836a7..bbfc51c3e 100644
--- a/packages/checkout/components/text-input/validated-text-input.tsx
+++ b/packages/checkout/components/text-input/validated-text-input.tsx
@@ -52,8 +52,12 @@ interface ValidatedTextInputProps
errorMessage?: string | undefined;
// Custom validation function that is run on change. Use setCustomValidity to set an error message.
customValidation?:
- | ( ( inputObject: HTMLInputElement ) => boolean )
+ | ( (
+ inputObject: HTMLInputElement,
+ fieldName: { key: string }
+ ) => boolean )
| undefined;
+ fieldName: string;
}
const ValidatedTextInput = ( {
@@ -69,6 +73,7 @@ const ValidatedTextInput = ( {
value = '',
customValidation,
label,
+ fieldName,
...rest
}: ValidatedTextInputProps ): JSX.Element => {
const [ isPristine, setIsPristine ] = useState( true );
@@ -106,7 +111,8 @@ const ValidatedTextInput = ( {
}
const inputIsValid = customValidation
- ? inputObject.checkValidity() && customValidation( inputObject )
+ ? inputObject.checkValidity() &&
+ customValidation( inputObject, fieldName )
: inputObject.checkValidity();
if ( inputIsValid ) {
@@ -130,6 +136,7 @@ const ValidatedTextInput = ( {
errorIdString,
setValidationErrors,
label,
+ fieldName,
]
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment