Files
payload/test/fields/collections/Blocks/index.ts
Sasha 26a10ed071 perf: reduce generated types for select by respecting interfaceName (#9870)
This PR can significantly reduce your `payload-types.ts` file if you
have sharable fields / blocks that use the `interfaceName` property.
Previously we didn't respect it for select types.

Before:
```ts
export interface Collection1Select<T extends boolean = true> {
  testing?: T;
  title?: T;
  meta?:
    | T
    | {
        title?: T;
        description?: T;
        id?: T;
      };
  blocks?:
    | T
    | {
        block1?:
          | T
          | {
              b1title?: T;
              b1description?: T;
              id?: T;
              blockName?: T;
            };
        block2?:
          | T
          | {
              b2title?: T;
              b2description?: T;
              id?: T;
              blockName?: T;
            };
      };
  updatedAt?: T;
  createdAt?: T;
}
```
After:
```ts
export interface Collection1Select<T extends boolean = true> {
  testing?: T;
  title?: T;
  meta?: T | SharedMetaArraySelect<T>;
  blocks?:
    | T
    | {
        block1?: T | SharedMetaBlockSelect<T>;
        block2?: T | AnotherSharedBlockSelect<T>;
      };
  updatedAt?: T;
  createdAt?: T;
}

/**
 * This interface was referenced by `Config`'s JSON-Schema
 * via the `definition` "SharedMetaArray_select".
 */
export interface SharedMetaArraySelect<T extends boolean = true> {
  title?: T;
  description?: T;
  id?: T;
}
/**
 * This interface was referenced by `Config`'s JSON-Schema
 * via the `definition` "SharedMetaBlock_select".
 */
export interface SharedMetaBlockSelect<T extends boolean = true> {
  b1title?: T;
  b1description?: T;
  id?: T;
  blockName?: T;
}
/**
 * This interface was referenced by `Config`'s JSON-Schema
 * via the `definition` "AnotherSharedBlock_select".
 */
export interface AnotherSharedBlockSelect<T extends boolean = true> {
  b2title?: T;
  b2description?: T;
  id?: T;
  blockName?: T;
}
```

Regenerated all the types in `/test`. The diff is noticeable for
`fields` -
https://github.com/payloadcms/payload/pull/9870/files#diff-95beaac24c72c7bd60933e325cdcd94a4c3630a1ce22fabad624ec80cc74fc8c
2024-12-16 17:22:17 +02:00

375 lines
8.0 KiB
TypeScript

import type { BlocksField, CollectionConfig } from 'payload'
import { slateEditor } from '@payloadcms/richtext-slate'
import { blockFieldsSlug, textFieldsSlug } from '../../slugs.js'
import { getBlocksFieldSeedData } from './shared.js'
export const getBlocksField = (prefix?: string): BlocksField => ({
name: 'blocks',
type: 'blocks',
blocks: [
{
slug: prefix ? `${prefix}Content` : 'content',
interfaceName: prefix ? `${prefix}ContentBlock` : 'ContentBlock',
fields: [
{
name: 'text',
type: 'text',
required: true,
},
{
name: 'richText',
type: 'richText',
editor: slateEditor({}),
},
],
},
{
slug: prefix ? `${prefix}Number` : 'number',
interfaceName: prefix ? `${prefix}NumberBlock` : 'NumberBlock',
fields: [
{
name: 'number',
type: 'number',
required: true,
},
],
},
{
slug: prefix ? `${prefix}SubBlocks` : 'subBlocks',
interfaceName: prefix ? `${prefix}SubBlocksBlock` : 'SubBlocksBlock',
fields: [
{
type: 'collapsible',
fields: [
{
name: 'subBlocks',
type: 'blocks',
blocks: [
{
slug: 'text',
fields: [
{
name: 'text',
type: 'text',
required: true,
},
],
},
{
slug: 'number',
fields: [
{
name: 'number',
type: 'number',
required: true,
},
],
},
],
},
],
label: 'Collapsible within Block',
},
],
},
{
slug: prefix ? `${prefix}Tabs` : 'tabs',
interfaceName: prefix ? `${prefix}TabsBlock` : 'TabsBlock',
fields: [
{
type: 'tabs',
tabs: [
{
fields: [
{
type: 'collapsible',
fields: [
{
// collapsible
name: 'textInCollapsible',
type: 'text',
},
],
label: 'Collapsible within Block',
},
{
type: 'row',
fields: [
{
// collapsible
name: 'textInRow',
type: 'text',
},
],
},
],
label: 'Tab with Collapsible',
},
],
},
],
},
],
defaultValue: getBlocksFieldSeedData(prefix),
required: true,
})
const BlockFields: CollectionConfig = {
slug: blockFieldsSlug,
fields: [
getBlocksField(),
{
...getBlocksField(),
name: 'duplicate',
},
{
...getBlocksField('localized'),
name: 'collapsedByDefaultBlocks',
admin: {
initCollapsed: true,
},
localized: true,
},
{
...getBlocksField('localized'),
name: 'disableSort',
admin: {
isSortable: false,
},
localized: true,
},
{
...getBlocksField('localized'),
name: 'localizedBlocks',
localized: true,
},
{
name: 'i18nBlocks',
type: 'blocks',
blocks: [
{
slug: 'text',
fields: [
{
name: 'text',
type: 'text',
},
],
graphQL: {
singularName: 'I18nText',
},
labels: {
plural: {
en: 'Texts en',
es: 'Texts es',
},
singular: {
en: 'Text en',
es: 'Text es',
},
},
},
],
label: {
en: 'Block en',
es: 'Block es',
},
labels: {
plural: {
en: 'Blocks en',
es: 'Blocks es',
},
singular: {
en: 'Block en',
es: 'Block es',
},
},
},
{
name: 'blocksWithLocalizedArray',
type: 'blocks',
blocks: [
{
slug: 'localizedArray',
fields: [
{
name: 'array',
type: 'array',
localized: true,
fields: [
{
name: 'text',
type: 'text',
},
],
},
],
},
],
},
{
name: 'blocksWithSimilarConfigs',
type: 'blocks',
blocks: [
{
slug: 'block-a',
fields: [
{
name: 'items',
type: 'array',
fields: [
{
name: 'title',
type: 'text',
required: true,
},
],
},
],
},
{
slug: 'block-b',
fields: [
{
name: 'items',
type: 'array',
fields: [
{
name: 'title2',
type: 'text',
required: true,
},
],
},
],
},
{
slug: 'group-block',
fields: [
{
name: 'group',
type: 'group',
fields: [
{
name: 'text',
type: 'text',
},
],
},
],
},
],
},
{
name: 'blocksWithSimilarGroup',
type: 'blocks',
admin: {
description:
'The purpose of this field is to test validateExistingBlockIsIdentical works with similar blocks with group fields',
},
blocks: [
{
slug: 'group-block',
fields: [
{
name: 'group',
type: 'group',
fields: [
{
name: 'text',
type: 'text',
},
],
},
],
},
{
slug: 'block-b',
fields: [
{
name: 'items',
type: 'array',
fields: [
{
name: 'title2',
type: 'text',
required: true,
},
],
},
],
},
],
},
{
name: 'blocksWithMinRows',
type: 'blocks',
blocks: [
{
slug: 'block',
fields: [
{
name: 'blockTitle',
type: 'text',
},
],
},
],
minRows: 2,
},
{
name: 'customBlocks',
type: 'blocks',
blocks: [
{
slug: 'block-1',
fields: [
{
name: 'block1Title',
type: 'text',
},
],
},
{
slug: 'block-2',
fields: [
{
name: 'block2Title',
type: 'text',
},
],
},
],
},
{
name: 'ui',
type: 'ui',
admin: {
components: {
Field: '/collections/Blocks/components/AddCustomBlocks/index.js#AddCustomBlocks',
},
},
},
{
name: 'relationshipBlocks',
type: 'blocks',
blocks: [
{
slug: 'relationships',
fields: [
{
name: 'relationship',
type: 'relationship',
relationTo: textFieldsSlug,
},
],
},
],
},
],
}
export default BlockFields