Skip to content

Commit

Permalink
chore: use formFieldInstanceRegistry for submission
Browse files Browse the repository at this point in the history
Related to #1142
  • Loading branch information
Skaiir committed Apr 10, 2024
1 parent 247135f commit 0c465e9
Showing 1 changed file with 10 additions and 37 deletions.
47 changes: 10 additions & 37 deletions packages/form-js-viewer/src/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,51 +416,24 @@ export class Form {
*/
_getSubmitData() {
const formFieldRegistry = this.get('formFieldRegistry');
const formFields = this.get('formFields');
const pathRegistry = this.get('pathRegistry');
const formFieldInstanceRegistry = this.get('formFieldInstanceRegistry');
const formData = this._getState().data;

function collectSubmitDataRecursively(submitData, formField, indexes) {
const { disabled, type } = formField;
const { config: fieldConfig } = formFields.get(type);

// (1) Process keyed fields
if (!disabled && fieldConfig.keyed) {
const valuePath = pathRegistry.getValuePath(formField, { indexes });
const value = get(formData, valuePath);
set(submitData, valuePath, value);
}

// (2) Process parents
if (!Array.isArray(formField.components)) {
return;
}

// (3a) Recurse repeatable parents both across the indexes of repetition and the children
if (fieldConfig.repeatable && formField.isRepeating) {

const valueData = get(formData, pathRegistry.getValuePath(formField, { indexes }));

if (!Array.isArray(valueData)) {
return;
}
const submitData = {};

valueData.forEach((_, index) => {
formField.components.forEach((component) => {
collectSubmitDataRecursively(submitData, component, { ...indexes, [formField.id]: index });
});
});
formFieldInstanceRegistry.getAllKeyed().forEach((formFieldInstance) => {
const { id, valuePath } = formFieldInstance;
const { disabled } = formFieldRegistry.get(id);

if (disabled) {
return;
}

// (3b) Recurse non-repeatable parents only across the children
formField.components.forEach((component) => collectSubmitDataRecursively(submitData, component, indexes));
}
const value = get(formData, valuePath);
set(submitData, valuePath, value);
});

const workingSubmitData = {};
collectSubmitDataRecursively(workingSubmitData, formFieldRegistry.getForm(), {});
return this._applyConditions(workingSubmitData, formData);
return submitData;
}

/**
Expand Down

0 comments on commit 0c465e9

Please sign in to comment.