Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/collection-columns-array-field.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystatic/core': patch
---

Allow scalar `fields.array(...)` fields in `collection().columns` (#1548)
71 changes: 71 additions & 0 deletions packages/keystatic/src/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { expect, test } from 'vitest';
import { collection, fields } from './index';
import { parseProps } from './form/parse-props';

test('collection().columns accepts scalar array fields and rejects object/content fields', () => {
const validCollection = collection({
label: 'Posts',
slugField: 'slug',
columns: ['slug', 'title', 'tags', 'counts', 'slugList'],
schema: {
slug: fields.slug({ name: { label: 'Slug' } }),
title: fields.text({ label: 'Title' }),
tags: fields.array(fields.text({ label: 'Tag' }), { label: 'Tags' }),
counts: fields.array(fields.integer({ label: 'Count' }), {
label: 'Counts',
}),
slugList: fields.array(fields.slug({ name: { label: 'Item slug' } }), {
label: 'Slug list',
}),
nested: fields.array(
fields.object({ name: fields.text({ label: 'Name' }) }),
{ label: 'Nested' }
),
body: fields.markdoc({ label: 'Body' }),
},
});

collection({
label: 'Invalid columns',
slugField: 'slug',
columns: [
// @ts-expect-error array of objects is not allowed in columns
'nested',
// @ts-expect-error content field is not allowed in columns
'body',
],
schema: validCollection.schema,
});

expect(validCollection.columns).toEqual([
'slug',
'title',
'tags',
'counts',
'slugList',
]);

const parsed = parseProps(
{ kind: 'object', fields: validCollection.schema },
{ slug: 'hello', title: 'Hello', tags: ['a', 'b'], counts: [1, 2] },
[],
[],
(schema, value, path) => {
if (schema.formKind === 'content' || schema.formKind === 'assets') {
return;
}
if (
path.length === 1 &&
path[0] === 'slug' &&
schema.formKind === 'slug'
) {
return schema.reader.parseWithSlug(value, { slug: 'hello', glob: '*' });
}
return schema.reader.parse(value);
},
true
);

expect((parsed as { tags: unknown }).tags + '').toBe('a,b');
expect((parsed as { counts: unknown }).counts + '').toBe('1,2');
});
15 changes: 14 additions & 1 deletion packages/keystatic/src/config.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { ColorScheme } from '@keystar/ui/types';
import { ReactElement } from 'react';

import { ComponentSchema, FormField, SlugFormField } from './form/api';
import {
ArrayField,
ComponentSchema,
FormField,
SlugFormField,
} from './form/api';
import type { Locale } from './app/l10n/locales';
import { RepoConfig } from './app/repo-config';

Expand Down Expand Up @@ -189,6 +194,14 @@ export function collection<
string | number | boolean | Date | null | undefined
>
| SlugFormField<any, any, any, string>
| ArrayField<
| FormField<
any,
any,
string | number | boolean | Date | null | undefined
>
| SlugFormField<any, any, any, string>
>
? K & string
: never;
}[keyof Schema][];
Expand Down
Loading