Skip to content
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

Better types for withStyles WIP - no merge yet - #12106

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions packages/material-ui/src/styles/withStyles.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { WithTheme } from '../styles/withTheme';
import { ConsistentWith, Overwrite } from '..';
import { ConsistentWith, Overwrite, Omit } from '..';
import { Theme } from './createMuiTheme';
import * as CSS from 'csstype';
import * as JSS from 'jss';
Expand Down Expand Up @@ -48,16 +48,30 @@ export type WithStyles<T extends string | StyleRules | StyleRulesCallback = stri
>;
};

// Theme is mandatory
export type WithStylesAndTheme<T extends string | StyleRules | StyleRulesCallback = string> = WithStyles<T> & WithTheme;

export interface StyledComponentProps<ClassKey extends string = string> {
classes?: Partial<ClassNameMap<ClassKey>>;
innerRef?: React.Ref<any> | React.RefObject<any>;
}

export default function withStyles<ClassKey extends string>(
declare function withStyles<ClassKey extends string>(
style: StyleRulesCallback<ClassKey> | StyleRules<ClassKey>,
options?: WithStylesOptions<ClassKey>,
options: WithStylesOptions<ClassKey> & { withTheme: true },
): {
<P extends ConsistentWith<P, StyledComponentProps<ClassKey>>>(
component: React.ComponentType<P & WithStyles<ClassKey> & WithTheme>,
): React.ComponentType<Omit<Overwrite<P, StyledComponentProps<ClassKey>>, "theme" | "classes">>;
};

declare function withStyles<ClassKey extends string>(
style: StyleRulesCallback<ClassKey> | StyleRules<ClassKey>,
options?: WithStylesOptions<ClassKey> & { withTheme?: false },
): {
<P extends ConsistentWith<P, StyledComponentProps<ClassKey>>>(
component: React.ComponentType<P & WithStyles<ClassKey>>,
): React.ComponentType<Overwrite<P, StyledComponentProps<ClassKey>>>;
): React.ComponentType<Omit<Overwrite<P, StyledComponentProps<ClassKey>>, "classes">>;
};

export default withStyles;
47 changes: 47 additions & 0 deletions packages/material-ui/src/styles/withStylesPlayground.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as React from 'react';

import withStyles, { WithStylesAndTheme } from './withStyles';
import { Theme } from '.';
import createStyles from './createStyles';

const baseStyles = createStyles({
root: {
fontSize: 5,
},
title: {
borderStyle: 'solid',
},
});

interface IProps extends WithStylesAndTheme<typeof baseStyles> {
title: string;
}

function FooWithTheme({ theme, classes, title }: IProps) {
return (
<div className={classes.title} title={`theme is here!` + theme.direction}>
{title}
</div>
);
}

// theme and classes are missing here
[<FooWithTheme title={'bla'} />];

const Foo = withStyles(baseStyles, { withTheme: true })(FooWithTheme);

// theme and classes are NOT missing here.
[<Foo title={'bla'} />];


// theme is NOT available, as withTheme is not true
withStyles(baseStyles, { withTheme: false })((props) => <div>{props.theme.direction}</div>);

// theme IS available, as withTheme is true
withStyles(baseStyles, { withTheme: true })((props) => <div>{props.theme.direction}</div>);

const Foo2 = withStyles(baseStyles, { withTheme: false })(FooWithTheme);
const Foo3 = withStyles(baseStyles)(FooWithTheme);

// theme is mandatory! due to withTheme: false / missing
[<Foo2 title={"title"}/>, <Foo3 title="title" />]