Skip to content

Commit 91dcde7

Browse files
committed
fix: add support of multiple field group types
1 parent a35d59a commit 91dcde7

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

index.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
110110
const similar = suggestIfTypo(adminforth.config.resources.map((res) => res.resourceId), this.options.foreignResourceId);
111111
throw new Error(`ForeignInlineListPlugin: Resource with ID "${this.options.foreignResourceId}" not found. ${similar ? `Did you mean "${similar}"?` : ''}`);
112112
}
113+
113114
const newColumn = {
114115
name: `foreignInlineList_${this.foreignResource.resourceId}`,
115116
label: 'Foreign Inline List',
@@ -133,23 +134,39 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
133134
};
134135

135136
if (this.options.placeInGroup?.name) {
136-
const targetGroup = resourceConfig.options?.fieldGroups?.find(
137-
group => group.groupName === this.options.placeInGroup?.name
138-
);
137+
const fieldGroupTypes = [
138+
'fieldGroups',
139+
'createFieldGroups',
140+
'editFieldGroups',
141+
'showFieldGroups'
142+
] as const;
143+
144+
let columnAdded = false;
139145

140-
if (!targetGroup) {
141-
throw new Error(`ForeignInlineListPlugin: Group "${this.options.placeInGroup?.name}" not found`);
142-
}
143-
if (this.options.placeInGroup.position < 0 || this.options.placeInGroup.position > targetGroup.columns.length) {
144-
throw new Error(`ForeignInlineListPlugin: Invalid position ${this.options.placeInGroup?.position}. Must be between 0 and ${targetGroup.columns.length} for group "${this.options.placeInGroup?.name}"`);
145-
}
146+
for (const groupType of fieldGroupTypes) {
147+
const targetGroup = resourceConfig.options?.[groupType]?.find(
148+
group => group.groupName === this.options.placeInGroup?.name
149+
);
150+
151+
if (targetGroup) {
152+
if (this.options.placeInGroup.position < 0 || this.options.placeInGroup.position > targetGroup.columns.length) {
153+
throw new Error(`ForeignInlineListPlugin: Invalid position ${this.options.placeInGroup?.position}. Must be between 0 and ${targetGroup.columns.length} for group "${this.options.placeInGroup?.name}"`);
154+
}
146155

147-
const beforeColumnName = targetGroup.columns[this.options.placeInGroup.position - 1];
148-
const beforeColumnIndex = resourceConfig.columns.findIndex(
149-
col => col.name === beforeColumnName
150-
);
151-
targetGroup.columns.splice(this.options.placeInGroup.position, 0, newColumn.name);
152-
resourceConfig.columns.splice(beforeColumnIndex + 1, 0, newColumn);
156+
// Only add the column to resourceConfig.columns once
157+
if (!columnAdded) {
158+
const beforeColumnName = targetGroup.columns[this.options.placeInGroup.position - 1];
159+
const beforeColumnIndex = resourceConfig.columns.findIndex(
160+
col => col.name === beforeColumnName
161+
);
162+
resourceConfig.columns.splice(beforeColumnIndex + 1, 0, newColumn);
163+
columnAdded = true;
164+
}
165+
166+
// Add the column name to the group's columns array
167+
targetGroup.columns.splice(this.options.placeInGroup.position, 0, newColumn.name);
168+
}
169+
}
153170
} else {
154171
resourceConfig.columns.push(newColumn);
155172
}

0 commit comments

Comments
 (0)