Skip to content

Commit

Permalink
Ensure we do NOT process empty rows for DataGrid and EditGrid, but we…
Browse files Browse the repository at this point in the history
… do process them for forms.
  • Loading branch information
travist committed Nov 18, 2024
1 parent 36f3a6f commit fcfb128
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 40 deletions.
8 changes: 0 additions & 8 deletions src/process/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ export async function process<ProcessScope>(
components,
data,
async (component, compData, row, path, components, index, parent, paths) => {
// Skip processing if row is null or undefined
if (!row) {
return;
}
await processOne<ProcessScope>({
...context,
data: compData,
Expand Down Expand Up @@ -76,10 +72,6 @@ export function processSync<ProcessScope>(context: ProcessContext<ProcessScope>)
components,
data,
(component, compData, row, path, components, index, parent, paths) => {
// Skip processing if row is null or undefined
if (!row) {
return;
}
processOneSync<ProcessScope>({
...context,
data: compData,
Expand Down
11 changes: 2 additions & 9 deletions src/process/processOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ProcessorsContext, ProcessorType } from 'types';
import { getModelType } from 'utils/formUtil';

export async function processOne<ProcessorScope>(context: ProcessorsContext<ProcessorScope>) {
const { processors, row, component } = context;
const { processors, component } = 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 @@ -26,9 +26,6 @@ export async function processOne<ProcessorScope>(context: ProcessorsContext<Proc
});
}

if (!row) {
return;
}
context.processor = ProcessorType.Custom;
for (const processor of processors) {
if (processor?.process) {
Expand All @@ -38,7 +35,7 @@ export async function processOne<ProcessorScope>(context: ProcessorsContext<Proc
}

export function processOneSync<ProcessorScope>(context: ProcessorsContext<ProcessorScope>) {
const { processors, row, component } = context;
const { processors, component } = 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 @@ -61,10 +58,6 @@ export function processOneSync<ProcessorScope>(context: ProcessorsContext<Proces
});
}

if (!row) {
return;
}

// Process the components.
context.processor = ProcessorType.Custom;
for (const processor of processors) {
Expand Down
15 changes: 10 additions & 5 deletions src/utils/formUtil/eachComponentData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,17 @@ export const eachComponentData = (
}
if (isComponentNestedDataType(component)) {
const value = get(data, compPaths?.dataPath || '') as DataObject;
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
if (compPaths) {
compPaths.dataIndex = i;
if (
getModelType(component) === 'nestedArray' ||
getModelType(component) === 'nestedDataArray'
) {
if (Array.isArray(value) && value.length) {
for (let i = 0; i < value.length; i++) {
if (compPaths) {
compPaths.dataIndex = i;
}
eachComponentData(component.components, data, fn, includeAll, component, compPaths);
}
eachComponentData(component.components, data, fn, includeAll, component, compPaths);
}
resetComponentScope(component);
return true;
Expand Down
29 changes: 17 additions & 12 deletions src/utils/formUtil/eachComponentDataAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,24 @@ export const eachComponentDataAsync = async (
}
if (isComponentNestedDataType(component)) {
const value = get(data, compPaths?.dataPath || '');
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
if (compPaths) {
compPaths.dataIndex = i;
if (
getModelType(component) === 'nestedArray' ||
getModelType(component) === 'nestedDataArray'
) {
if (Array.isArray(value) && value.length) {
for (let i = 0; i < value.length; i++) {
if (compPaths) {
compPaths.dataIndex = i;
}
await eachComponentDataAsync(
component.components,
data,
fn,
includeAll,
component,
compPaths,
);
}
await eachComponentDataAsync(
component.components,
data,
fn,
includeAll,
component,
compPaths,
);
}
resetComponentScope(component);
return true;
Expand Down
14 changes: 8 additions & 6 deletions src/utils/formUtil/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,15 +546,17 @@ export function getComponentLocalData(paths: ComponentPaths, data: any): string
return parentPath ? get(data, parentPath, null) : data;
}

export function shouldProcessComponent(component: Component, row: any, value: any): boolean {
export function shouldProcessComponent(comp: Component, row: any, value: any): boolean {
if (isEmpty(row)) {
return false;
}
if (getModelType(component) === 'dataObject') {
const noReferenceAttached = value && value._id && isEmpty(value.data) && !has(value, 'form');
const shouldProcessNestedFormData =
value && value._id ? !noReferenceAttached : value !== undefined;
if (!shouldProcessNestedFormData) {
if (getModelType(comp) === 'dataObject') {
const noReferenceAttached = value?._id ? isEmpty(value.data) && !has(value, 'form') : false;
const shouldBeCleared =
(!comp.hasOwnProperty('clearOnHide') || comp.clearOnHide) &&
(comp.hidden || comp.scope?.conditionallyHidden);
const shouldSkipProcessingNestedFormData = noReferenceAttached || shouldBeCleared;
if (shouldSkipProcessingNestedFormData) {
return false;
}
}
Expand Down

0 comments on commit fcfb128

Please sign in to comment.