Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Event type is lost when using as prop in React package #337

Open
kris-ellery opened this issue Apr 4, 2023 · 2 comments · May be fixed by #340
Open

Event type is lost when using as prop in React package #337

kris-ellery opened this issue Apr 4, 2023 · 2 comments · May be fixed by #340
Labels
bug Something isn't working

Comments

@kris-ellery
Copy link

Hi there! Thank you for working on a robust polymorphic component package with forwardRef support. I've been following the recent updates, including addition of react-polymorphed types. I noticed the event type gets lost when using as prop. It works in react-polymorphed, but not here. I was wondering if thats expected or a known issue?

react-polymorphed

import React, { forwardRef } from 'react';
import type { PolyRefFunction } from 'react-polymorphed';

const polyForwardRef = forwardRef as PolyRefFunction;

const PolyComponent = polyForwardRef<'div', {}>((props, ref) => {
    const { as: As = 'div', ...rest } = props;
    return <As ref={ref} {...rest} />;
});

const App = () => {
    return (
        <>
            {/* 🙂 event: React.MouseEvent<HTMLDivElement, MouseEvent> */}
            <PolyComponent onClick={(event) => console.log(event)} />
            {/* 🙂 event: React.MouseEvent<HTMLAnchorElement, MouseEvent> */}
            <PolyComponent as="a" onClick={(event) => console.log(event)} />
        </>
    );
};

@polymorphic-factory/react

import React from 'react';
import { polymorphicFactory } from '@polymorphic-factory/react';

const poly = polymorphicFactory();

const App = () => {
    return (
        <>
            {/* 🙂 event: React.MouseEvent<HTMLDivElement, MouseEvent> */}
            <poly.div onClick={(event) => console.log(event)} />
            {/* 😔 event: any */}
            <poly.div as="a" onClick={(event) => console.log(event)} />
        </>
    );
};
@kris-ellery
Copy link
Author

Same happens when using forwardRef directly.

import React from 'react';
import { forwardRef } from '@polymorphic-factory/react';

const PolyComponent = forwardRef<'div', {}>((props, ref) => {
    const { as: As = 'div', ...rest } = props;
    return <As ref={ref} {...rest} />;
});

const App = () => {
    return (
        <>
            {/* 🙂 event: React.MouseEvent<HTMLDivElement, MouseEvent> */}
            <PolyComponent onClick={(event) => console.log(event)} />
            {/* 😔 event: any */}
            <PolyComponent as="a" onClick={(event) => console.log(event)} />
        </>
    );
};

@kris-ellery kris-ellery linked a pull request Apr 5, 2023 that will close this issue
@TimKolberger TimKolberger added the bug Something isn't working label Apr 6, 2023
@TimKolberger
Copy link
Collaborator

Thanks for catching that and providing a good reproduction 💖

When we remove the ElementType and narrow it to as?: Default, the event typings are correct again ✨ but another error appears when I try to declare a React component like this:

const MyComponent: ComponentWithAs<'div'> = (props) => <poly.div as="a" {...props} />
Types of property 'as' are incompatible.
  Type 'AsComponent | undefined' is not assignable to type '"div" | undefined'.
    Type 'AsComponent' is not assignable to type '"div" | undefined'.
      Type 'ElementType' is not assignable to type '"div" | undefined'.
        Type '"symbol"' is not assignable to type '"div"'.

See your PR Action as reference as well: https://github.com/chakra-ui/polymorphic/actions/runs/4613923890/jobs/8191534864?pr=340#step:4:36

Still investigating how to resolve this. If you have any ideas, kindly let me know :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants