-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
refactor(typescript): convert Grid, FlexGrid and children to TypeScript #13074
Conversation
✅ Deploy Preview for carbon-components-react ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for carbon-elements ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
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.
Thanks for putting this together @lewandom!
In this case we're not looking to remove CSSGridColumn
's ability to accept the span
prop, we'd want to allow the Column
component to receive everything the CSSGridColumn
might need so that it's able to pass it on instead.
And we do want to bring back in the spanClassName
class and getClassNameForSpan
function into this PR 🙏🏻
Other than that LGTM!
@francinelucca thanks for reviewing! Just wanted to double-confirm: the |
@lewandom not deprecated. I think if TS can let us get away with not specifically adding it to the proptypes but still allow passing it through Unless @tay1orjones you think we should explicitly add it anyway? |
56a6a46
to
799e983
Compare
@francinelucca the |
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.
This looks good to me. Just one comment/question about if we could consolidate the RenderAsProps
with the existing helper in common.d.ts
export type RenderAsProps<BaseProps, T> = | ||
| RenderAsDefaultProps<BaseProps> | ||
| (T extends keyof JSX.IntrinsicElements | ||
? RenderAsIntrinsicProps<BaseProps, T> | ||
: RenderAsCustomComponentProps<BaseProps, T>); |
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.
This is a dense file that would benefit from extensive commenting to explain the structure and purpose of each type.
Also, a helper already exists for as
props:
carbon/packages/react/src/types/common.d.ts
Lines 13 to 20 in fa262d5
/** | |
* For "as" props. Creates an "as" property that supports native html tags such as 'span', 'a', 'button' as well as custom functional components | |
* All native props for the supplied html tag/component are inferred as part of the base component props, allowing us to use props like `href` on an 'a' element ect | |
*/ | |
export type PolymorphicProps<Element extends React.ElementType, Props> = Props & | |
Omit<React.ComponentProps<Element>, 'as'> & { | |
as?: Element; | |
}; |
Should that be extended with this? Consider lifting this up out of the Grid component file and into that common file. The as
prop pattern is used across 21 different components. Searching/find all as: PropTypes
in the codebase you can see the different ways it's used. Having a cohesive approach for them would be great.
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.
@tay1orjones thanks for pointing me at the PolymorphicProps type, I wasn't aware of it's existence. I started with the DefinitelyTyped types and wasn't aware we have such a type already available. In fact, I was also not satisfied what DefinitelyTyped provided and was searching for a method to use ComponentProps type, but couldn't find one. PolymorphicProps are clean and elegant.
I've removed RenderAsProps entirely as it was not necessary anymore and rewritten all components to use PolymorphicProps. Please review once more.
41ce6a4
to
a9ab077
Compare
…pt (carbon-design-system#13074) * refactor(typescript): convert Grid, FlexGrid and children to TypeScript * fix(Grid): restore span attribute support * fix(typescript): use PolymorphicProps
Closes #12537.
Convert React Grid, FlexGrid and related components (Column, ColumnHang, Row) to .tsx to add TypeScript support.
Changelog
New
Changed
Testing / Reviewing
The component imports for the storybook should be valid and rendering. In a TypeScript environment, you should be able to import and reference all public components (Grid, FlexGrid, Column, ColumnHang, Row) and their props typings. The use of
as
attribute should allow for specifying both intrinsic and custom components as a replacement to the default base component (div
). When used in TypeScript setup, type validation / hints for attributes coming from custom base component should work.