Skip to content

Commit 3a9a7e1

Browse files
committed
Add ChildFields component and integrate with Fields for better grouping handling
1 parent d92b03c commit 3a9a7e1

2 files changed

Lines changed: 109 additions & 32 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { FC } from 'react';
2+
import { ConditionalWrapper } from '@components/ConditionalWrapper';
3+
import { Fields } from '@components/Preview/Fields';
4+
import { checkShouldGroupWrap, getValueGroupWrapper } from './Fields';
5+
6+
type ChildFieldsProps = {
7+
schema: Schema;
8+
entryChildren?: string[];
9+
level: number;
10+
paths: Array<string>;
11+
altSchema?: Schema;
12+
altUserValues?: UserValues;
13+
altSelectedEntries?: Array<string>;
14+
altDisplayNames?: Record<string, string>;
15+
hideEntities?: boolean;
16+
forceRenderAllTopLevelEntities?: boolean;
17+
isGroupable?: boolean;
18+
isGroup?: boolean;
19+
};
20+
21+
export const ChildFields: FC<ChildFieldsProps> = ({
22+
schema,
23+
entryChildren,
24+
paths,
25+
level,
26+
altSchema,
27+
altUserValues,
28+
altSelectedEntries,
29+
altDisplayNames,
30+
hideEntities,
31+
forceRenderAllTopLevelEntities,
32+
isGroupable,
33+
isGroup,
34+
}) => {
35+
return (
36+
<>
37+
{entryChildren?.map(uuid => {
38+
const schemaEntry = schema.get(uuid);
39+
40+
return (
41+
<ConditionalWrapper
42+
key={uuid}
43+
condition={(!isGroupable && checkShouldGroupWrap(level, schemaEntry)) || isGroup}
44+
wrapper={getValueGroupWrapper({ schemaEntry })}
45+
>
46+
<Fields
47+
key={uuid}
48+
uuid={uuid}
49+
base={schema}
50+
paths={paths}
51+
level={level + 1}
52+
altSchema={altSchema}
53+
altUserValues={altUserValues}
54+
altSelectedEntries={altSelectedEntries}
55+
altDisplayNames={altDisplayNames}
56+
hideEntities={hideEntities}
57+
forceRenderAllTopLevelEntities={forceRenderAllTopLevelEntities}
58+
/>
59+
</ConditionalWrapper>
60+
);
61+
})}
62+
</>
63+
);
64+
};

src/components/Preview/Fields.tsx

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ import { ConditionalWrapper } from '@components/ConditionalWrapper';
88
import { useInputsState, useProfileState, useUIState } from '@src/store';
99
import { Labels } from './Labels';
1010
import { Values } from './Values';
11+
import { ChildFields } from './ChildFields';
1112

