Skip to content

Commit

Permalink
Admin Generator (Future): Render custom content within forms (#3235)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesricky authored Jan 21, 2025
1 parent a35375d commit eb09bd3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions demo/admin/src/products/future/ProductForm.cometGen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export const ProductForm: FormConfig<GQLProduct> = {
},
{ type: "boolean", name: "inStock" },
{ type: "date", name: "availableSince", startAdornment: { icon: "CalendarToday" } },
{ type: "component", component: { name: "FutureProductNotice", import: "../../helpers/FutureProductNotice" } },
{ type: "block", name: "image", label: "Image", block: { name: "DamImageBlock", import: "@comet/cms-admin" } },
{ type: "fileUpload", name: "priceList", label: "Price List", maxFileSize: 1024 * 1024 * 4, download: true },
{ type: "fileUpload", name: "datasheets", label: "Datasheets", multiple: true, maxFileSize: 1024 * 1024 * 4, download: false },
Expand Down
2 changes: 2 additions & 0 deletions demo/admin/src/products/future/generated/ProductForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import React from "react";
import { FormSpy } from "react-final-form";
import { FormattedMessage } from "react-intl";

import { FutureProductNotice } from "../../helpers/FutureProductNotice";
import { validateTitle } from "../validateTitle";
import {
GQLManufacturersSelectQuery,
Expand Down Expand Up @@ -407,6 +408,7 @@ export function ProductForm({ id }: FormProps): React.ReactElement {
</InputAdornment>
}
/>
<FutureProductNotice />
<Field
name="image"
isEqual={isEqual}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ComponentFormFieldConfig } from "../generator";
import { Imports } from "../utils/generateImportsCode";
import { GenerateFieldsReturn } from "./generateFields";

export function generateComponentFormField({ config }: { config: ComponentFormFieldConfig }): GenerateFieldsReturn {
const imports: Imports = [{ name: config.component.name, importPath: config.component.import }];
const code = `<${config.component.name} />`;

return {
imports,
hooksCode: "",
formFragmentFields: [],
formValueToGqlInputCode: "",
formValuesConfig: [],
finalFormConfig: undefined,
code,
gqlDocuments: {},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IntrospectionQuery } from "graphql";

import { FormConfig, GeneratorReturn, isFormFieldConfig, isFormLayoutConfig } from "../generator";
import { Imports } from "../utils/generateImportsCode";
import { generateComponentFormField } from "./generateComponentFormField";
import { generateFormField } from "./generateFormField";
import { generateFormLayout } from "./generateFormLayout";

Expand All @@ -27,7 +28,13 @@ export type GenerateFieldsReturn = GeneratorReturn & {
export function findFieldByName(name: string, fields: FormConfig<any>["fields"]): FormConfig<any>["fields"][0] | undefined {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return fields.reduce<FormConfig<any>["fields"][0] | undefined>((acc, field) => {
return acc ? acc : field.name === name ? field : isFormLayoutConfig(field) ? findFieldByName(name, field.fields) : undefined;
return acc
? acc
: "name" in field && field.name === name
? field
: isFormLayoutConfig(field)
? findFieldByName(name, field.fields)
: undefined;
}, undefined);
}

Expand Down Expand Up @@ -61,7 +68,12 @@ export function generateFields({
const code = fields
.map((field) => {
let generated: GenerateFieldsReturn;
if (isFormFieldConfig(field)) {

if (field.type === "component") {
generated = generateComponentFormField({
config: field,
});
} else if (isFormFieldConfig(field)) {
generated = generateFormField({
gqlIntrospection,
baseOutputFilename,
Expand Down
6 changes: 4 additions & 2 deletions packages/admin/cms-admin/src/generator/future/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type InputBaseFieldConfig = {
endAdornment?: Adornment;
};

export type ComponentFormFieldConfig = { type: "component"; component: ImportReference };

export type FormFieldConfig<T> = (
| ({ type: "text"; multiline?: boolean } & InputBaseFieldConfig)
| ({ type: "number" } & InputBaseFieldConfig)
Expand Down Expand Up @@ -99,7 +101,7 @@ export type FormLayoutConfig<T> =
supportText?: string; // can contain field-placeholder
collapsible?: boolean; // default true
initiallyExpanded?: boolean; // default false
fields: (FormFieldConfig<T> | OptionalNestedFieldsConfig<T>)[];
fields: (FormFieldConfig<T> | OptionalNestedFieldsConfig<T> | ComponentFormFieldConfig)[];
}
| OptionalNestedFieldsConfig<T>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -113,7 +115,7 @@ export type FormConfig<T extends { __typename?: string }> = {
mode?: "edit" | "add" | "all";
fragmentName?: string;
createMutation?: string;
fields: (FormFieldConfig<T> | FormLayoutConfig<T>)[];
fields: (FormFieldConfig<T> | FormLayoutConfig<T> | ComponentFormFieldConfig)[];
};

export type TabsConfig = { type: "tabs"; tabs: { name: string; content: GeneratorConfig }[] };
Expand Down

0 comments on commit eb09bd3

Please sign in to comment.