Files
payloadcms/packages/payload/src/admin/elements/Cell.ts
Jarrod Flesch 77c99c2f49 feat!: re-order DefaultCellComponentProps generics (#9207)
### What?
Changes the order of the `DefaultCellComponentProps` generic type,
allowing us to infer the type of cellData when a ClientField type is
passed as the first generic argument. You can override the cellData type
by passing the second generic.

Previously:
```ts
type DefaultCellComponentProps<TCellData = any, TField extends ClientField = ClientField>
```

New:
```ts
type DefaultCellComponentProps<TField extends ClientField = ClientField, TCellData = undefined>
```

### Why?
Changing the ClientField type to be the first argument allows us to
infer the cellData value type based on the type of field.

I could have kept the same signature but the usage would look like:
```ts
// Not very DX friendly
const MyCellComponent<DefaultCellComponentProps<,ClientField>> = () => null
```

### How?
The changes made
[here](https://github.com/payloadcms/payload/compare/chore/beta/simplify-DefaultCellComponentProps?expand=1#diff-24f3c92e546c2be3fed0bab305236bba83001309a7239c20a3e3dbd6f5f71dc6R29-R73)
allow this. You can override the type by passing in the second argument
to the generic.
2024-11-14 12:31:42 -05:00

96 lines
3.0 KiB
TypeScript

import type { I18nClient } from '@payloadcms/translations'
import type { ClientCollectionConfig } from '../../collections/config/client.js'
import type { SanitizedCollectionConfig } from '../../collections/config/types.js'
import type {
ArrayFieldClient,
BlocksFieldClient,
CheckboxFieldClient,
ClientField,
CodeFieldClient,
DateFieldClient,
EmailFieldClient,
Field,
GroupFieldClient,
JSONFieldClient,
NumberFieldClient,
PointFieldClient,
RadioFieldClient,
RelationshipFieldClient,
SelectFieldClient,
TextareaFieldClient,
TextFieldClient,
UploadFieldClient,
} from '../../fields/config/types.js'
import type { Payload } from '../../types/index.js'
export type RowData = Record<string, any>
export type DefaultCellComponentProps<
TField extends ClientField = ClientField,
TCellData = undefined,
> = {
readonly cellData: TCellData extends undefined
? TField extends RelationshipFieldClient
? number | Record<string, any> | string
: TField extends NumberFieldClient
? TField['hasMany'] extends true
? number[]
: number
: TField extends TextFieldClient
? TField['hasMany'] extends true
? string[]
: string
: TField extends
| CodeFieldClient
| EmailFieldClient
| JSONFieldClient
| RadioFieldClient
| TextareaFieldClient
? string
: TField extends BlocksFieldClient
? {
[key: string]: any
blockType: string
}[]
: TField extends CheckboxFieldClient
? boolean
: TField extends DateFieldClient
? Date | number | string
: TField extends GroupFieldClient
? Record<string, any>
: TField extends UploadFieldClient
? File | string
: TField extends ArrayFieldClient
? Record<string, unknown>[]
: TField extends SelectFieldClient
? TField['hasMany'] extends true
? string[]
: string
: TField extends PointFieldClient
? { x: number; y: number }
: any
: TCellData
readonly className?: string
readonly collectionConfig: ClientCollectionConfig
readonly columnIndex?: number
readonly customCellProps?: Record<string, any>
readonly field: TField
readonly link?: boolean
readonly onClick?: (args: {
cellData: unknown
collectionSlug: SanitizedCollectionConfig['slug']
rowData: RowData
}) => void
readonly rowData: RowData
}
export type DefaultServerCellComponentProps<
TField extends ClientField = ClientField,
TCellData = any,
> = {
field: Field
i18n: I18nClient
payload: Payload
} & Omit<DefaultCellComponentProps<TField, TCellData>, 'field'>