corvée(dépendances) ajoute Carbon Fields

This commit is contained in:
gcch 2024-08-09 18:45:01 +02:00
commit 62368587e5
459 changed files with 72750 additions and 26 deletions

View file

@ -0,0 +1,46 @@
/**
* External dependencies.
*/
import { pick, cloneDeep } from 'lodash';
/**
* Carbon Fields dependencies.
*/
import { uniqueId } from '@carbon-fields/core';
/**
* Flattens a field.
*
* @param {Object} field
* @param {string} containerId
* @param {Object[]} accumulator
* @return {Object}
*/
export default function flattenField( field, containerId, accumulator ) {
field = cloneDeep( field );
// Replace the id of the field.
field.id = uniqueId();
// Keep reference to the container.
field.container_id = containerId;
// The complex fields represent a nested structure of fields.
// So we need to flat them as well.
if ( field.type === 'complex' ) {
field.value.forEach( ( group ) => {
group.id = uniqueId();
group.container_id = containerId;
group.fields = group.fields.map( ( groupField ) => flattenField( groupField, containerId, accumulator ) );
} );
}
accumulator.push( field );
return pick( field, [
'id',
'type',
'name',
'base_name'
] );
}