Created
October 30, 2023 15:47
-
-
Save tyteen4a03/d41c085729339108462f166a060f7fff to your computer and use it in GitHub Desktop.
Payload Form Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default buildConfig({ | |
admin: { | |
components: { | |
views: { | |
PayloadFormExample: { | |
Component: PayloadFormExample, | |
path: "/collections/my-collection/my-form", | |
}, | |
}, | |
}, | |
}, | |
... | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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