Skip to content

feat!: explicitly types field components - #8136

Merged
jacobsfletch merged 9 commits into
betafrom
types/custom-fields
Sep 9, 2024
Merged

jacobsfletch merged 9 commits into
betafrom
types/custom-fields

Conversation

@jacobsfletch

@jacobsfletch jacobsfletch commented Sep 9, 2024 •

Copy link
Copy Markdown
Member

Description

Currently, there is no way of typing custom server field components. This is because internally, all field components are client components, and so these were never fully typed. For example, the docs currently indicate for all custom fields to be typed in this way:

Old:

export const MyClientTextFieldComponent: React.FC<TextFieldProps>

But if your component is a server component, you will never receive the fully typed field prop, payload prop, etc. unless you've typed that yourself using some of the underlying utilities. So to fix this, every field now explicitly exports a type for each environment:

New:

  • Client component:

    'use client'
    export const MyClientTextFieldComponent: TextFieldClientComponent
  • Server component:

    export const MyServerTextFieldComponent: TextFieldServerComponent

This pattern applies to every field type, where the field name is prepended onto the component type.

import type {
  TextFieldClientComponent,
  TextFieldServerComponent,
  TextFieldClientProps,
  TextFieldServerProps,
  TextareaFieldClientComponent,
  TextareaFieldServerComponent,
  TextareaFieldClientProps,
  TextareaFieldServerProps,
  // ...and so on for each field type
} from 'payload'

BREAKING CHANGES

We are no longer exporting TextFieldProps etc. for each field type. Instead, we now export props for each client/server environment explicitly. If you were previously importing one of these types into your custom component, simply change the import name to reflect your environment.

Old:

import type { TextFieldProps } from 'payload'

New:

import type { TextFieldClientProps, TextFieldServerProps } from 'payload'

Related: #7754.

  • I have read and understand the CONTRIBUTING.md document in this repository.

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Checklist:

  • Existing test suite passes locally with my changes
  • I have made corresponding changes to the documentation

@jacobsfletch jacobsfletch changed the title feat: explicitly defines field component types feat!: explicitly defines field component types Sep 9, 2024
@jacobsfletch
jacobsfletch marked this pull request as ready for review September 9, 2024 19:15
@jacobsfletch jacobsfletch changed the title feat!: explicitly defines field component types feat!: explicitly types field components Sep 9, 2024
@jacobsfletch
jacobsfletch enabled auto-merge (squash) September 9, 2024 20:11
@jacobsfletch
jacobsfletch merged commit 8e1a5c8 into beta Sep 9, 2024
@jacobsfletch
jacobsfletch deleted the types/custom-fields branch September 9, 2024 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant