Skip to content

Commit

Permalink
Added TypeScript definitions for innerRef.
Browse files Browse the repository at this point in the history
Closes #424.
  • Loading branch information
cameron-martin committed Oct 22, 2017
1 parent 435eb2f commit 7185e5d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
16 changes: 16 additions & 0 deletions packages/react-emotion/typescript_tests/typescript_tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,19 @@ mount = <Button type="submit" bgColor="red" />;
*/

flush();

/**
* innerRef
*/

Component = styled('div')``;
mount = <Component innerRef={(element: HTMLDivElement) => {}} />;

Component = styled.div``;
mount = <Component innerRef={(element: HTMLDivElement) => {}} />;

Component = styled.div({});
mount = <Component innerRef={(element: HTMLDivElement) => {}} />;

Component = Component.withComponent('input');
mount = <Component innerRef={(element: HTMLInputElement) => {}} />;
12 changes: 8 additions & 4 deletions packages/react-emotion/typings/react-emotion.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ export type ThemedProps<Props, Theme> = Props & {
theme: Theme,
}

type ElementProps<Tag extends keyof JSX.IntrinsicElements> =
& JSX.IntrinsicElements[Tag]
& { innerRef?: JSX.IntrinsicElements[Tag]['ref'] }

export interface StyledComponent<Props, Theme, IntrinsicProps>
extends
ComponentClass<Props & IntrinsicProps>,
StatelessComponent<Props & IntrinsicProps>
{
withComponent<Tag extends keyof JSX.IntrinsicElements>(tag: Tag):
StyledComponent<Props, Theme, JSX.IntrinsicElements[Tag]>
StyledComponent<Props, Theme, ElementProps<Tag>>

withComponent(component: Component<Props>):
StyledComponent<Props, Theme, {}>
Expand Down Expand Up @@ -74,15 +78,15 @@ type ShorthandsFactories<Theme> = {
<Props = {}>(
strings: TemplateStringsArray,
...vars: Interpolation<ThemedProps<Props & JSX.IntrinsicElements[Tag], Theme>>[],
): StyledComponent<Props, Theme, JSX.IntrinsicElements[Tag]>
): StyledComponent<Props, Theme, ElementProps<Tag>>

// overload for object as styles
<Props = {}>(
...styles: (
| ObjectStyleAttributes
| ((props: ThemedProps<Props & JSX.IntrinsicElements[Tag], Theme>) => ObjectStyleAttributes)
)[]
): StyledComponent<Props, Theme, JSX.IntrinsicElements[Tag]>
): StyledComponent<Props, Theme, ElementProps<Tag>>
};
};

Expand All @@ -91,7 +95,7 @@ export interface ThemedReactEmotionInterface<Theme> extends ShorthandsFactories<
<Props, Tag extends keyof JSX.IntrinsicElements>(
tag: Tag,
options?: Options,
): CreateStyled<Props, Theme, JSX.IntrinsicElements[Tag]>
): CreateStyled<Props, Theme, ElementProps<Tag>>

// overload for component
<Props>(
Expand Down

0 comments on commit 7185e5d

Please sign in to comment.