Skip to content
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

Improve the AsType to support inheriting props from a passed component #1334

Open
brettdorrans opened this issue Apr 13, 2023 · 1 comment
Open
Labels
enhancement New feature or request

Comments

@brettdorrans
Copy link
Member

import React, { ComponentType, HTMLAttributes } from 'react';

type AsProp<C extends keyof JSX.IntrinsicElements | ComponentType<any>> = {
  as?: C;
} & (C extends keyof JSX.IntrinsicElements
  ? JSX.IntrinsicElements[C]
  : C extends ComponentType<infer P>
  ? P & HTMLAttributes<HTMLElement>
  : never);

type ComponentProps<C extends keyof JSX.IntrinsicElements | ComponentType<any>> = AsProp<C>;

function Component<C extends keyof JSX.IntrinsicElements | ComponentType<any>>({
  as: Component = 'div' as C,
  ...props
}: ComponentProps<C>) {
  return <Component {...props} />;
}

export default Component;

In this implementation, the ComponentProps type defines a generic type C that can be either a string representing a valid HTML element or a React component type. The as prop is defined as optional, and its type is set to C.

The AsProp type is used to merge the as prop type with the props of the component specified in as. If C is a string representing an HTML element, the type of as prop is merged with the HTML attributes of that element. If C is a React component, the type of as prop is merged with the props of that component.

The Component function takes in the as prop and spreads the rest of the props onto the component specified in as. If no as prop is specified, it defaults to the div element. The Component function returns the specified component with the props passed in.

You can use the component like this:

import React from 'react';
import Component from './Component';

function AnotherComponent({ text }: { text: string }) {
  return <h1>{text}</h1>;
}

function App() {
  return (
    <div>
      <Component as="span">This is a span</Component>
      <Component as={AnotherComponent} text="This is AnotherComponent" />
    </div>
  );
}

export default App;

In the example above, we're rendering two instances of Component, one with an as prop of "span", and another with an as prop of AnotherComponent. In the second case, we're passing in the text prop to AnotherComponent.

@brettdorrans brettdorrans added the enhancement New feature or request label Apr 13, 2023
@brettdorrans
Copy link
Member Author

@brettdorrans brettdorrans linked a pull request Apr 15, 2023 that will close this issue
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant