Skip to content

Commit

Permalink
move path mutation into processOne and out of eachComponent; update f…
Browse files Browse the repository at this point in the history
…n signatures
  • Loading branch information
brendanbond committed Nov 4, 2024
1 parent 87688e6 commit 50c880c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 47 deletions.
25 changes: 22 additions & 3 deletions src/process/processOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function dataValue(component: Component, row: any) {
}

export async function processOne<ProcessorScope>(context: ProcessorsContext<ProcessorScope>) {
const { processors, component } = context;
const { processors, component, path } = context;
// Create a getter for `value` that is always derived from the current data object
if (typeof context.value === 'undefined') {
Object.defineProperty(context, 'value', {
Expand All @@ -22,8 +22,18 @@ export async function processOne<ProcessorScope>(context: ProcessorsContext<Proc
},
});
}
// If the component has ephemeral state, then we need to reset the ephemeral state in case this is e.g. a data grid, in which each row needs to be validated independently

// Define the component path
Object.defineProperty(component, 'path', {
enumerable: false,
writable: true,
value: path,
});

// If the component has ephemeral state, then we need to reset it in case this is e.g. a data grid,
// in which each row needs to be validated independently
resetEphemeralState(component);

if (!context.row) {
return;
}
Expand All @@ -36,7 +46,7 @@ export async function processOne<ProcessorScope>(context: ProcessorsContext<Proc
}

export function processOneSync<ProcessorScope>(context: ProcessorsContext<ProcessorScope>) {
const { processors, component } = context;
const { processors, component, path } = context;
// Create a getter for `value` that is always derived from the current data object
if (typeof context.value === 'undefined') {
Object.defineProperty(context, 'value', {
Expand All @@ -49,8 +59,17 @@ export function processOneSync<ProcessorScope>(context: ProcessorsContext<Proces
},
});
}

// Define the component path
Object.defineProperty(component, 'path', {
enumerable: false,
writable: true,
value: path,
});

// If the component has ephemeral state, then we need to reset the ephemeral state in case this is e.g. a data grid, in which each row needs to be validated independently
resetEphemeralState(component);

if (!context.row) {
return;
}
Expand Down
32 changes: 3 additions & 29 deletions src/utils/formUtil/eachComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ import { componentInfo, componentPath, componentFormPath } from './index';
* The current data path of the element. Example: data.user.firstName
* @param {Object} parent
* The parent object.
* @param {Boolean} noComponentChange
* Whether or not to add properties (e.g. path/parent) to the component object
*/
export function eachComponent(
components: Component[],
fn: EachComponentCallback,
includeAll?: boolean,
path: string = '',
parent?: Component,
noComponentChange?: boolean,
) {
if (!components) return;
components.forEach((component: any) => {
Expand All @@ -33,7 +30,7 @@ export function eachComponent(
const info = componentInfo(component);
let noRecurse = false;
// Keep track of parent references.
if (parent && !noComponentChange) {
if (parent) {
// Ensure we don't create infinite JSON structures.
Object.defineProperty(component, 'parent', {
enumerable: false,
Expand All @@ -58,42 +55,20 @@ export function eachComponent(

const compPath = componentPath(component, path);

if (!noComponentChange) {
Object.defineProperty(component, 'path', {
enumerable: false,
writable: true,
value: compPath,
});
}

if (includeAll || component.tree || !info.layout) {
noRecurse = !!fn(component, compPath, components, parent);
}

if (!noRecurse) {
if (info.hasColumns) {
component.columns.forEach((column: any) =>
eachComponent(
column.components,
fn,
includeAll,
path,
parent ? component : null,
noComponentChange,
),
eachComponent(column.components, fn, includeAll, path, parent ? component : null),
);
} else if (info.hasRows) {
component.rows.forEach((row: any) => {
if (Array.isArray(row)) {
row.forEach((column) =>
eachComponent(
column.components,
fn,
includeAll,
path,
parent ? component : null,
noComponentChange,
),
eachComponent(column.components, fn, includeAll, path, parent ? component : null),
);
}
});
Expand All @@ -104,7 +79,6 @@ export function eachComponent(
includeAll,
componentFormPath(component, path, compPath),
parent ? component : null,
noComponentChange,
);
}
}
Expand Down
13 changes: 1 addition & 12 deletions src/utils/formUtil/eachComponentAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export async function eachComponentAsync(
includeAll = false,
path = '',
parent?: any,
noComponentChange?: boolean,
) {
if (!components) return;
for (let i = 0; i < components.length; i++) {
Expand All @@ -17,7 +16,7 @@ export async function eachComponentAsync(
const component = components[i];
const info = componentInfo(component);
// Keep track of parent references.
if (parent && !noComponentChange) {
if (parent) {
// Ensure we don't create infinite JSON structures.
Object.defineProperty(component, 'parent', {
enumerable: false,
Expand All @@ -41,13 +40,6 @@ export async function eachComponentAsync(
}
const compPath = componentPath(component, path);

if (!noComponentChange) {
Object.defineProperty(component, 'path', {
enumerable: false,
writable: true,
value: compPath,
});
}
if (includeAll || component.tree || !info.layout) {
if (await fn(component, compPath, components, parent)) {
continue;
Expand All @@ -61,7 +53,6 @@ export async function eachComponentAsync(
includeAll,
path,
parent ? component : null,
noComponentChange,
);
}
} else if (info.hasRows) {
Expand All @@ -75,7 +66,6 @@ export async function eachComponentAsync(
includeAll,
path,
parent ? component : null,
noComponentChange,
);
}
}
Expand All @@ -87,7 +77,6 @@ export async function eachComponentAsync(
includeAll,
componentFormPath(component, path, compPath),
parent ? component : null,
noComponentChange,
);
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/utils/formUtil/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,6 @@ export function getComponent(
}
},
includeAll,
undefined,
undefined,
true,
);
return result;
}
Expand Down

0 comments on commit 50c880c

Please sign in to comment.