mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
✨ New utility function to loop all group-fields
This commit is contained in:
parent
8b0e12c06d
commit
89fa0f1fa7
1 changed files with 27 additions and 10 deletions
|
@ -90,13 +90,11 @@ class FormFieldGroup {
|
||||||
this.showField( this.#contentSelector );
|
this.showField( this.#contentSelector );
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.keys( this.fields ).forEach( ( key ) => {
|
this.loopFields( ( { selector } ) => {
|
||||||
const field = this.fields[ key ];
|
if ( this.#active /* && ! field.showInput */ ) {
|
||||||
|
this.hideField( selector );
|
||||||
if ( this.active && ! field.showInput ) {
|
|
||||||
this.hideField( field.selector );
|
|
||||||
} else {
|
} else {
|
||||||
this.showField( field.selector );
|
this.showField( selector );
|
||||||
}
|
}
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
@ -107,18 +105,39 @@ class FormFieldGroup {
|
||||||
},
|
},
|
||||||
isEmpty: () => {
|
isEmpty: () => {
|
||||||
let isEmpty = true;
|
let isEmpty = true;
|
||||||
Object.keys( this.fields ).forEach( ( fieldKey ) => {
|
|
||||||
|
this.loopFields( ( field, fieldKey ) => {
|
||||||
if ( this.dataValue( fieldKey ) ) {
|
if ( this.dataValue( fieldKey ) ) {
|
||||||
isEmpty = false;
|
isEmpty = false;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} );
|
} );
|
||||||
|
|
||||||
return isEmpty;
|
return isEmpty;
|
||||||
},
|
},
|
||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoke a callback on every field in the current group.
|
||||||
|
*
|
||||||
|
* @param {(field: object, key: string) => void} callback
|
||||||
|
*/
|
||||||
|
loopFields( callback ) {
|
||||||
|
Object.keys( this.#fields ).forEach( ( key ) => {
|
||||||
|
const field = this.#fields[ key ];
|
||||||
|
const fieldSelector = `${ this.#baseSelector } ${ field.selector }`;
|
||||||
|
|
||||||
|
callback(
|
||||||
|
{
|
||||||
|
...field,
|
||||||
|
},
|
||||||
|
key
|
||||||
|
);
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
showField( selector ) {
|
showField( selector ) {
|
||||||
const field = document.querySelector(
|
const field = document.querySelector(
|
||||||
this.#baseSelector + ' ' + selector
|
this.#baseSelector + ' ' + selector
|
||||||
|
@ -159,9 +178,7 @@ class FormFieldGroup {
|
||||||
}
|
}
|
||||||
|
|
||||||
toSubmitData( data ) {
|
toSubmitData( data ) {
|
||||||
Object.keys( this.fields ).forEach( ( fieldKey ) => {
|
this.loopFields( ( field, fieldKey ) => {
|
||||||
const field = this.fields[ fieldKey ];
|
|
||||||
|
|
||||||
if ( ! field.valuePath || ! field.selector ) {
|
if ( ! field.valuePath || ! field.selector ) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue