From 01cc2c4ee4e7e8269c7cca9ee9afb3d592b0108a Mon Sep 17 00:00:00 2001 From: Milind Chatrabhuji Date: Wed, 25 Nov 2026 12:16:03 +0530 Subject: [PATCH] fix(keystatic): allow scalar array fields in collection columns (#1548) --- .changeset/collection-columns-array-field.md | 5 ++ packages/keystatic/src/config.test.ts | 71 ++++++++++++++++++++ packages/keystatic/src/config.tsx | 15 ++++- 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 .changeset/collection-columns-array-field.md create mode 100644 packages/keystatic/src/config.test.ts diff --git a/.changeset/collection-columns-array-field.md b/.changeset/collection-columns-array-field.md new file mode 100644 index 000000000..dd8deddf8 --- /dev/null +++ b/.changeset/collection-columns-array-field.md @@ -0,0 +1,5 @@ +--- +'@keystatic/core': patch +--- + +Allow scalar `fields.array(...)` fields in `collection().columns` (#1548) diff --git a/packages/keystatic/src/config.test.ts b/packages/keystatic/src/config.test.ts new file mode 100644 index 000000000..8f946118b --- /dev/null +++ b/packages/keystatic/src/config.test.ts @@ -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'); +}); diff --git a/packages/keystatic/src/config.tsx b/packages/keystatic/src/config.tsx index 4c95368f3..968aaf859 100644 --- a/packages/keystatic/src/config.tsx +++ b/packages/keystatic/src/config.tsx @@ -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'; @@ -189,6 +194,14 @@ export function collection< string | number | boolean | Date | null | undefined > | SlugFormField + | ArrayField< + | FormField< + any, + any, + string | number | boolean | Date | null | undefined + > + | SlugFormField + > ? K & string : never; }[keyof Schema][];