Skip to content

feat: threads field config through components and strictly types props - #7754

Merged
jacobsfletch merged 23 commits into
betafrom
fix/field-comp-props
Aug 20, 2024
Merged

jacobsfletch merged 23 commits into
betafrom
fix/field-comp-props

Conversation

@jacobsfletch

@jacobsfletch jacobsfletch commented Aug 19, 2024 •

Copy link
Copy Markdown
Member

Description

Threads the field config to all "field subcomponents" through props, i.e. field label, description, error, etc. This way, the field config that controls any particular component is easily accessible and strongly typed, i.e. props.field.maxLength. This is true for both server and client components, whose server-side props are now also contextually typed. This behavior was temporarily removed in #7474 due to bloating HTML, but has since been resolved in #7620. This PR also makes significant improvements to component types by exporting explicit types for every component of every field, each with its own client/server variation. Now, a custom component can look something like this:

import type { TextFieldLabelServerComponent } from 'payload'

import React from 'react'

export const CustomLabel: TextFieldLabelServerComponent = (props) => {
  return (
    <div>{`The max length of this field is: ${props?.field?.maxLength}`}</div>
  )
}

The following types are now available:

import type {
  TextFieldClientComponent,
  TextFieldServerComponent,
  TextFieldLabelClientComponent,
  TextFieldLabelServerComponent,
  TextFieldDescriptionClientComponent,
  TextFieldDescriptionServerComponent,
  TextFieldErrorClientComponent,
  TextFieldErrorServerComponent,
  // ...and so one for each field
} from 'payload'

BREAKING CHANGES:

In order to strictly type these components, a few breaking changes have been made solely to type definitions. This only effects you if you are heavily using custom components.

Old

import type { ErrorComponent, LabelComponent, DescriptionComponent } from 'payload'

New:

import type {
  FieldErrorClientComponent,
  FieldErrorServerComponent,
  FieldLabelClientComponent,
  FieldLabelServerComponent,
  FieldDescriptionClientComponent,
  FieldDescriptionServerComponent,
  // Note: these are the generic, underlying types of the more stricter types described above ^
  // For example, you should use the type that is explicit for your particular field and environment
  // i.e. `TextFieldLabelClientComponent` and not simply `FieldLabelClientComponent`
} from 'payload'
  • I have read and understand the CONTRIBUTING.md document in this repository.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have added tests that prove my fix is effective or that my feature works
  • Existing test suite passes locally with my changes

@jacobsfletch jacobsfletch changed the title fix: field comp props fix: threads field config through props and strictly types sub components Aug 19, 2024
@jacobsfletch jacobsfletch changed the title fix: threads field config through props and strictly types sub components feat: threads field config through subcomponents and strictly types props Aug 19, 2024
@jacobsfletch
jacobsfletch marked this pull request as ready for review August 19, 2024 17:24
@AlessioGr AlessioGr self-assigned this Aug 19, 2024
Comment thread packages/ui/src/fields/Select/Input.tsx Resolved
@jacobsfletch jacobsfletch changed the title feat: threads field config through subcomponents and strictly types props feat: threads field config through components and strictly types props Aug 20, 2024
@jacobsfletch
jacobsfletch enabled auto-merge (squash) August 20, 2024 04:13
@jacobsfletch
jacobsfletch merged commit 3a91deb into beta Aug 20, 2024
@jacobsfletch
jacobsfletch deleted the fix/field-comp-props branch August 20, 2024 04:25
jacobsfletch added a commit that referenced this pull request Sep 9, 2024
## 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:
    
```tsx
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:
    ```tsx
    'use client'
    export const MyClientTextFieldComponent: TextFieldClientComponent
    ```

- Server component:
    ```tsx
    export const MyServerTextFieldComponent: TextFieldServerComponent
    ```

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

```ts
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:

```tsx
import type { TextFieldProps } from 'payload'
``` 

New:

```tsx
import type { TextFieldClientProps, TextFieldServerProps } from 'payload'
``` 

Related: #7754. 

- [x] I have read and understand the
[CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md)
document in this repository.

## Type of change

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

## Checklist:

- [x] Existing test suite passes locally with my changes
- [x] I have made corresponding changes to the documentation
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.

2 participants