New utility function to loop all group-fields

This commit is contained in:
Philipp Stracker 2024-09-30 18:57:00 +02:00
parent 8b0e12c06d
commit 89fa0f1fa7
No known key found for this signature in database

View file

@ -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;
} }