Files
payloadcms/packages/payload/src/utilities/isReactComponent.ts
Alessio Gravili cfeac79b99 feat!: fix non-functional custom RSC component handling, separate label and description props, fix non-functional label function handling (#6264)
Breaking Changes:

- Globals config: `admin.description` no longer accepts a custom component. You will have to move it to `admin.components.elements.Description`
- Collections config: `admin.description` no longer accepts a custom component. You will have to move it to `admin.components.edit.Description`
- All Fields: `field.admin.description` no longer accepts a custom component. You will have to move it to `field.admin.components.Description`
- Collapsible Field: `field.label` no longer accepts a custom component. You will have to move it to `field.admin.components.RowLabel`
- Array Field: `field.admin.components.RowLabel` no longer accepts strings or records
- If you are using our exported field components in your own app, their `labelProps` property has been stripped down and no longer contains the `label` and `required` prop. Those can now only be configured at the top-level
2024-05-09 17:12:01 -04:00

31 lines
1.0 KiB
TypeScript

import type React from 'react'
import { isPlainObject } from './isPlainObject.js'
export function isReactServerComponentOrFunction<T extends any>(
component: React.ComponentType | any,
): component is T {
const isClassComponent =
typeof component === 'function' &&
component.prototype &&
typeof component.prototype.render === 'function'
const isFunctionalComponent =
typeof component === 'function' && (!component.prototype || !component.prototype.render)
return isClassComponent || isFunctionalComponent
}
export function isReactClientComponent<T extends any>(
component: React.ComponentType | any,
): component is T {
// Do this to test for client components (`use client` directive) bc they import as empty objects
return typeof component === 'object' && !isPlainObject(component)
}
export function isReactComponentOrFunction<T extends any>(
component: React.ComponentType | any,
): component is T {
return isReactServerComponentOrFunction(component) || isReactClientComponent(component)
}