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

[docs] Add TypeScript guide on the polymorphic components #32168

Merged
merged 9 commits into from
May 11, 2022

Conversation

mnajdova
Copy link
Member

@mnajdova mnajdova commented Apr 7, 2022

Related to #31314, #31976

Looks like this applies for the base components too, maybe would be worth adding it there too cc @michaldudak

Here is a codendbox describing the issue and the suggested solution:
https://codesandbox.io/s/amazing-lucy-rs38sz?file=/src/App.tsx

@mui-bot
Copy link

mui-bot commented Apr 7, 2022

No bundle size changes

Generated by 🚫 dangerJS against 517bbd5

@michaldudak
Copy link
Member

michaldudak commented Apr 7, 2022

@mnajdova The codesandbox you linked here has an issue - in lines 9 and 10 a Button is wrapped in styled, not ButtonUnstyled. Correcting it makes it work without errors.

This is likely because ButtonUnstyled does not inherit from OverridableComponent. Because of this, it's not really polymorphic, but it prevents this problem (and hopefully is faster, but I haven't measured it). Any additional props can be placed on a componentsProps.root object.

But in principle, yes, it does apply to generic components. There is a note in Select docs describing the need to cast because of this problem.

@danilo-leal danilo-leal added the docs Improvements or additions to the documentation label Apr 7, 2022
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Apr 8, 2022
@mnajdova
Copy link
Member Author

mnajdova commented Apr 8, 2022

@mnajdova The codesandbox you linked here has an issue - in lines 9 and 10 a Button is wrapped in styled, not ButtonUnstyled. Correcting it makes it work without errors.

This is likely because ButtonUnstyled does not inherit from OverridableComponent. Because of this, it's not really polymorphic, but it prevents this problem (and hopefully is faster, but I haven't measured it). Any additional props can be placed on a componentsProps.root object.

But in principle, yes, it does apply to generic components. There is a note in Select docs describing the need to cast because of this problem.

Right, I've updated the sandboxed. Let's leave it as is for know then and if necessary we can add similar section on one of the guides for the base components.

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Apr 11, 2022
@mnajdova mnajdova marked this pull request as ready for review April 12, 2022 08:32
@mnajdova mnajdova requested a review from samuelsycamore April 15, 2022 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to the documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants