feat: add support for interfaceName on radio and select fields to create reusable top level types (#11277)
Adds support for `interfaceName` on radio and select fields. Adding this property will extract your provided options into a top level type for re-use.  Added types test to make sure assignment is consistent.
This commit is contained in:
@@ -22,6 +22,38 @@ export default buildConfigWithDefaults({
|
||||
type: 'text',
|
||||
name: 'title',
|
||||
},
|
||||
{
|
||||
name: 'selectField',
|
||||
type: 'select',
|
||||
required: true,
|
||||
interfaceName: 'MySelectOptions',
|
||||
options: [
|
||||
{
|
||||
label: 'Option 1',
|
||||
value: 'option-1',
|
||||
},
|
||||
{
|
||||
label: 'Option 2',
|
||||
value: 'option-2',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'radioField',
|
||||
type: 'radio',
|
||||
required: true,
|
||||
interfaceName: 'MyRadioOptions',
|
||||
options: [
|
||||
{
|
||||
label: 'Option 1',
|
||||
value: 'option-1',
|
||||
},
|
||||
{
|
||||
label: 'Option 2',
|
||||
value: 'option-2',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
|
||||
@@ -6,6 +6,16 @@
|
||||
* and re-run `payload generate:types` to regenerate this file.
|
||||
*/
|
||||
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "MySelectOptions".
|
||||
*/
|
||||
export type MySelectOptions = 'option-1' | 'option-2';
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "MyRadioOptions".
|
||||
*/
|
||||
export type MyRadioOptions = 'option-1' | 'option-2';
|
||||
/**
|
||||
* Supported timezones in IANA format.
|
||||
*
|
||||
@@ -132,6 +142,8 @@ export interface Post {
|
||||
id: string;
|
||||
text?: string | null;
|
||||
title?: string | null;
|
||||
selectField: MySelectOptions;
|
||||
radioField: MyRadioOptions;
|
||||
updatedAt: string;
|
||||
createdAt: string;
|
||||
}
|
||||
@@ -249,6 +261,8 @@ export interface PayloadMigration {
|
||||
export interface PostsSelect<T extends boolean = true> {
|
||||
text?: T;
|
||||
title?: T;
|
||||
selectField?: T;
|
||||
radioField?: T;
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,14 @@ import type {
|
||||
import payload from 'payload'
|
||||
import { describe, expect, test } from 'tstyche'
|
||||
|
||||
import type { Menu, Post, User } from './payload-types.js'
|
||||
import type {
|
||||
Menu,
|
||||
MyRadioOptions,
|
||||
MySelectOptions,
|
||||
Post,
|
||||
SupportedTimezones,
|
||||
User,
|
||||
} from './payload-types.js'
|
||||
|
||||
const asType = <T>() => {
|
||||
return '' as T
|
||||
@@ -124,4 +131,18 @@ describe('Types testing', () => {
|
||||
}>()
|
||||
})
|
||||
})
|
||||
|
||||
describe('generated types', () => {
|
||||
test('has SupportedTimezones', () => {
|
||||
expect<SupportedTimezones>().type.toBeAssignableTo<string>()
|
||||
})
|
||||
|
||||
test('has global generated options interface based on select field', () => {
|
||||
expect(asType<Post['selectField']>()).type.toBe<MySelectOptions>()
|
||||
})
|
||||
|
||||
test('has global generated options interface based on radio field', () => {
|
||||
expect(asType<Post['radioField']>()).type.toBe<MyRadioOptions>()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user