-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[Card] Fix TypeScript not recognizing "component" prop #20179
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
packages/material-ui/src/CardHeader/CardHeader.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
import * as React from 'react'; | ||
import CardHeader from '@material-ui/core/CardHeader'; | ||
|
||
const CustomComponent: React.FC<{ stringProp: string; numberProp: number }> = () => <div />; | ||
|
||
function componentPropTest() { | ||
<CardHeader component="div" />; | ||
<CardHeader component={CustomComponent} stringProp="string" numberProp={1} />; | ||
// $ExpectError | ||
<CardHeader component="incorrectComponent" />; | ||
// $ExpectError | ||
<CardHeader component={CustomComponent} />; | ||
} | ||
|
||
function mixedCardHeaderComponentAndTypographyTest() { | ||
<CardHeader component="div" titleTypographyProps={{ component: 'a', href: 'href' }} />; | ||
<CardHeader component="div" subheaderTypographyProps={{ component: 'a', href: 'href' }} />; | ||
<CardHeader | ||
component={CustomComponent} | ||
stringProp="string" | ||
numberProp={1} | ||
titleTypographyProps={{ component: CustomComponent, stringProp: 'stringProp', numberProp: 2 }} | ||
/>; | ||
<CardHeader | ||
component={CustomComponent} | ||
stringProp="string" | ||
numberProp={1} | ||
titleTypographyProps={{ component: CustomComponent, stringProp: 'stringProp', numberProp: 2 }} | ||
subheaderTypographyProps={{ | ||
component: CustomComponent, | ||
stringProp: 'stringProp', | ||
numberProp: 2, | ||
}} | ||
/>; | ||
// $ExpectError | ||
<CardHeader component="incorrectComponent" />; | ||
// $ExpectError | ||
<CardHeader component={CustomComponent} />; | ||
<CardHeader | ||
component={CustomComponent} | ||
stringProp="string" | ||
numberProp={1} | ||
// $ExpectError | ||
titleTypographyProps={{ component: CustomComponent, stringProp: 'stringProp' }} | ||
subheaderTypographyProps={{ | ||
component: CustomComponent, | ||
stringProp: 'stringProp', | ||
numberProp: 2, | ||
}} | ||
/>; | ||
// $ExpectError | ||
<CardHeader | ||
component={CustomComponent} | ||
stringProp="string" | ||
numberProp={1} | ||
titleTypographyProps={{ component: CustomComponent, stringProp: 'stringProp' }} | ||
subheaderTypographyProps={{ component: CustomComponent, stringProp: 'stringProp' }} | ||
/>; | ||
<CardHeader | ||
// $ExpectError | ||
component="incorrectComponent" | ||
stringProp="string" | ||
numberProp={1} | ||
titleTypographyProps={{ component: CustomComponent, stringProp: 'stringProp', numberProp: 2 }} | ||
subheaderTypographyProps={{ | ||
component: CustomComponent, | ||
stringProp: 'stringProp', | ||
numberProp: 2, | ||
}} | ||
/>; | ||
} | ||
|
||
function titleTypographyPropsTest() { | ||
// $ExpectError | ||
<CardHeader titleTypographyProps={{ component: 'incorrectComponent' }} />; | ||
<CardHeader titleTypographyProps={{ component: 'a', href: 'href' }} />; | ||
<CardHeader | ||
titleTypographyProps={{ component: CustomComponent, stringProp: 'stringProp', numberProp: 2 }} | ||
/>; | ||
<CardHeader titleTypographyProps={{ variant: 'h1' }} />; | ||
<CardHeader titleTypographyProps={{ align: 'left' }} />; | ||
<CardHeader | ||
titleTypographyProps={{ | ||
color: 'primary', | ||
display: 'block', | ||
gutterBottom: true, | ||
noWrap: true, | ||
variantMapping: { h1: 'h1' }, | ||
}} | ||
/>; | ||
// $ExpectError | ||
<CardHeader titleTypographyProps={{ component: CustomComponent, numberProp: 2 }} />; | ||
<CardHeader | ||
titleTypographyProps={{ | ||
component: 'a', | ||
// $ExpectError | ||
propThatDoesntExist: 'shouldNotWork', | ||
}} | ||
/>; | ||
} | ||
|
||
function subheaderTypographyPropsTest() { | ||
<CardHeader subheaderTypographyProps={{ component: 'a', href: 'href' }} />; | ||
<CardHeader | ||
subheaderTypographyProps={{ | ||
component: CustomComponent, | ||
stringProp: 'stringProp', | ||
numberProp: 2, | ||
}} | ||
/>; | ||
<CardHeader subheaderTypographyProps={{ variant: 'h1' }} />; | ||
<CardHeader subheaderTypographyProps={{ align: 'left' }} />; | ||
<CardHeader | ||
subheaderTypographyProps={{ | ||
color: 'primary', | ||
display: 'block', | ||
gutterBottom: true, | ||
noWrap: true, | ||
variantMapping: { h1: 'h1' }, | ||
}} | ||
/>; | ||
<CardHeader | ||
subheaderTypographyProps={{ | ||
component: 'a', | ||
// $ExpectError | ||
propThatDoesntExist: 'shouldNotWork', | ||
}} | ||
/>; | ||
// $ExpectError | ||
<CardHeader subheaderTypographyProps={{ component: 'incorrectComponent' }} />; | ||
// $ExpectError | ||
<CardHeader subheaderTypographyProps={{ component: CustomComponent, numberProp: 2 }} />; | ||
} | ||
|
||
function mixedTypographyPropsTest() { | ||
<CardHeader | ||
titleTypographyProps={{ component: 'a', href: 'href' }} | ||
subheaderTypographyProps={{ component: 'a', href: 'href' }} | ||
/>; | ||
<CardHeader | ||
titleTypographyProps={{ component: CustomComponent, stringProp: 'stringProp', numberProp: 2 }} | ||
subheaderTypographyProps={{ | ||
component: CustomComponent, | ||
stringProp: 'stringProp', | ||
numberProp: 2, | ||
}} | ||
/>; | ||
// $ExpectError | ||
<CardHeader | ||
titleTypographyProps={{ component: 'incorrectComponent' }} | ||
subheaderTypographyProps={{ component: 'incorrectComponent' }} | ||
/>; | ||
<CardHeader | ||
titleTypographyProps={{ | ||
component: 'a', | ||
// $ExpectError | ||
propThatDoesntExist: 'shouldNotWork', | ||
}} | ||
subheaderTypographyProps={{ | ||
component: 'a', | ||
// $ExpectError | ||
propThatDoesntExist: 'shouldNotWork', | ||
}} | ||
/>; | ||
// $ExpectError | ||
<CardHeader | ||
titleTypographyProps={{ component: CustomComponent, numberProp: 2 }} | ||
subheaderTypographyProps={{ component: CustomComponent, numberProp: 2 }} | ||
/>; | ||
<CardHeader | ||
// $ExpectError | ||
titleTypographyProps={{ component: CustomComponent, numberProp: 2 }} | ||
subheaderTypographyProps={{ component: CustomComponent, numberProp: 2, stringProp: 'yada' }} | ||
/>; | ||
<CardHeader | ||
titleTypographyProps={{ component: CustomComponent, numberProp: 2, stringProp: 'yada' }} | ||
// $ExpectError | ||
subheaderTypographyProps={{ component: CustomComponent, numberProp: 2 }} | ||
/>; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eps1lon @fyodore82 need some advice...
CardHeader has an additional level of complexity compared to
ListItemText
in that it's a type map.The component prop on both
titleTypographyProps
&subheaderTypographyProps
now works as expected. However I can't seem to find the way to get these particular set of tests to fail — the tests that look into unknown props on typography object. The nested typography prop objects accept any number of unknown props. The correct typography props work as expected and the optional component prop too but the typing system just let's any other unknown props go through as well.FYI: At the moment, the
test_types
build on the CI is broken for this reason.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have the bandwidth to deep-dive into this issue. Maybe some issue with to many free type parameters or some quirk in JSX.
For now just document that this is undesired behavior in code comments. In addition it might be nice to extend these undesired behavior with tests using
createElement
directly (instead of JSX) and if wrong value types are rejected e.g. { variant: 123213 } or { component: SomeComponentImplementingFooNumber, foo: 'a-string-which-should-be-a-number' }There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
component
prop type like it does in JSX — both for CardHeader and it's nested typography items.{ variant: 123213 }
, etc)