-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Add withTheme definition #521
Conversation
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.
Thank you, I also recently found that typings did not cover all the API.
In addition to inline comments, there's a definition of the styled-component
module (with theme type parameter), where need to add this method also.
typings/styled-components.d.ts
Outdated
|
||
type Component<P> = ComponentClass<P> | StatelessComponent<P>; | ||
type Component<P> = ComponentClass<P> | StatelessComponent<P> | PureComponent<P, any>; |
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 think PureComponent
should be here. PureComponent
is an instance, but here must be a constructor.
What the use case required this?
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 saw how withRouter was implemented in React Router, they were also using PureComponent. I'll remove this :)
typings/styled-components.d.ts
Outdated
@@ -199,6 +199,8 @@ export interface ThemedStyledComponentsModule<T> { | |||
declare const styled: StyledInterface; | |||
|
|||
export const css: ThemedCssFunction<any>; | |||
export function withTheme<C extends Component<any>>(component: C): C; |
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.
- it does not reflect that the resulting component does not have
theme
prop - component parameter must have a
theme
prop - has no strong type theming support (
theme
prop must be of the same type as theme) - we do not need to capture component type and return it, we only need to capture props type and always return
ComponentClass
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.
- what's the point for this? you need to specify the theme prop anyway, like this:
class MyComponent extends React.Component<ThemeProps<{}>, {}> {
render() {
const { theme } = this.props;
console.log("Current theme: ", theme);
return <h1>Hello</h1>;
}
}
export default withTheme(MyComponent);
- yeah, that would be helpful, will change :)
- same as 1
- will change
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'd suggest something like this here:
export function withTheme<P extends { theme?: T; }, T>(component: Component<P>): ComponentClass<rest(P, 'theme')>;
where rest(P, 'theme')
is a new type operator that is going to be landed with TS smth like 2.3, unfortunately (microsoft/TypeScript#13470).
I do not have any other ideas how to express removing a property from a generic type. Do you have any?
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.
Why would you need to remove the property?
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.
An inner class is required to have theme
prop and an implementer may want to have it required:
interface MyComponentProps {
theme: MyTheme; // I want have this required
text: string;
}
class MyComponent extends React.Component<MyComponentProps, {}> {
...
}
<MyComponentProps theme={theme} text={text} /> // ok
<MyComponentProps text={text} /> // error, I can't use without theme
so after applying the withTheme
, the component will lose theme
prop:
interface MyThemedComponentProps {
text: string;
}
const MyThemedComponent: ComponentClass<MyThemedComponentProps> = withTheme(MyComponent);
<MyThemedComponent text={text} /> // now that is ok
<MyThemedComponent theme={theme} text={text} /> // and now that is an error, because there's no theme prop anymore
How it going, @patrick91 ? Do you need any help? |
I'm pretty busy these days! I'll try to find some time soon :) |
@Igorbek I don't see any way to remove the theme prop any than using |
I can't mark the theme prop as required, it will complain when using the component. |
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.
yes, I've been thinking about this and haven't come up with something useful as well.
We'll be waiting for better HOC support in typescript
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.
LGTM, let's shipit?
Yup!
On Wed, 1 Mar 2017 at 07:38, Max Stoiber ***@***.***> wrote:
***@***.**** approved this pull request.
LGTM, let's shipit?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#521 (review)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAotlaZH5qPGmH6s-rrK5-UipKRTtPJMks5rhSB9gaJpZM4MGM5u>
.
--
Patrick
|
See #483