Skip to content

feat(ui)!: passes field props to custom components - #7360

Merged
jacobsfletch merged 35 commits into
betafrom
feat/field-comp-props
Jul 26, 2024
Merged

jacobsfletch merged 35 commits into
betafrom
feat/field-comp-props

Conversation

@jacobsfletch

@jacobsfletch jacobsfletch commented Jul 25, 2024 •

Copy link
Copy Markdown
Member

Description

Currently, there is no way to read field props from within a custom field component, i.e. admin.components.Description. For example, if you set maxLength: 100 on your field, your custom description component cannot read it from props.maxLength or any other methods. Because these components are rendered on the server, there is also no way of using admin.component.Field to inject custom props yourself, either. To support this, we can simply pass the base component props into these components on the server, as expected. This has also led to custom field component props becoming more strictly typed within the config.

This change is considered breaking only because the types have changed. This only affects you if you were previously importing the following types into your own custom components. To migrate, simply change the import paths for that type.

Old:

import type {
  ArrayFieldProps,
  ReducedBlock,
  BlocksFieldProps,
  CheckboxFieldProps,
  CodeFieldProps,
  CollapsibleFieldProps,
  DateFieldProps,
  EmailFieldProps,
  GroupFieldProps,
  HiddenFieldProps,
  JSONFieldProps,
  NumberFieldProps,
  PointFieldProps,
  RadioFieldProps,
  RelationshipFieldProps,
  RichTextComponentProps,
  RowFieldProps,
  SelectFieldProps,
  TabsFieldProps,
  TextFieldProps,
  TextareaFieldProps,
  UploadFieldProps,
  ErrorProps,
  FormFieldBase, 
  FieldComponentProps,
  FieldMap,
  MappedField,
  MappedTab,
  ReducedBlock,
} from '@payloadcms/ui'

New:

import type {
  FormFieldBase, 
  // etc.
} from 'payload'

Custom field components are now much more strongly typed. To make this happen, an explicit type for every custom component has been generated for every field type. The convention is to append DescriptionComponent, LabelComponent, and ErrorComponent onto the end of the field name, i.e. TextFieldDescriptionComponent. Here's an example:

import type { TextFieldDescriptionComponent } from 'payload'

import React from 'react'

export const CustomDescription: TextFieldDescriptionComponent = (props) => {
  return (
    <div id="custom-field-description">{`The max length of this field is: ${props?.maxLength}`}</div>
  )
}

Here's the full list of all new types:

Label Components:

import type {
  ArrayFieldLabelComponent,
  BlocksFieldLabelComponent,
  CheckboxFieldLabelComponent,
  CodeFieldLabelComponent,
  CollapsibleFieldLabelComponent,
  DateFieldLabelComponent,
  EmailFieldLabelComponent,
  GroupFieldLabelComponent,
  HiddenFieldLabelComponent,
  JSONFieldLabelComponent,
  NumberFieldLabelComponent,
  PointFieldLabelComponent,
  RadioFieldLabelComponent,
  RelationshipFieldLabelComponent,
  RichTextFieldLabelComponent,
  RowFieldLabelComponent,
  SelectFieldLabelComponent,
  TabsFieldLabelComponent,
  TextFieldLabelComponent,
  TextareaFieldLabelComponent,
  UploadFieldLabelComponent
} from 'payload'

Error Components:

import type {
  ArrayFieldErrorComponent,
  BlocksFieldErrorComponent,
  CheckboxFieldErrorComponent,
  CodeFieldErrorComponent,
  CollapsibleFieldErrorComponent,
  DateFieldErrorComponent,
  EmailFieldErrorComponent,
  GroupFieldErrorComponent,
  HiddenFieldErrorComponent,
  JSONFieldErrorComponent,
  NumberFieldErrorComponent,
  PointFieldErrorComponent,
  RadioFieldErrorComponent,
  RelationshipFieldErrorComponent,
  RichTextFieldErrorComponent,
  RowFieldErrorComponent,
  SelectFieldErrorComponent,
  TabsFieldErrorComponent,
  TextFieldErrorComponent,
  TextareaFieldErrorComponent,
  UploadFieldErrorComponent
} from 'payload'

Description Components:

import type {
  ArrayFieldDescriptionComponent,
  BlocksFieldDescriptionComponent,
  CheckboxFieldDescriptionComponent,
  CodeFieldDescriptionComponent,
  CollapsibleFieldDescriptionComponent,
  DateFieldDescriptionComponent,
  EmailFieldDescriptionComponent,
  GroupFieldDescriptionComponent,
  HiddenFieldDescriptionComponent,
  JSONFieldDescriptionComponent,
  NumberFieldDescriptionComponent,
  PointFieldDescriptionComponent,
  RadioFieldDescriptionComponent,
  RelationshipFieldDescriptionComponent,
  RichTextFieldDescriptionComponent,
  RowFieldDescriptionComponent,
  SelectFieldDescriptionComponent,
  TabsFieldDescriptionComponent,
  TextFieldDescriptionComponent,
  TextareaFieldDescriptionComponent,
  UploadFieldDescriptionComponent
} from 'payload'

This PR also:

  • Standardizes the FieldBase['label'] type with a new LabelStatic type. This makes type usage much more consistent across components.

  • Simplifies some of the typings in the field component map, removes unneeded <Omit>, etc.

  • Fixes misc. linting issues around voiding promises

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

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist:

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

@JarrodMFlesch

Copy link
Copy Markdown
Contributor

@jacobsfletch nice! Does this mean on custom select fields I will be able to get options?

@jacobsfletch

Copy link
Copy Markdown
Member Author

@jacobsfletch nice! Does this mean on custom select fields I will be able to get options?

You got it 👍 field props will now be provided to all admin.components automatically

@DanRibbens DanRibbens left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great!

@jacobsfletch jacobsfletch changed the title feat(ui): passes field props to custom components feat!: passes field props to custom components Jul 25, 2024
@jacobsfletch jacobsfletch changed the title feat!: passes field props to custom components feat(ui)!: passes field props to custom components Jul 25, 2024
@jmikrut

jmikrut commented Jul 25, 2024

Copy link
Copy Markdown
Member

Looks like we neeed to get a few tests ironed out on this one yet, right?

@jacobsfletch

Copy link
Copy Markdown
Member Author

Looks like we neeed to get a few tests ironed out on this one yet, right?

I've written a few tests for this already, do you mean more tests, or passing tests?

@jacobsfletch
jacobsfletch enabled auto-merge (squash) July 26, 2024 17:55
@jacobsfletch
jacobsfletch disabled auto-merge July 26, 2024 18:02
@jacobsfletch
jacobsfletch merged commit 97837f0 into beta Jul 26, 2024
@jacobsfletch
jacobsfletch deleted the feat/field-comp-props branch July 26, 2024 18:03
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.

4 participants