12-
const checkShouldGroupWrap = (level: number, entry = {} as SchemaEntry) => {
13+
export const checkShouldGroupWrap = (level: number, entry = {} as SchemaEntry) => {
1314
const { children, type } = entry;
1415

15-
return (!children?.length || type === AdvancedFieldType.dropdown) && level !== GROUP_BY_LEVEL;
16+
return (!children?.length || type === AdvancedFieldType.dropdown || type === AdvancedFieldType.group) &&
17+
type === AdvancedFieldType.group
18+
? level === GROUP_BY_LEVEL
19+
: level !== GROUP_BY_LEVEL;
1620
};
1721

18-
const getPreviewWrapper =
22+
export const getPreviewWrapper =
1923
({
2024
isBlock,
2125
wrapEntities,
@@ -38,7 +42,7 @@ const getPreviewWrapper =
3842
</div>
3943
);
4044

41-
const getValueGroupWrapper =
45+
export const getValueGroupWrapper =
4246
({ schemaEntry }: { schemaEntry?: SchemaEntry }) =>
4347
({ children }: { children: ReactNode }) =>
4448
children ? (
@@ -85,12 +89,15 @@ export const Fields = ({
8589
const hasEmptyChildren = checkEmptyChildren(base, entry);
8690

8791
const isDependentDropdownOption =
88-
entry?.type === AdvancedFieldType.dropdownOption && !!schema.get(getParentEntryUuid(entry?.path))?.linkedEntry?.controlledBy;
92+
entry?.type === AdvancedFieldType.dropdownOption &&
93+
!!schema.get(getParentEntryUuid(entry?.path))?.linkedEntry?.controlledBy;
8994

9095
const controlledEntry = schema.get(getParentEntryUuid(entry?.path || []))?.linkedEntry?.controlledBy;
9196
const controlledEntryValue = controlledEntry ? userValues[controlledEntry] : undefined;
9297
const visibleDropdownOption =
93-
isDependentDropdownOption && controlledEntryValue && selectedEntries.includes(uuid) ? <div>{entry?.displayName}</div> : null;
98+
isDependentDropdownOption && controlledEntryValue && selectedEntries.includes(uuid) ? (
99+
<div>{entry?.displayName}</div>
100+
) : null;
94101

95102
if (visibleDropdownOption) return visibleDropdownOption;
96103

@@ -104,6 +111,8 @@ export const Fields = ({
104111
// don't render top level entities not selected for preview
105112
if (isEntity && !currentlyPreviewedEntityBfid.has(bfid) && !forceRenderAllTopLevelEntities) return null;
106113

114+
const isGroupParentEntryType = base.get(entry.path[entry.path.length - 2])?.type === AdvancedFieldType.group;
115+
107116
const {
108117
isGroupable,
109118
shouldRenderLabelOrPlaceholders,
@@ -128,9 +137,31 @@ export const Fields = ({
128137
forceRenderAllTopLevelEntities,
129138
});
130139

140+
const renderChildren = (children: string[] | undefined, isGroup = false) => {
141+
return (
142+
<ChildFields
143+
schema={base}
144+
entryChildren={children}
145+
paths={paths}
146+
level={level}
147+
altSchema={altSchema}
148+
altUserValues={altUserValues}
149+
altSelectedEntries={altSelectedEntries}
150+
altDisplayNames={altDisplayNames}
151+
hideEntities={hideEntities}
152+
forceRenderAllTopLevelEntities={forceRenderAllTopLevelEntities}
153+
isGroupable={isGroupable}
154+
isGroup={isGroup}
155+
/>
156+
);
157+
};
158+
131159
return (
132160
<ConditionalWrapper
133-
condition={isBlock || isBlockContents || wrapEntities}
161+
condition={
162+
((isBlock || isBlockContents || wrapEntities) && !isGroupParentEntryType) ||
163+
entry.type === AdvancedFieldType.group
164+
}
134165
wrapper={getPreviewWrapper({ isBlock, isBlockContents, wrapEntities })}
135166
>
136167
{shouldRenderLabelOrPlaceholders && (
@@ -152,31 +183,13 @@ export const Fields = ({
152183
htmlId={htmlId}
153184
/>
154185
)}
155-
{children?.map(uuid => {
156-
const schemaEntry = schema.get(uuid);
157-
158-
return (
159-
<ConditionalWrapper
160-
key={uuid}
161-
condition={!isGroupable && checkShouldGroupWrap(level, schemaEntry)}
162-
wrapper={getValueGroupWrapper({ schemaEntry })}
163-
>
164-
<Fields
165-
key={uuid}
166-
uuid={uuid}
167-
base={base}
168-
paths={paths}
169-
level={level + 1}
170-
altSchema={altSchema}
171-
altUserValues={altUserValues}
172-
altSelectedEntries={altSelectedEntries}
173-
altDisplayNames={altDisplayNames}
174-
hideEntities={hideEntities}
175-
forceRenderAllTopLevelEntities={forceRenderAllTopLevelEntities}
176-
/>
177-
</ConditionalWrapper>
178-
);
179-
})}
186+
{entry.type === AdvancedFieldType.group ? (
187+
<div className="preview-block-contents" data-testid="preview-fields">
188+
{renderChildren(children, true)}
189+
</div>
190+
) : (
191+
renderChildren(children)
192+
)}
180193
</ConditionalWrapper>
181194
);
182195
};

0 commit comments

Comments
 (0)