Skip to content

Instantly share code, notes, and snippets.

@tyteen4a03
Created October 30, 2023 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyteen4a03/d41c085729339108462f166a060f7fff to your computer and use it in GitHub Desktop.
Save tyteen4a03/d41c085729339108462f166a060f7fff to your computer and use it in GitHub Desktop.
Payload Form Example
export default buildConfig({
admin: {
components: {
views: {
PayloadFormExample: {
Component: PayloadFormExample,
path: "/collections/my-collection/my-form",
},
},
},
},
...
});
import { Gutter } from "payload/components/elements";
import { Form, FormSubmit, RenderFields, fieldTypes } from "payload/components/forms";
import { DefaultTemplate } from "payload/components/templates";
import { AdminViewComponent } from "payload/config";
import { useStepNav } from "payload/dist/admin/components/elements/StepNav";
import { Field } from "payload/types";
import React, { useEffect } from "react";
import { useTranslation } from "react-i18next";
const PayloadFormExample: AdminViewComponent = () => {
const { setStepNav } = useStepNav();
const { t } = useTranslation();
const fields: Field[] = [
{
name: "test",
label: "Test",
type: "text",
},
];
// This effect will only run one time and will allow us
// to set the step nav to display our custom route name
useEffect(() => {
setStepNav([
{
label: "My Collection",
url: "/admin/collections/my-collection",
},
{
label: "My Form",
},
]);
}, [setStepNav]);
return (
<DefaultTemplate>
<Gutter>
<h1>My Form</h1>
<Form method="post" action="/api/my-collection/my-action" fields={fields}>
<RenderFields fieldSchema={fields} fieldTypes={fieldTypes} readOnly={false} />
<FormSubmit>{t("general:submit")}</FormSubmit>
</Form>
</Gutter>
</DefaultTemplate>
);
};
export default PayloadFormExample;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment