Skip to content

feat: types custom component props #11894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jacobsfletch
Copy link
Member

Edit: After thinking about this more, I think the component-level types should be deprecated in favor of direct props.

Reasons:

  1. Component-level type only works for components defined as arrow functions, not components defined as regular functions
  2. This has snowballed into types for every single field "slot", i.e. FieldLabelClientComponent and has become an awful pattern

This feels like a mistake. Should have never been written this way. Encouraging the use of props directly eliminates all of this.

Here's the previous description of this PR if anyone is curious:

When creating custom fields and injecting custom props, it is currently not possible to have those props typed within the base fields types that Payload provides.

For example:

{
  type: 'text',
  admin: {
    Field: {
      path: './path-to-my-component',
      clientProps: {
        customProp: 'This is a custom prop'
      }
  }
}

In your component definition, customProp does not exist on the TextFieldServerComponent type:

import { TextFieldServerComponent } from 'payload'

const MyComponent: TextFieldServerComponent = ({
  customProp // THIS DOES NOT EXIST
}) => {
 // ...
}

Currently, to do this you'd need to use the prop types directly:

import { TextFieldServerProps } from 'payload'

const MyComponent: React.FC<TextFieldServerProps & {
  customProp: string
}> = (props) => {
 // ...
}

With this change you could define the custom props on the component-level type by passing it as a generic:

import { TextFieldServerComponent } from 'payload'

const MyComponent: TextFieldServerComponent<{
  customProp: string
}> = ({
  customProp // EXISTS
}) => {
 // ...
}

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