Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add row in action forms #1173

Merged
merged 13 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 130 additions & 1 deletion packages/_example/src/forest/customizations/card.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,133 @@
import { CardCustomizer } from '../typings';

export default (collection: CardCustomizer) =>
collection.addManyToOneRelation('customer', 'customer', { foreignKey: 'customer_id' });
collection
.addManyToOneRelation('customer', 'customer', { foreignKey: 'customer_id' })
.addAction('create new card', {
scope: 'Global',
execute: (context, resultBuilder) => {
return resultBuilder.success('ok', {
html: `test<script type="text/javascript">alert("test")</script>`,
});
},
form: [
{
type: 'Number',
label: 'Customer',
widget: 'Dropdown',
search: 'dynamic',
options: async (ctx, searchValue) => {
const results = await ctx.dataSource.getCollection('customer').list(
{
...(searchValue
? {
conditionTree: {
aggregator: 'Or',
conditions: [
{ field: 'name', operator: 'Contains', value: searchValue },
{ field: 'firstName', operator: 'Contains', value: searchValue },
],
},
}
: {}),
},
['id'],
);

return results.map(({ id }) => id);
},
},
{
type: 'String',
label: 'Plan',
widget: 'Dropdown',
options: ['Base', 'Gold', 'Black'],
},
{
type: 'Layout',
component: 'HtmlBlock',
content: ctx => {
switch (ctx.formValues.Plan) {
case 'Base':
return `<h1>Should setup:</h1>
<ul>
<li>separator</li>
<li>price</li>
<li>max withdraw / max payment</li>
</ul>`;
case 'Gold':
return `<h1>Should setup:</h1>
<ul>
<li>max payment / Systematic check (if max payment > 1000)</li>
<li>discount / discount months</li>
</ul>`;
case 'Back':
return `<h1>Should setup:</h1>
<ul>
<li>max withdraw</li>
</ul>`;
default:
return `Select a card plan`;
}
},
},
{
type: 'Layout',
component: 'Separator',
if: ctx => ['Base'].includes(ctx.formValues.Plan),
},
{
type: 'Number',
label: 'price',
defaultValue: 40,
if: ctx => ['Base'].includes(ctx.formValues.Plan),
},
{
type: 'Number',
label: 'price',
defaultValue: 80,
if: ctx => ['Gold'].includes(ctx.formValues.Plan),
},
{ type: 'Layout', component: 'Separator' },
{ type: 'Layout', component: 'HtmlBlock', content: '<h3>constraints:</h3>' },
{
type: 'Layout',
component: 'Row',
fields: [
{
type: 'Number',
label: 'Max withdraw',
if: ctx => ['Base', 'Black'].includes(ctx.formValues.Plan),
},
{
type: 'Number',
label: 'Max payment',
if: ctx => ['Base', 'Gold'].includes(ctx.formValues.Plan),
},
{
type: 'Boolean',
label: 'Systematic check',
if: ctx =>
['Base', 'Gold'].includes(ctx.formValues.Plan) &&
ctx.formValues['Max payment'] > 1000,
},
],
},
{
type: 'Layout',
component: 'Row',
fields: [
{
type: 'Number',
label: 'Discount',
if: ctx => ['Gold'].includes(ctx.formValues['display fields']),
},
{
type: 'Number',
label: 'Discount months',
if: ctx => ['Gold'].includes(ctx.formValues['display fields']),
},
],
},
],
});
61 changes: 51 additions & 10 deletions packages/agent/src/utils/forest-schema/generator-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import {
Collection,
ColumnSchema,
DataSource,
LayoutElementInput,
PrimitiveTypes,
SchemaUtils,
} from '@forestadmin/datasource-toolkit';
import {
ForestServerAction,
ForestServerActionField,
ForestServerActionFormElementFieldReference,
ForestServerActionFormLayoutElement,
} from '@forestadmin/forestadmin-client';
import path from 'path';
Expand Down Expand Up @@ -126,7 +128,14 @@ export default class SchemaGeneratorActions {
return output as ForestServerActionField;
}

static buildLayoutSchema(element: ActionLayoutElement): ForestServerActionFormLayoutElement {
static buildLayoutSchema(
element: ActionLayoutElement,
options?: { forceInput?: boolean },
): ForestServerActionFormLayoutElement {
if (options?.forceInput) {
element.component = 'Input';
}

switch (element.component) {
case 'Input':
return {
Expand All @@ -138,6 +147,16 @@ export default class SchemaGeneratorActions {
component: 'htmlBlock',
content: element.content,
};
case 'Row':
return {
component: 'row',
fields: element.fields.map(
field =>
SchemaGeneratorActions.buildLayoutSchema(field, {
forceInput: true,
}) as ForestServerActionFormElementFieldReference,
),
};
case 'Separator':
default:
return {
Expand All @@ -159,16 +178,9 @@ export default class SchemaGeneratorActions {
formElements.forEach(element => {
if (element.type === 'Layout') {
hasLayout = true;

layout.push(element);
} else {
fields.push(element);
layout.push({
type: 'Layout',
component: 'Input',
fieldId: element.label,
});
}

layout.push(SchemaGeneratorActions.parseLayout(element, fields));
});

if (!hasLayout) {
Expand All @@ -177,4 +189,33 @@ export default class SchemaGeneratorActions {

return { fields, layout };
}

private static parseLayout(
element: ActionFormElement,
allFields: ActionField[],
): ActionLayoutElement {
if (element.type === 'Layout') {
if (element.component === 'Row') {
const fields = element.fields.map(
field => SchemaGeneratorActions.parseLayout(field, allFields) as LayoutElementInput,
);

return {
type: 'Layout',
component: 'Row',
fields,
};
}

return element;
}

allFields.push(element);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😢 Mutating function parameters. 😬


return {
type: 'Layout',
component: 'Input',
fieldId: element.label,
};
}
}
41 changes: 26 additions & 15 deletions packages/agent/test/utils/forest-schema/generator-actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,6 @@ describe('SchemaGeneratorActions', () => {
value: null,
watchChanges: false,
},
{
type: 'Layout',
component: 'Separator',
},
{
type: 'Layout',
component: 'HtmlBlock',
content: 'some text content',
},
Comment on lines -136 to -144
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀

{
label: 'inclusive gender',
description: 'Choose None, Male, Female or Both',
Expand Down Expand Up @@ -313,9 +304,20 @@ describe('SchemaGeneratorActions', () => {
content: 'some text content',
},
{
label: 'description',
type: 'String',
watchChanges: false,
type: 'Layout',
component: 'Row',
fields: [
{
label: 'description',
type: 'String',
watchChanges: false,
},
{
label: 'address',
type: 'String',
watchChanges: false,
},
],
},
],
),
Expand All @@ -327,7 +329,7 @@ describe('SchemaGeneratorActions', () => {

const schema = SchemaGeneratorActions.buildFieldsAndLayout(collection.dataSource, form);

expect(schema.fields.length).toEqual(2);
expect(schema.fields.length).toEqual(3);
expect(schema.layout).toEqual([
{
component: 'input',
Expand All @@ -336,8 +338,17 @@ describe('SchemaGeneratorActions', () => {
{ component: 'separator' },
{ component: 'htmlBlock', content: 'some text content' },
{
component: 'input',
fieldId: 'description',
component: 'row',
fields: [
{
component: 'input',
fieldId: 'description',
},
{
component: 'input',
fieldId: 'address',
},
],
},
]);
});
Expand Down
Loading
Loading