Skip to content

Commit

Permalink
fix: threads path and value through field desc fns again and defers c…
Browse files Browse the repository at this point in the history
…lient-side rendering
  • Loading branch information
jacobsfletch committed Nov 17, 2024
1 parent c9040b6 commit 18ccfe2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
10 changes: 9 additions & 1 deletion docs/admin/fields.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const MyCollectionConfig: SanitizedCollectionConfig = {

#### Description Functions

Custom Descriptions can also be defined as a function. Description Functions are executed on the server and can be used to format simple descriptions based on the user's current [Locale](../configuration/localization).
Custom Descriptions can also be defined as a function. Description Functions are executed on the server and can be used to format simple descriptions based on the user's current [Locale](../configuration/localization), document data, etc.

To easily add a Description Function to a field, set the `admin.description` property to a _function_ in your [Field Config](../fields/overview):

Expand All @@ -120,6 +120,14 @@ All Description Functions receive the following arguments:
| Argument | Description |
| -------------- | ---------------------------------------------------------------- |
| **`t`** | The `t` function used to internationalize the Admin Panel. [More details](../configuration/i18n) |
| **`path`** | A string representing the direct, dynamic path to the field at runtime, i.e. `myGroup.myArray.0.myField`. |
| **`data`** | The initial data of the document. |
| **`value`** | The value of the field at render-time. |

<Banner type="info">
<strong>Note:</strong>
If you need to subscribe to live changes within your form, use must use a Description Component instead. [More details](#description).
</Banner>

## Conditional Logic

Expand Down
16 changes: 13 additions & 3 deletions packages/payload/src/admin/forms/Description.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import type { LabelFunction, ServerProps } from '../../config/types.js'
import type { TFunction } from '@payloadcms/translations'

import type { ServerProps } from '../../config/types.js'
import type { Field } from '../../fields/config/types.js'
import type { ClientFieldWithOptionalType } from './Field.js'

export type DescriptionFunction = LabelFunction
import type { Data } from './Form.js'

export type DescriptionFunction = <T = unknown>({
t,
}: {
data: Data
path: string
t: TFunction
value?: T
}) => string

export type FieldDescriptionClientComponent<
TFieldClient extends ClientFieldWithOptionalType = ClientFieldWithOptionalType,
Expand Down
11 changes: 3 additions & 8 deletions packages/payload/src/fields/config/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,10 @@ export const createClientField = ({
} & ClientField

if (incomingField.admin && 'description' in incomingField.admin) {
if (
typeof incomingField.admin?.description === 'string' ||
typeof incomingField.admin?.description === 'object'
) {
if (typeof incomingField.admin?.description === 'function') {
delete (clientField as FieldWithDescription).admin.description
} else {
;(clientField as FieldWithDescription).admin.description = incomingField.admin.description
} else if (typeof incomingField.admin?.description === 'function') {
;(clientField as FieldWithDescription).admin.description = incomingField.admin?.description({
t: i18n.t,
})
}
}

Expand Down
27 changes: 13 additions & 14 deletions packages/ui/src/forms/fieldSchemasToFormState/renderField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,20 +202,19 @@ export const renderField: RenderFieldMethod = ({

if (fieldConfig.admin) {
if ('description' in fieldConfig.admin) {
// @TODO move this to client, only render if it is a function
fieldState.customComponents.Description = (
<FieldDescription
description={
typeof fieldConfig.admin?.description === 'string' ||
typeof fieldConfig.admin?.description === 'object'
? fieldConfig.admin.description
: typeof fieldConfig.admin?.description === 'function'
? fieldConfig.admin?.description({ t: req.i18n.t })
: ''
}
path={path}
/>
)
if (typeof fieldConfig.admin?.description === 'function') {
fieldState.customComponents.Description = (
<FieldDescription
description={fieldConfig.admin?.description({
data,
path,
t: req.i18n.t,
value: 'name' in fieldConfig ? formState[fieldConfig.name] : undefined,
})}
path={path}
/>
)
}
}

if (fieldConfig.admin?.components) {
Expand Down

0 comments on commit 18ccfe2

Please sign in to comment.