代码字段

Code Field 会在数据库中保存字符串,但为 Admin Panel 提供了一个代码编辑器风格的界面。

展示 Payload 管理面板中的 Code 字段
此字段使用了 `monaco-react` 编辑器的语法高亮功能。

要添加 Code Field,请在 Field Config 中将 type 设置为 code

import type { Field } from 'payload'

export const MyBlocksField: Field = {
  // ...
  type: 'code', // highlight-line
}

配置选项

选项描述
name *作为属性名存储在数据库中并从数据库检索时使用。了解更多
label在 Admin Panel 中用作字段标签的文本,或为每种语言提供键的对象。
unique强制要求 Collection 中每个条目该字段的值必须唯一。
index为该字段构建索引以加快查询速度。如果用户会频繁查询该字段的数据,请将此字段设为 true
minLength用于默认验证函数,确保值的最小字符长度。
maxLength用于默认验证函数,确保值的最大字符长度。
validate提供自定义验证函数,该函数将在 Admin Panel 和后端执行。了解更多
saveToJWT如果该字段是顶级字段且嵌套在支持身份验证的配置中,则将其数据包含在用户 JWT 中。
hooks提供字段钩子来控制该字段的逻辑。更多详情
access提供字段访问控制,指定用户可以查看和操作该字段数据的权限。更多详情
hidden完全限制该字段在所有 API 中的可见性。仍会保存到数据库,但不会出现在任何 API 或 Admin Panel 中。
defaultValue提供用于该字段默认值的数据。了解更多
localized启用该字段的本地化功能。需要在基础配置中启用本地化
required要求该字段必须有值。
admin特定于 Admin Panel 的配置。详见下文
custom用于添加自定义数据(例如插件)的扩展点
typescriptSchema通过提供 JSON schema 来覆盖字段类型生成
virtual设置为 true 以禁用数据库中的字段,或提供字符串路径来将字段与关系链接。参见虚拟字段

* 星号表示该属性为必填项。

管理选项

要自定义 Admin Panel 中 Code Field 的外观和行为,可以使用 admin 选项:

import type { Field } from 'payload'

export const MyCodeField: Field = {
  // ...
  admin: {
    // highlight-line
    // ...
  },
}

Code Field 继承了基础 Field Admin Config 的所有默认管理选项,并新增了以下额外选项:

选项描述
language可设置为此处列出的任何编程语言。
editorOptions可传递给 monaco 编辑器的选项,查看完整列表

示例

`collections/ExampleCollection.ts

import type { CollectionConfig } from 'payload'

export const ExampleCollection: CollectionConfig = {
  slug: 'example-collection',
  fields: [
    {
      name: 'trackingCode', // 必填
      type: 'code', // 必填
      required: true,
      admin: {
        language: 'javascript',
      },
    },
  ],
}

自定义组件

字段组件

服务器组件

import type React from 'react'
import { CodeField } from '@payloadcms/ui'
import type { CodeFieldServerComponent } from 'payload'

export const CustomCodeFieldServer: CodeFieldServerComponent = ({
  clientField,
  path,
  schemaPath,
  permissions,
}) => {
  return (
    <CodeField
      field={clientField}
      path={path}
      schemaPath={schemaPath}
      permissions={permissions}
    />
  )
}

客户端组件

'use client'
import React from 'react'
import { CodeField } from '@payloadcms/ui'
import type { CodeFieldClientComponent } from 'payload'

export const CustomCodeFieldClient: CodeFieldClientComponent = (props) => {
  return <CodeField {...props} />
}

标签

服务器组件

import React from 'react'
import { FieldLabel } from '@payloadcms/ui'
import type { CodeFieldLabelServerComponent } from 'payload'

export const CustomCodeFieldLabelServer: CodeFieldLabelServerComponent = ({
  clientField,
  path,
}) => {
  return (
    <FieldLabel
      label={clientField?.label || clientField?.name}
      path={path}
      required={clientField?.required}
    />
  )
}

客户端组件

'use client'
import React from 'react'
import { FieldLabel } from '@payloadcms/ui'
import type { CodeFieldLabelClientComponent } from 'payload'

export const CustomCodeFieldLabelClient: CodeFieldLabelClientComponent = ({
  field,
  path,
}) => {
  return (
    <FieldLabel
      label={field?.label || field?.name}
      path={path}
      required={field?.required}
    />
  )
}