Skip to content

Commit 6021288

Browse files
authored
Merge pull request #5 from devforth/place-in-group
feat: add support for placing components in specific groups within re…
2 parents 7d809c6 + 5ccff19 commit 6021288

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

index.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import type {
32
AdminForthResource,
43
IAdminForth,
@@ -111,7 +110,7 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
111110
const similar = suggestIfTypo(adminforth.config.resources.map((res) => res.resourceId), this.options.foreignResourceId);
112111
throw new Error(`ForeignInlineListPlugin: Resource with ID "${this.options.foreignResourceId}" not found. ${similar ? `Did you mean "${similar}"?` : ''}`);
113112
}
114-
resourceConfig.columns.push({
113+
const newColumn = {
115114
name: `foreignInlineList_${this.foreignResource.resourceId}`,
116115
label: 'Foreign Inline List',
117116
virtual: true,
@@ -131,6 +130,28 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
131130
}
132131
}
133132
},
134-
});
133+
};
134+
135+
if (this.options.placeInGroup?.name) {
136+
const targetGroup = resourceConfig.options?.fieldGroups?.find(
137+
group => group.groupName === this.options.placeInGroup?.name
138+
);
139+
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+
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);
153+
} else {
154+
resourceConfig.columns.push(newColumn);
155+
}
135156
}
136157
}

types.ts

+10
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,14 @@ export type PluginOptions = {
1919
* @param resourceConfig - Resource config of the table.
2020
*/
2121
modifyTableResourceConfig?: (resourceConfig: AdminForthResource) => void;
22+
23+
/**
24+
* Place the component in a specific group.
25+
*
26+
* @param group - Group to place the component in.
27+
*/
28+
placeInGroup?: {
29+
name: string;
30+
position: number;
31+
};
2232
}

0 commit comments

Comments
 (0)