feat: explicitly exports fields for reuse #35 (#39)

This commit is contained in:
Jacob Fletcher
2023-05-15 11:56:34 -04:00
parent a89df757bf
commit f8da32a9bd
9 changed files with 1573 additions and 8864 deletions

View File

@@ -65,8 +65,6 @@ export default config;
} }
``` ```
You can also provide your own custom field definitions by passing a new [Payload Block](https://payloadcms.com/docs/fields/blocks#block-configs) object into `fields`.
- `redirectRelationships` - `redirectRelationships`
An array of collection slugs that, when enabled, are populated as options in form redirect fields. An array of collection slugs that, when enabled, are populated as options in form redirect fields.
@@ -115,6 +113,10 @@ export default config;
```ts ```ts
formOverrides: { formOverrides: {
slug: "contact-forms", slug: "contact-forms",
access: {
read: () => true,
update: () => false,
},
fields: [ fields: [
{ {
name: "custom-field", name: "custom-field",
@@ -132,66 +134,68 @@ export default config;
```js ```js
formSubmissionOverrides: { formSubmissionOverrides: {
slug: "leads"; slug: "leads",
} }
``` ```
## Fields ## Fields
Each field represents a form input. To override default settings pass either a boolean value or a partial [Payload Block](https://payloadcms.com/docs/fields/blocks) keyed to the block's slug. Each field represents a form input. To override default settings pass either a boolean value or a partial [Payload Block](https://payloadcms.com/docs/fields/blocks) _keyed to the block's slug_. See [Field Overrides](#field-overrides) for more details on how to do this.
- Text > NOTE: "fields" here are in reference to the _fields to build forms with_, not to be confused with the _fields of a collection_ which are set via `formOverrides.fields`.
- `text`
- `name`: string - `name`: string
- `label`: string - `label`: string
- `defaultValue`: string - `defaultValue`: string
- `width`: string - `width`: string
- `required`: checkbox - `required`: checkbox
- Textarea - `textarea`
- `name`: string - `name`: string
- `label`: string - `label`: string
- `defaultValue`: string - `defaultValue`: string
- `width`: string - `width`: string
- `required`: checkbox - `required`: checkbox
- Select - `select`
- `name`: string - `name`: string
- `label`: string - `label`: string
- `defaultValue`: string - `defaultValue`: string
- `width`: string - `width`: string
- `options`: array - `options`: array
- `required`: checkbox - `required`: checkbox
- Email - `email`
- `name`: string - `name`: string
- `label`: string - `label`: string
- `defaultValue`: string - `defaultValue`: string
- `width`: string - `width`: string
- `required`: checkbox - `required`: checkbox
- State - `state`
- `name`: string - `name`: string
- `label`: string - `label`: string
- `defaultValue`: string - `defaultValue`: string
- `width`: string - `width`: string
- `required`: checkbox - `required`: checkbox
- Country - `country`
- `name`: string - `name`: string
- `label`: string - `label`: string
- `defaultValue`: string - `defaultValue`: string
- `width`: string - `width`: string
- `required`: checkbox - `required`: checkbox
- Checkbox - `checkbox`
- `name`: string - `name`: string
- `label`: string - `label`: string
- `defaultValue`: checkbox - `defaultValue`: checkbox
- `width`: string - `width`: string
- `required`: checkbox - `required`: checkbox
- Number - `number`
- `name`: string - `name`: string
- `label`: string - `label`: string
- `defaultValue`: number - `defaultValue`: number
- `width`: string - `width`: string
- `required`: checkbox - `required`: checkbox
- Message - `message`
- `message`: richText - `message`: richText
- Payment - `payment`
- `name`: string - `name`: string
- `label`: string - `label`: string
- `defaultValue`: number - `defaultValue`: number
@@ -205,6 +209,28 @@ Each field represents a form input. To override default settings pass either a b
- `valueType`: string - `static`, `valueOfField` - `valueType`: string - `static`, `valueOfField`
- `value`: string - only if `valueType` is `static` - `value`: string - only if `valueType` is `static`
### Field Overrides
You can also provide your own custom field definitions by passing a new [Payload Block](https://payloadcms.com/docs/fields/blocks#block-configs) object into `fields`. You can override or extend any existing fields by first importing the `fields` from the plugin:
```ts
import { fields } from "@payloadcms/plugin-form-builder";
```
Then merging it into your own custom field:
```ts
fields: {
text: {
...fields.text,
labels: {
singular: "Custom Text Field",
plural: "Custom Text Fields",
}
}
}
```
## Email ## Email
This plugin relies on the [email configuration](https://payloadcms.com/docs/email/overview) defined in your `payload.init()`. It will read from your config and attempt to send your emails using the credentials provided. This plugin relies on the [email configuration](https://payloadcms.com/docs/email/overview) defined in your `payload.init()`. It will read from your config and attempt to send your emails using the credentials provided.

View File

@@ -16,7 +16,7 @@
"deepmerge": "^4.2.2", "deepmerge": "^4.2.2",
"dotenv": "^8.2.0", "dotenv": "^8.2.0",
"express": "^4.17.1", "express": "^4.17.1",
"payload": "^1.3.0" "payload": "^1.8.2"
}, },
"devDependencies": { "devDependencies": {
"@types/express": "^4.17.9", "@types/express": "^4.17.9",

View File

@@ -4,7 +4,7 @@ import type { Block } from 'payload/types'
// import formBuilderPlugin from '../../dist'; // import formBuilderPlugin from '../../dist';
// eslint-disable-next-line import/no-relative-packages // eslint-disable-next-line import/no-relative-packages
import formBuilderPlugin from '../../src' import formBuilderPlugin, { fields } from '../../src'
import { Pages } from './collections/Pages' import { Pages } from './collections/Pages'
import { Users } from './collections/Users' import { Users } from './collections/Users'
@@ -68,6 +68,13 @@ export default buildConfig({
fields: { fields: {
payment: true, payment: true,
colorField, colorField,
text: {
...fields.text,
labels: {
singular: 'Custom Text Field',
plural: 'Custom Text Fields',
},
},
// payment: { // payment: {
// paymentProcessor: { // paymentProcessor: {
// options: [ // options: [

View File

@@ -14,7 +14,7 @@ app.get('/', (_, res) => {
// Initialize Payload // Initialize Payload
const start = async (): Promise<any> => { const start = async (): Promise<any> => {
await payload.initAsync({ await payload.init({
secret: process.env.PAYLOAD_SECRET, secret: process.env.PAYLOAD_SECRET,
mongoURL: process.env.MONGODB_URI, mongoURL: process.env.MONGODB_URI,
express: app, express: app,

File diff suppressed because it is too large Load Diff

View File

@@ -572,7 +572,7 @@ const Message: Block = {
} }
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
export default { export const fields = {
select: Select, select: Select,
checkbox: Checkbox, checkbox: Checkbox,
text: Text, text: Text,
@@ -586,3 +586,5 @@ export default {
} as { } as {
[key: string]: Block | ((fieldConfig?: boolean | FieldConfig) => Block) [key: string]: Block | ((fieldConfig?: boolean | FieldConfig) => Block)
} }
export default fields

View File

@@ -2,7 +2,7 @@ import merge from 'deepmerge'
import type { Block, CollectionConfig, Field } from 'payload/types' import type { Block, CollectionConfig, Field } from 'payload/types'
import type { FieldConfig, PluginConfig } from '../../types' import type { FieldConfig, PluginConfig } from '../../types'
import fields from './fields' import { fields } from './fields'
// all settings can be overridden by the config // all settings can be overridden by the config
export const generateFormCollection = (formConfig: PluginConfig): CollectionConfig => { export const generateFormCollection = (formConfig: PluginConfig): CollectionConfig => {

View File

@@ -3,8 +3,8 @@ import type { Config } from 'payload/config'
import { generateFormCollection } from './collections/Forms' import { generateFormCollection } from './collections/Forms'
import { generateSubmissionCollection } from './collections/FormSubmissions' import { generateSubmissionCollection } from './collections/FormSubmissions'
import type { PluginConfig } from './types' import type { PluginConfig } from './types'
// import path from 'path';
export { fields } from './collections/Forms/fields'
export { getPaymentTotal } from './utilities/getPaymentTotal' export { getPaymentTotal } from './utilities/getPaymentTotal'
const FormBuilder = const FormBuilder =

File diff suppressed because it is too large Load Diff