diff --git a/.eslintrc.js b/.eslintrc.js index 42a1a9efd3..dab80a47a9 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -55,13 +55,14 @@ module.exports = { }, ], rules: { - 'no-sparse-arrays': 'off', 'import/no-extraneous-dependencies': ['error', { packageDir: './' }], 'react/jsx-filename-extension': [2, { extensions: ['.js', '.jsx', '.ts', '.tsx'] }], 'import/prefer-default-export': 'off', 'react/prop-types': 'off', 'react/require-default-props': 'off', 'react/no-unused-prop-types': 'off', + 'no-console': 'warn', + 'no-sparse-arrays': 'off', 'no-underscore-dangle': 'off', 'no-use-before-define': 'off', 'arrow-body-style': 0, diff --git a/demo/collections/DefaultValues.ts b/demo/collections/DefaultValues.ts index 22cc1a2774..16e94005a8 100644 --- a/demo/collections/DefaultValues.ts +++ b/demo/collections/DefaultValues.ts @@ -292,16 +292,13 @@ const DefaultValues: CollectionConfig = { type: 'array', name: 'asyncArray', defaultValue: () => { - console.log('asyncArray called'); - return [{ child: 'ok' }, { }]; - // return [{ child: 'ok', id: '384884' }, { id: '1111' }]; + return [{ child: 'ok' }]; }, fields: [ { name: 'child', type: 'text', defaultValue: () => { - console.log('async child callded'); return 'async child'; }, }, diff --git a/demo/payload-types.ts b/demo/payload-types.ts index ea95fdd1a5..af483b1d77 100644 --- a/demo/payload-types.ts +++ b/demo/payload-types.ts @@ -421,6 +421,7 @@ export interface DefaultValueTest { group?: { nestedText1?: string; nestedText2?: string; + nestedText3?: string; }; array?: { arrayText1?: string; @@ -474,6 +475,12 @@ export interface DefaultValueTest { richText?: { [k: string]: unknown; }[]; + asyncArray?: { + child?: string; + id?: string; + }[]; + asyncText?: string; + function?: string; } /** * This interface was referenced by `Config`'s JSON-Schema @@ -615,7 +622,7 @@ export interface RelationshipA { export interface RelationshipB { id: string; title?: string; - disableRelation?: boolean; + disableRelation: boolean; post?: (string | RelationshipA)[]; postManyRelationships?: | { diff --git a/docs/fields/overview.mdx b/docs/fields/overview.mdx index 2c3829591d..c07ac97158 100644 --- a/docs/fields/overview.mdx +++ b/docs/fields/overview.mdx @@ -230,37 +230,34 @@ const customFieldWithCondition = { ### DefaultValue -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. +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 given an optional argument object containing: +Functions are called with an optional argument object containing: - `user` - the authenticated user object - `locale` - the currently selected locale string -An example of when to use a function is when you need to supply fields with locale specific defaults. +Here is an example of a defaultValue function that uses both: ```js -const localizedDefaults = { - welcome: { - en: 'Hello World', - es: 'Hola Mundo', - }, +const translation: { + en: 'Written by', + es: 'escrito por', }; const field = { - name: 'welcome', + name: 'attribution', type: 'text', - maxLength: 20, admin: { // highlight-start - description: ({ user, locale }) => (localizedDefaults.welcome[locale]) + description: ({ user, locale }) => (`${translation[locale]} ${user.name}`) // highlight-end } }; ``` - - `defaultValue` functions can use `async` callbacks allowing requests to be made to populate field data from other sources. + + `defaultValue` also supports `async` functions allowing requests to be made to populate field data from a query. ### Description diff --git a/src/admin/components/forms/Form/index.tsx b/src/admin/components/forms/Form/index.tsx index fa4b46e5ed..9bd6022c06 100644 --- a/src/admin/components/forms/Form/index.tsx +++ b/src/admin/components/forms/Form/index.tsx @@ -382,14 +382,6 @@ const Form: React.FC = (props) => { baseClass, ].filter(Boolean).join(' '); - if (log) { - // eslint-disable-next-line no-console - console.log(fields); - } - - console.log(fields); - - return (