feat: expose req to defaultValue function arguments (#9937)

Rework of https://github.com/payloadcms/payload/pull/5912

### What?
Now, when `defaultValue` is defined as function you can receive the
`req` argument:
```ts
{
  name: 'defaultValueFromReq',
  type: 'text',
  defaultValue: async ({ req, user, locale }) => {
    return Promise.resolve(req.context.defaultValue)
  },
},
```

`user` and `locale` even though are repeated in `req`, this potentially
leaves some room to add more args in the future without removing them
now.
This also improves type for `defaultValue`:
```ts
type SerializableValue = boolean | number | object | string
export type DefaultValue =
  | ((args: {
      locale?: TypedLocale
      req: PayloadRequest
      user: PayloadRequest['user']
    }) => SerializableValue)
  | SerializableValue
```

### Why?
To access the current URL / search params / Local API and other things
directly in `defaultValue`.

### How?
Passes `req` through everywhere where we call `defaultValue()`
This commit is contained in:
Sasha
2024-12-16 20:18:11 +02:00
committed by GitHub
parent 26a10ed071
commit 6dea111d28
13 changed files with 64 additions and 12 deletions

View File

@@ -783,6 +783,7 @@ export interface TextField {
localizedHasMany?: string[] | null;
withMinRows?: string[] | null;
withMaxRows?: string[] | null;
defaultValueFromReq?: string | null;
disableListColumnText?: string | null;
disableListFilterText?: string | null;
array?:
@@ -2997,6 +2998,7 @@ export interface TextFieldsSelect<T extends boolean = true> {
localizedHasMany?: T;
withMinRows?: T;
withMaxRows?: T;
defaultValueFromReq?: T;
disableListColumnText?: T;
disableListFilterText?: T;
array?: