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 );
|
||||
}
|
||||
|
||||
Object.keys( this.fields ).forEach( ( key ) => {
|
||||
const field = this.fields[ key ];
|
||||
|
||||
if ( this.active && ! field.showInput ) {
|
||||
this.hideField( field.selector );
|
||||
this.loopFields( ( { selector } ) => {
|
||||
if ( this.#active /* && ! field.showInput */ ) {
|
||||
this.hideField( selector );
|
||||
} else {
|
||||
this.showField( field.selector );
|
||||
this.showField( selector );
|
||||
}
|
||||
} );
|
||||
|
||||
|
@ -107,18 +105,39 @@ class FormFieldGroup {
|
|||
},
|
||||
isEmpty: () => {
|
||||
let isEmpty = true;
|
||||
Object.keys( this.fields ).forEach( ( fieldKey ) => {
|
||||
|
||||
this.loopFields( ( field, fieldKey ) => {
|
||||
if ( this.dataValue( fieldKey ) ) {
|
||||
isEmpty = false;
|
||||
return false;
|
||||
}
|
||||
} );
|
||||
|
||||
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 ) {
|
||||
const field = document.querySelector(
|
||||
this.#baseSelector + ' ' + selector
|
||||
|
@ -159,9 +178,7 @@ class FormFieldGroup {
|
|||
}
|
||||
|
||||
toSubmitData( data ) {
|
||||
Object.keys( this.fields ).forEach( ( fieldKey ) => {
|
||||
const field = this.fields[ fieldKey ];
|
||||
|
||||
this.loopFields( ( field, fieldKey ) => {
|
||||
if ( ! field.valuePath || ! field.selector ) {
|
||||
return true;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue