Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(beta): pass req to defaultValue function and better type for defaultValue #5912

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions docs/fields/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,9 @@ The `condition` function should return a boolean that will control if the field

Fields can be prefilled with starting values using the `defaultValue` property. This is used in the admin UI and also on the backend as API requests will be populated with missing or undefined field values. You can assign the defaultValue directly in the field configuration or supply a function for dynamic behavior. Values assigned during a create request on the server are added before validation occurs.

Functions are called with an optional argument object containing:
Functions are called with an argument `req` - The Payload `request` object:

- `user` - the authenticated user object
- `locale` - the currently selected locale string

Here is an example of a defaultValue function that uses both:
Here is an example of a defaultValue function that uses `req`:

```ts
const translation: {
Expand All @@ -241,7 +238,7 @@ const field = {
name: 'attribution',
type: 'text',
// highlight-start
defaultValue: ({ user, locale }) => `${translation[locale]} ${user.name}`,
defaultValue: (req) => `${translation[req.locale]} ${req.user.name}`,
// highlight-end
}
```
Expand Down
2 changes: 1 addition & 1 deletion packages/payload/src/fields/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ export interface FieldBase {
update?: FieldAccess
}
admin?: Admin
defaultValue?: ((req: PayloadRequest) => unknown) | boolean | number | object | string
/** Extension point to add your custom data. Server only. */
custom?: Record<string, any>
defaultValue?: any
hidden?: boolean
hooks?: {
afterChange?: FieldHook[]
Expand Down
8 changes: 3 additions & 5 deletions packages/payload/src/fields/getDefaultValue.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import type { User } from '../auth/index.js'
import type { PayloadRequest } from '../types/index.js'

import { deepCopyObject } from '../utilities/deepCopyObject.js'

type Args = {
defaultValue: unknown
locale: string | undefined
user: PayloadRequest['user']
req: PayloadRequest
value?: unknown
}

const getValueWithDefault = ({ defaultValue, locale, user, value }: Args): unknown => {
const getValueWithDefault = ({ defaultValue, req, value }: Args): unknown => {
if (typeof value !== 'undefined') {
return value
}

if (defaultValue && typeof defaultValue === 'function') {
return defaultValue({ locale, user })
return defaultValue(req)
}

if (typeof defaultValue === 'object') {
Expand Down
3 changes: 1 addition & 2 deletions packages/payload/src/fields/hooks/afterRead/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ export const promise = async ({
) {
siblingDoc[field.name] = await getValueWithDefault({
defaultValue: field.defaultValue,
locale,
user: req.user,
req,
value: siblingDoc[field.name],
})
}
Expand Down
3 changes: 1 addition & 2 deletions packages/payload/src/fields/hooks/beforeValidate/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,7 @@ export const promise = async <T>({
} else if (typeof field.defaultValue !== 'undefined') {
siblingData[field.name] = await getValueWithDefault({
defaultValue: field.defaultValue,
locale: req.locale,
user: req.user,
req,
value: siblingData[field.name],
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export const defaultValuePromise = async <T>({
) {
siblingData[field.name] = await getDefaultValue({
defaultValue: field.defaultValue,
locale: req.locale,
user: req.user,
req,
value: siblingData[field.name],
})
}
Expand Down
Loading