polymorphic-component.tsx 513 B

12345678910111213141516171819
  1. import { MergeAndOverride } from '../../../../types/utils'
  2. type PolymorphicComponentOwnProps<E extends React.ElementType> = {
  3. as?: E
  4. }
  5. export type PolymorphicComponentProps<E extends React.ElementType> =
  6. MergeAndOverride<React.ComponentProps<E>, PolymorphicComponentOwnProps<E>>
  7. function PolymorphicComponent<E extends React.ElementType = 'div'>({
  8. as,
  9. ...props
  10. }: PolymorphicComponentProps<E>) {
  11. const Component = as || 'div'
  12. return <Component {...props} />
  13. }
  14. export default PolymorphicComponent