Skip to content

Commit 7ca0dea

Browse files
committed
feat: implement search functionality for resource selection in custom component creation
1 parent 47fec1d commit 7ca0dea

File tree

2 files changed

+100
-17
lines changed

2 files changed

+100
-17
lines changed

adminforth/commands/createCustomComponent/configUpdater.js

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,27 @@ async function findResourceFilePath(resourceId) {
4747

4848
if (n.TSAsExpression.check(declaration) && n.ObjectExpression.check(declaration.expression)) {
4949
objectExpressionNode = declaration.expression;
50-
}
51-
else if (n.ObjectExpression.check(declaration)) {
50+
} else if (n.ObjectExpression.check(declaration)) {
5251
objectExpressionNode = declaration;
52+
} else if (n.Identifier.check(declaration)) {
53+
const varName = declaration.name;
54+
55+
recast.visit(ast, {
56+
visitVariableDeclaration(path) {
57+
for (const decl of path.node.declarations) {
58+
if (
59+
n.VariableDeclarator.check(decl) &&
60+
n.Identifier.check(decl.id) &&
61+
decl.id.name === varName &&
62+
n.ObjectExpression.check(decl.init)
63+
) {
64+
objectExpressionNode = decl.init;
65+
return false;
66+
}
67+
}
68+
this.traverse(path);
69+
}
70+
});
5371
}
5472

5573
if (objectExpressionNode) {
@@ -118,6 +136,25 @@ export async function updateResourceConfig(resourceId, columnName, fieldType, co
118136
objectExpressionNode = declaration.expression;
119137
} else if (n.ObjectExpression.check(declaration)) {
120138
objectExpressionNode = declaration;
139+
} else if (n.Identifier.check(declaration)) {
140+
const varName = declaration.name;
141+
142+
recast.visit(ast, {
143+
visitVariableDeclaration(path) {
144+
for (const decl of path.node.declarations) {
145+
if (
146+
n.VariableDeclarator.check(decl) &&
147+
n.Identifier.check(decl.id) &&
148+
decl.id.name === varName &&
149+
n.ObjectExpression.check(decl.init)
150+
) {
151+
objectExpressionNode = decl.init;
152+
return false;
153+
}
154+
}
155+
this.traverse(path);
156+
}
157+
});
121158
}
122159

123160
if (!objectExpressionNode) {
@@ -446,9 +483,28 @@ export async function updateCrudInjectionConfig(resourceId, crudType, injectionP
446483
let objectExpressionNode = null;
447484

448485
if (n.TSAsExpression.check(declaration) && n.ObjectExpression.check(declaration.expression)) {
449-
objectExpressionNode = declaration.expression;
486+
objectExpressionNode = declaration.expression;
450487
} else if (n.ObjectExpression.check(declaration)) {
451-
objectExpressionNode = declaration;
488+
objectExpressionNode = declaration;
489+
} else if (n.Identifier.check(declaration)) {
490+
const varName = declaration.name;
491+
492+
recast.visit(ast, {
493+
visitVariableDeclaration(path) {
494+
for (const decl of path.node.declarations) {
495+
if (
496+
n.VariableDeclarator.check(decl) &&
497+
n.Identifier.check(decl.id) &&
498+
decl.id.name === varName &&
499+
n.ObjectExpression.check(decl.init)
500+
) {
501+
objectExpressionNode = decl.init;
502+
return false;
503+
}
504+
}
505+
this.traverse(path);
506+
}
507+
});
452508
}
453509

454510
if (!objectExpressionNode) {

adminforth/commands/createCustomComponent/main.js

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,26 @@ async function handleFieldComponentCreation(config, resources) {
5858

5959
console.log(chalk.grey(`Selected ❯ 🔤 Custom fields ❯ ${fieldType}`));
6060

61-
const resourceId = await select({
62-
message: 'Select resource for which you want to change component:',
63-
choices: [
64-
...resources.map(r => ({ name: `${r.label} ${chalk.grey(`${r.resourceId}`)}`, value: r.resourceId })),
65-
new Separator(),
66-
{ name: '🔙 BACK', value: '__BACK__' },
67-
]
61+
const resourceId = await search({
62+
message: 'Select resource for which you want to change component:',
63+
source: async (input) => {
64+
const searchTerm = input ? input.toLowerCase() : '';
65+
66+
const filtered = resources.filter(r => {
67+
const label = r.label || '';
68+
const id = r.resourceId || '';
69+
return label.toLowerCase().includes(searchTerm) || id.toLowerCase().includes(searchTerm);
70+
});
71+
72+
return [
73+
...filtered.map(r => ({
74+
name: `${r.label} ${chalk.grey(`${r.resourceId}`)}`,
75+
value: r.resourceId,
76+
})),
77+
new Separator(),
78+
{ name: '🔙 BACK', value: '__BACK__' },
79+
];
80+
}
6881
});
6982
if (resourceId === '__BACK__') return handleFieldComponentCreation(config, resources); // Pass config back
7083

@@ -144,14 +157,28 @@ async function handleCrudPageInjectionCreation(config, resources) {
144157

145158
console.log(chalk.grey(`Selected ❯ 📄 CRUD Page Injection ❯ ${crudType}`));
146159

147-
const resourceId = await select({
160+
const resourceId = await search({
148161
message: 'Select resource for which you want to inject the component:',
149-
choices: [
150-
...resources.map(r => ({ name: `${r.label} ${chalk.grey(`${r.resourceId}`)}`, value: r.resourceId })),
151-
new Separator(),
152-
{ name: '🔙 BACK', value: '__BACK__' },
153-
],
162+
source: async (input) => {
163+
const searchTerm = input ? input.toLowerCase() : '';
164+
165+
const filtered = resources.filter(r => {
166+
const label = r.label || '';
167+
const id = r.resourceId || '';
168+
return label.toLowerCase().includes(searchTerm) || id.toLowerCase().includes(searchTerm);
169+
});
170+
171+
return [
172+
...filtered.map(r => ({
173+
name: `${r.label} ${chalk.grey(`${r.resourceId}`)}`,
174+
value: r.resourceId,
175+
})),
176+
new Separator(),
177+
{ name: '🔙 BACK', value: '__BACK__' },
178+
];
179+
}
154180
});
181+
155182
if (resourceId === '__BACK__') return handleCrudPageInjectionCreation(config, resources);
156183

157184
const selectedResource = resources.find(r => r.resourceId === resourceId);

0 commit comments

Comments
 (0)