Skip to content

Commit

Permalink
Merge pull request styled-components#521 from patrick91/fix/ts-with-t…
Browse files Browse the repository at this point in the history
…heme

Add withTheme definition
  • Loading branch information
mxstbr authored Mar 1, 2017
2 parents 7ee58b5 + 0d3f28b commit c5bb046
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 5 deletions.
3 changes: 2 additions & 1 deletion native/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export {
OuterStyledProps,
StyledFunction,
BaseStyledInterface,
css
css,
withTheme,
} from "..";

import { StyledFunction, BaseStyledInterface } from "..";
Expand Down
17 changes: 17 additions & 0 deletions typings/styled-components-hoc-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as React from "react";

import styled from "..";
import { css, keyframes, ThemeProvider, injectGlobal, withTheme, ThemeProps } from "..";


class MyComponent extends React.Component<ThemeProps<{}>, {}> {
render() {
const { theme } = this.props;

console.log("Current theme: ", theme);

return <h1>Hello</h1>;
}
}

export default withTheme(MyComponent);
8 changes: 7 additions & 1 deletion typings/styled-components-test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from "react";

import styled from "..";
import { css, keyframes, ThemeProvider, injectGlobal } from "..";
import { css, keyframes, ThemeProvider, injectGlobal, withTheme } from "..";

// Create a <Title> react component that renders an <h1> which is
// centered, palevioletred and sized at 1.5em
Expand Down Expand Up @@ -149,3 +149,9 @@ injectGlobal`
${cssWithFunc1}
`;
*/

const name = "hey";

const ThemedButton = withTheme(MyButton);

<ThemedButton name={name} />;
5 changes: 4 additions & 1 deletion typings/styled-components.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import { StatelessComponent, ComponentClass } from "react";
import { StatelessComponent, ComponentClass, PureComponent } from "react";

type Component<P> = ComponentClass<P> | StatelessComponent<P>;

Expand Down Expand Up @@ -192,13 +192,16 @@ export interface ThemedStyledComponentsModule<T> {
css: ThemedCssFunction<T>;
keyframes(strings: TemplateStringsArray, ...interpolations: SimpleInterpolation[]): string;
injectGlobal(strings: TemplateStringsArray, ...interpolations: SimpleInterpolation[]): void;
withTheme<P extends { theme?: T; }, T>(component: Component<P>): ComponentClass<P>;

ThemeProvider: ThemeProviderComponent<T>;
}

declare const styled: StyledInterface;

export const css: ThemedCssFunction<any>;
export function withTheme<P extends { theme?: T; }, T>(component: Component<P>): ComponentClass<P>;

export function keyframes(strings: TemplateStringsArray, ...interpolations: SimpleInterpolation[]): string;
export function injectGlobal(strings: TemplateStringsArray, ...interpolations: SimpleInterpolation[]): void;

Expand Down
9 changes: 7 additions & 2 deletions typings/themed-tests/mytheme-styled-components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ const {
css,
injectGlobal,
keyframes,
ThemeProvider
withTheme,
ThemeProvider,
} = styledComponents as ThemedStyledComponentsModule<MyTheme>;

interface ThemeProps {
theme?: MyTheme;
}

export default styled;
export { css, injectGlobal, keyframes, ThemeProvider };
export { css, injectGlobal, keyframes, withTheme, ThemeProvider, ThemeProps };
25 changes: 25 additions & 0 deletions typings/themed-tests/with-theme-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as React from "react";

import styled, { css, keyframes, ThemeProvider, injectGlobal, withTheme, MyTheme, ThemeProps } from "./mytheme-styled-components";

interface MyComponentProps extends ThemeProps {
text: string;
}

class MyComponent extends React.Component<MyComponentProps, {}> {
render() {
return <h1>{this.props.theme}</h1>;
}
}

const theme: MyTheme = {
primaryColor: "red",
backgroundColor: "blue",
defaultMargin: 10,
};

const text = "hey";

const MyThemedComponent = withTheme(MyComponent);

<MyThemedComponent text={text} />;

0 comments on commit c5bb046

Please sign in to comment.