-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Conversation
I love the idea of passing I'm considering it may make sense to keep Maybe passing req is all we will ever need though, I can't think of a scenario that this is the case currently and adding to |
I thought about that but then you would have data duplication in the argument object, it's not like a big deal but even in for example |
@DanRibbens I have a group meta field used in multiple places. I want to have the
Unless you have a better suggestion. |
i think that shouldn't be specfic to import type { Field } from 'payload/types';
import type { Config } from 'payload-types';
const numberField = (collection: keyof Config['collections']): Field => {
return {
defaultValue: (req) => {
switch (collection) {
case 'admins':
return 3;
case 'posts':
return 4;
}
return 5;
},
name: 'someNumber',
type: 'number',
};
}; Then in your collection config: export const Collection: CollectionConfig = {
fields: [numberField('admins')],
slug: 'admins',
}; am i wrong about this? you don't even need to have // in func
let defaultValue: number = 5;
switch (collection) {
case "admins":
defaultValue = 4;
}
return {
type: "number",
defaultValue,
name: "someNumber"
| |
@r1tsuu Thank you for the suggestion! This would work, but I have to make a lot of changes to take advantage of it. I have a reusable meta field and I don't want to manually pass the collection I use it in. It is basically this:
If I provide a parameter to |
I agree with @r1tsuu. This is on the side of your config code and payload doesn't pass the collection config to itself like that. It is better that we don't introduce circular references. Because you already have a reused field it should be quick to find and replace it or change symbol in your IDE. |
Is it also possible to pass the search params? I need the id of a parent document as the default value for a drawer document (e.g. set a customer id as default value inside an order). |
This PR solves that as |
@r1tsuu @DanRibbens Do you guys have a status on this? I currently have a case where I need 'req' to be part of defaultValue. |
This PR is stale due to lack of activity. To keep the PR open, please indicate that it is still relevant in a comment below. |
reworked here #9937 |
Rework of #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()`
Description
Currently
defaultValue
function argument is called only server side, so why pass onlylocale
anduser
? It shouldn't be a breaking change becausereq
contains them. You also can't use the Local API indefaultValue
because of that,.Improved type for
defaultValue
, should respect all non-function values and function that acceptsPayloadRequest
.Type of change
Checklist: