Compare commits

...

1 Commits

Author SHA1 Message Date
Sasha
ccb4da475f temp 2025-04-04 20:39:41 +03:00
4 changed files with 105 additions and 1 deletions

View File

@@ -604,6 +604,63 @@ export default buildConfigWithDefaults({
},
],
},
{
slug: 'array-default-id',
versions: { drafts: true },
fields: [
{
type: 'tabs',
tabs: [
{
label: 'General',
fields: [
{
name: 'name',
type: 'text',
required: true,
},
],
},
{
label: 'Content',
fields: [
// ...
{
name: 'myItems',
labels: {
singular: 'item',
plural: 'items',
},
type: 'array',
required: false,
defaultValue: Array(4).fill({ name: '', description: '' }),
fields: [
{
name: 'name',
type: 'text',
required: true,
},
{
name: 'description',
type: 'text',
required: true,
},
],
},
],
},
// ...
],
},
// {
// name: 'array',
// type: 'array',
// fields: [{ type: 'text', name: 'text' }],
// defaultValue: Array(100).fill({ text: 'text-1' }),
// // defaultValue: Array.from({ length: 4 }, () => ({ text: 'text-1' })),
// },
],
},
],
globals: [
{

View File

@@ -1627,6 +1627,18 @@ describe('database', () => {
expect(result.select).toStrictEqual('default')
expect(result.point).toStrictEqual({ coordinates: [10, 20], type: 'Point' })
})
it('defaultValue should work with arrays', async () => {
const res_1 = await payload.create({ draft: true, collection: 'array-default-id', data: {} })
expect(res_1.array).toHaveLength(4)
expect(res_1.array[0]?.text).toBe('text-1')
expect(res_1.array[1]?.text).toBe('text-1')
// const res_2 = await payload.create({ draft: true, collection: 'array-default-id', data: {} })
// expect(res_2.array).toHaveLength(2)
// expect(res_2.array[0]?.text).toBe('text-1')
// expect(res_2.array[1]?.text).toBe('text-2')
})
})
describe('Schema generation', () => {

View File

@@ -80,6 +80,7 @@ export interface Config {
'fake-custom-ids': FakeCustomId;
'relationships-migration': RelationshipsMigration;
'compound-indexes': CompoundIndex;
'array-default-id': ArrayDefaultId;
users: User;
'payload-locked-documents': PayloadLockedDocument;
'payload-preferences': PayloadPreference;
@@ -100,6 +101,7 @@ export interface Config {
'fake-custom-ids': FakeCustomIdsSelect<false> | FakeCustomIdsSelect<true>;
'relationships-migration': RelationshipsMigrationSelect<false> | RelationshipsMigrationSelect<true>;
'compound-indexes': CompoundIndexesSelect<false> | CompoundIndexesSelect<true>;
'array-default-id': ArrayDefaultIdSelect<false> | ArrayDefaultIdSelect<true>;
users: UsersSelect<false> | UsersSelect<true>;
'payload-locked-documents': PayloadLockedDocumentsSelect<false> | PayloadLockedDocumentsSelect<true>;
'payload-preferences': PayloadPreferencesSelect<false> | PayloadPreferencesSelect<true>;
@@ -419,6 +421,21 @@ export interface CompoundIndex {
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "array-default-id".
*/
export interface ArrayDefaultId {
id: string;
array?:
| {
text?: string | null;
id?: string | null;
}[]
| null;
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "users".
@@ -495,6 +512,10 @@ export interface PayloadLockedDocument {
relationTo: 'compound-indexes';
value: string | CompoundIndex;
} | null)
| ({
relationTo: 'array-default-id';
value: string | ArrayDefaultId;
} | null)
| ({
relationTo: 'users';
value: string | User;
@@ -795,6 +816,20 @@ export interface CompoundIndexesSelect<T extends boolean = true> {
updatedAt?: T;
createdAt?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "array-default-id_select".
*/
export interface ArrayDefaultIdSelect<T extends boolean = true> {
array?:
| T
| {
text?: T;
id?: T;
};
updatedAt?: T;
createdAt?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "users_select".

View File

@@ -31,7 +31,7 @@
}
],
"paths": {
"@payload-config": ["./test/_community/config.ts"],
"@payload-config": ["./test/database/config.ts"],
"@payloadcms/admin-bar": ["./packages/admin-bar/src"],
"@payloadcms/live-preview": ["./packages/live-preview/src"],
"@payloadcms/live-preview-react": ["./packages/live-preview-react/src/index.ts"],