文本区域字段

Textarea 字段与 Text 字段几乎相同,但它提供了一个稍大的输入区域,更适合编辑较长的文本内容。

展示 Payload 管理面板中的文本区域字段和只读文本区域字段
管理面板中的 Textarea 字段和只读 Textarea 字段截图

要添加 Textarea 字段,只需在 字段配置 中将 type 设置为 textarea

import type { Field } from 'payload'

export const MyTextareaField: Field = {
  // ...
  type: 'textarea', // highlight-line
}

配置选项

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

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

管理选项

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

import type { Field } from 'payload'

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

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

选项描述
placeholder设置此属性可定义文本区域中的占位符字符串。
autoComplete设置此属性为字符串,将用于浏览器自动填充。
rows设置文本区域中可见文本行数。如未指定,默认为 2
rtl覆盖此字段在 Admin Panel 中的默认文本方向。设置为 true 可强制使用从右到左的文本方向。

示例

collections/ExampleCollection.ts

import type { CollectionConfig } from 'payload'

export const ExampleCollection: CollectionConfig = {
  slug: 'example-collection',
  fields: [
    {
      name: 'metaDescription', // 必填
      type: 'textarea', // 必填
      required: true,
    },
  ],
}

自定义组件

字段

服务端组件

import type React from 'react'
import { TextareaField } from '@payloadcms/ui'
import type { TextareaFieldServerComponent } from 'payload'

export const CustomTextareaFieldServer: TextareaFieldServerComponent = ({
  clientField,
  path,
  schemaPath,
  permissions,
}) => {
  return (
    <TextareaField
      field={clientField}
      path={path}
      schemaPath={schemaPath}
      permissions={permissions}
    />
  )
}

客户端组件

'use client'
import React from 'react'
import { TextareaField } from '@payloadcms/ui'
import type { TextareaFieldClientComponent } from 'payload'

export const CustomTextareaFieldClient: TextareaFieldClientComponent = (
  props,
) => {
  return <TextareaField {...props} />
}

标签组件

服务端组件

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

export const CustomTextareaFieldLabelServer: TextareaFieldLabelServerComponent =
  ({ 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 { TextareaFieldLabelClientComponent } from 'payload'

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