Skip to content

Commit 5e98ee2

Browse files
authored
[typescript] Update typings for beta.4 and beta.5 (#7793)
- Update `<Tabs>` API to match BC introduced in #7741 - Update `withStyle` API to match BC introduced in #7740 - Remove discriminated unions (microsoft/TypeScript#17882)
1 parent 1d0c88b commit 5e98ee2

File tree

5 files changed

+99
-109
lines changed

5 files changed

+99
-109
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"warning": "^3.0.0"
7878
},
7979
"devDependencies": {
80-
"@types/enzyme": "^2.8.5",
80+
"@types/enzyme": "^2.8.6",
8181
"@types/react": "^16.0.2",
8282
"app-module-path": "^2.2.0",
8383
"argos-cli": "^0.0.9",

test/typescript/components.spec.tsx

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import Table, {
4848
TableHead,
4949
TableRow,
5050
} from 'material-ui/Table';
51-
import { withStyles, createStyleSheet } from 'material-ui/styles';
51+
import { withStyles, StyleRulesCallback } from 'material-ui/styles';
5252

5353
const log = console.log;
5454
const FakeIcon = () => <div>ICON</div>;
@@ -308,6 +308,19 @@ const DrawerTest = () => {
308308
);
309309
};
310310

311+
const DockedDrawerTest = () =>
312+
class DockedDrawer extends React.Component<{}, { docked: boolean }> {
313+
state = { docked: true };
314+
render() {
315+
const docked: true | false = this.state.docked;
316+
return (
317+
<Drawer anchor="bottom" open={true} docked={docked}>
318+
List
319+
</Drawer>
320+
);
321+
}
322+
};
323+
311324
const GridListTest = () =>
312325
<GridList cellHeight={160} cols={3}>
313326
<GridListTest cols={1}>
@@ -595,13 +608,13 @@ const StepperTest = () =>
595608
};
596609

597610
const TableTest = () => {
598-
const styleSheet = createStyleSheet(theme => ({
611+
const styles: StyleRulesCallback = theme => ({
599612
paper: {
600613
width: '100%',
601614
marginTop: theme.spacing.unit * 3,
602615
overflowX: 'auto',
603616
},
604-
}));
617+
});
605618

606619
let id = 0;
607620
function createData(name, calories, fat, carbs, protein) {
@@ -660,7 +673,7 @@ const TableTest = () => {
660673
);
661674
}
662675

663-
return withStyles(styleSheet)(BasicTable);
676+
return withStyles(styles)(BasicTable);
664677
};
665678

666679
const TabsTest = () => {
@@ -669,21 +682,21 @@ const TabsTest = () => {
669682
{props.children}
670683
</div>;
671684

672-
const styleSheet = createStyleSheet(theme => ({
685+
const styles = theme => ({
673686
root: {
674687
flexGrow: 1,
675688
marginTop: theme.spacing.unit * 3,
676689
backgroundColor: theme.palette.background.paper,
677690
},
678-
}));
691+
});
679692

680693
class BasicTabs extends React.Component<{ classes: { root: string } }> {
681694
state = {
682-
index: 0,
695+
value: 0,
683696
};
684697

685-
handleChange = (event, index) => {
686-
this.setState({ index });
698+
handleChange = (event, value) => {
699+
this.setState({ value });
687700
};
688701

689702
render() {
@@ -692,21 +705,21 @@ const TabsTest = () => {
692705
return (
693706
<div className={classes.root}>
694707
<AppBar position="static">
695-
<Tabs index={this.state.index} onChange={this.handleChange}>
708+
<Tabs value={this.state.value} onChange={this.handleChange}>
696709
<Tab label="Item One" />
697710
<Tab label="Item Two" />
698711
<Tab label="Item Three" />
699712
</Tabs>
700713
</AppBar>
701-
{this.state.index === 0 &&
714+
{this.state.value === 0 &&
702715
<TabContainer>
703716
{'Item One'}
704717
</TabContainer>}
705-
{this.state.index === 1 &&
718+
{this.state.value === 1 &&
706719
<TabContainer>
707720
{'Item Two'}
708721
</TabContainer>}
709-
{this.state.index === 2 &&
722+
{this.state.value === 2 &&
710723
<TabContainer>
711724
{'Item Three'}
712725
</TabContainer>}
@@ -715,7 +728,7 @@ const TabsTest = () => {
715728
}
716729
}
717730

718-
return withStyles(styleSheet)(BasicTabs);
731+
return withStyles(styles)(BasicTabs);
719732
};
720733

721734
const TextFieldTest = () =>

test/typescript/styles.spec.tsx

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import * as React from 'react';
2-
import { createStyleSheet, withStyles } from 'material-ui/styles';
2+
import { withStyles, StyleRules } from 'material-ui/styles';
33
import { Theme } from 'material-ui/styles/theme';
44

5-
6-
7-
const stylesheet = createStyleSheet(({ palette, spacing }: Theme) => ({
5+
const styles = ({ palette, spacing }) => ({
86
root: {
97
padding: spacing.unit,
108
background: palette.background,
11-
color: palette.primary
12-
}
13-
}));
14-
9+
color: palette.primary,
10+
},
11+
});
1512

1613
interface StyledComponentClassNames {
1714
root: string;
@@ -21,10 +18,28 @@ interface StyledComponentProps {
2118
text: string;
2219
}
2320

24-
const Component: React.SFC<StyledComponentProps & { classes: StyledComponentClassNames }> =
25-
({ classes, text }) =>
26-
<div className={classes.root}>{text}</div>
21+
const Component: React.SFC<
22+
StyledComponentProps & { classes: StyledComponentClassNames }
23+
> = ({ classes, text }) =>
24+
<div className={classes.root}>
25+
{text}
26+
</div>;
27+
28+
const StyledComponent = withStyles<
29+
StyledComponentProps,
30+
StyledComponentClassNames
31+
>(styles)(Component);
2732

28-
const StyledComponent = withStyles<StyledComponentProps, StyledComponentClassNames>(stylesheet)(Component);
33+
<StyledComponent text="I am styled!" />;
34+
35+
// Also works with a plain object
36+
37+
const stylesAsPojo: StyleRules = {
38+
root: {
39+
background: 'hotpink',
40+
},
41+
};
2942

30-
<StyledComponent text="I am styled!"/>
43+
const AnotherStyledComponent = withStyles<{}, StyledComponentClassNames>({
44+
root: { background: 'hotpink' },
45+
})(({ classes }) => <div className={classes.root}>Stylish!</div>);

typings/index.d.ts

Lines changed: 39 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -476,27 +476,16 @@ declare module 'material-ui/Drawer/Drawer' {
476476
import { SlideProps } from 'material-ui/transitions/Slide';
477477
import { Theme } from 'material-ui/styles/theme';
478478

479-
type DrawerCommonProps = {
479+
export interface DrawerProps extends ModalProps {
480480
anchor?: 'left' | 'top' | 'right' | 'bottom';
481+
docked?: boolean;
481482
elevation?: number;
482483
enterTransitionDuration?: number;
483484
leaveTransitionDuration?: number;
484485
open?: boolean;
485486
SlideProps?: SlideProps;
486487
theme?: Theme;
487-
};
488-
489-
type DrawerDockedProps = {
490-
docked?: true;
491-
} & DrawerCommonProps &
492-
React.HtmlHTMLAttributes<HTMLDivElement>;
493-
494-
type DrawerModalProps = {
495-
docked?: false;
496-
onRequestClose?: React.EventHandler<any>;
497-
} & ModalProps;
498-
499-
export type DrawerProps = DrawerDockedProps | DrawerModalProps;
488+
}
500489

501490
export default class Drawer extends MaterialUI.Component<DrawerProps> {}
502491
}
@@ -872,24 +861,15 @@ declare module 'material-ui/List/List' {
872861
declare module 'material-ui/List/ListItem' {
873862
import { ButtonBaseProps } from 'material-ui/internal/ButtonBase';
874863

875-
interface ListItemCommonProps extends React.LiHTMLAttributes<HTMLLIElement> {
864+
export type ListItemProps = {
865+
button?: boolean;
876866
component?: React.ReactNode;
877867
dense?: boolean;
878868
disabled?: boolean;
879869
disableGutters?: boolean;
880870
divider?: boolean;
881-
}
882-
883-
export type ListItemDefaultProps = {
884-
button?: false;
885-
} & ListItemCommonProps;
886-
887-
export type ListItemButtonProps = {
888-
button?: true;
889-
} & ListItemCommonProps &
890-
ButtonBaseProps;
891-
892-
export type ListItemProps = ListItemDefaultProps | ListItemButtonProps;
871+
} & ButtonBaseProps &
872+
React.LiHTMLAttributes<HTMLLIElement>;
893873

894874
export default class ListItem extends MaterialUI.Component<ListItemProps> {}
895875
}
@@ -971,9 +951,9 @@ declare module 'material-ui/Menu/Menu' {
971951
}
972952

973953
declare module 'material-ui/Menu/MenuItem' {
974-
import { ListItemButtonProps } from 'material-ui/List/ListItem';
954+
import { ListItemProps } from 'material-ui/List/ListItem';
975955

976-
export interface MenuItemProps extends ListItemButtonProps {
956+
export interface MenuItemProps extends ListItemProps {
977957
component?: React.ReactNode;
978958
role?: string;
979959
selected?: boolean;
@@ -1301,11 +1281,11 @@ declare module 'material-ui/Tabs/Tab' {
13011281
disabled?: boolean;
13021282
fullWidth?: boolean;
13031283
icon?: React.ReactNode;
1304-
index?: number;
1284+
value?: any;
13051285
label?: React.ReactNode;
13061286
onChange?: (
13071287
event: React.ChangeEvent<{ checked: boolean }>,
1308-
index: number
1288+
value: any
13091289
) => void;
13101290
onClick?: React.EventHandler<any>;
13111291
selected?: boolean;
@@ -1349,10 +1329,10 @@ declare module 'material-ui/Tabs/Tabs' {
13491329
centered?: boolean;
13501330
children?: React.ReactNode;
13511331
fullWidth?: boolean;
1352-
index: false | number;
1332+
value: any;
13531333
indicatorClassName?: string;
13541334
indicatorColor?: 'accent' | 'primary' | string;
1355-
onChange: (event: React.ChangeEvent<{}>, index: number) => void;
1335+
onChange: (event: React.ChangeEvent<{}>, value: any) => void;
13561336
scrollable?: boolean;
13571337
scrollButtons?: 'auto' | 'on' | 'off';
13581338
textColor?: 'accent' | 'primary' | 'inherit' | string;
@@ -1621,8 +1601,6 @@ declare module 'material-ui/internal/Portal' {
16211601
}
16221602

16231603
declare module 'material-ui/internal/SwitchBase' {
1624-
import { StyleSheet } from 'material-ui/styles/createStyleSheet';
1625-
16261604
export interface SwitchBaseProps {
16271605
checked?: boolean | string;
16281606
checkedClassName?: string;
@@ -1648,7 +1626,6 @@ declare module 'material-ui/internal/SwitchBase' {
16481626
defaultIcon?: React.ReactNode;
16491627
defaultCheckedIcon?: React.ReactNode;
16501628
inputType?: string;
1651-
styleSheet?: StyleSheet;
16521629
}
16531630

16541631
export default function createSwitch(
@@ -1699,12 +1676,14 @@ declare module 'material-ui/styles' {
16991676
export { default as createBreakpoints } from 'material-ui/styles/breakpoints';
17001677
export { default as createMuiTheme } from 'material-ui/styles/theme';
17011678
export { default as createPalette } from 'material-ui/styles/palette';
1702-
export {
1703-
default as createStyleSheet,
1704-
} from 'material-ui/styles/createStyleSheet';
17051679
export { default as createTypography } from 'material-ui/styles/typography';
17061680
export { default as withStyles } from 'material-ui/styles/withStyles';
17071681
export { default as withTheme } from 'material-ui/styles/withTheme';
1682+
1683+
export {
1684+
StyleRules,
1685+
StyleRulesCallback,
1686+
} from 'material-ui/styles/withStyles';
17081687
}
17091688

17101689
declare module 'material-ui/styles/MuiThemeProvider' {
@@ -1781,34 +1760,6 @@ declare module 'material-ui/styles/createGenerateClassName' {
17811760
) => string;
17821761
}
17831762

1784-
declare module 'material-ui/styles/createStyleSheet' {
1785-
import { Theme } from 'material-ui/styles/theme';
1786-
1787-
export interface StyleRules {
1788-
[displayName: string]: Partial<React.CSSProperties>;
1789-
}
1790-
1791-
export interface StyleRulesCallback<Theme> {
1792-
(theme: Theme): StyleRules;
1793-
}
1794-
1795-
export interface StyleSheet {
1796-
name: string | false;
1797-
createStyles<T extends Theme = Theme>(theme: T): StyleRules;
1798-
options: Object;
1799-
themingEnabled: boolean;
1800-
}
1801-
1802-
export default function createStyleSheet<T extends Theme = Theme>(
1803-
callback: StyleRulesCallback<Theme> | StyleRules
1804-
): StyleSheet;
1805-
export default function createStyleSheet<T extends Theme = Theme>(
1806-
name: string,
1807-
callback: StyleRulesCallback<Theme> | StyleRules,
1808-
options?: Object
1809-
): StyleSheet;
1810-
}
1811-
18121763
declare module 'material-ui/styles/mixins' {
18131764
import { Spacing } from 'material-ui/styles/spacing';
18141765
import { Breakpoints } from 'material-ui/styles/breakpoints';
@@ -2050,11 +2001,28 @@ declare module 'material-ui/styles/typography' {
20502001

20512002
declare module 'material-ui/styles/withStyles' {
20522003
import { Theme } from 'material-ui/styles/theme';
2053-
import { StyleSheet } from 'material-ui/styles/createStyleSheet';
2004+
2005+
/**
2006+
* This is basically the API of JSS. It defines a Map<string, CSS>,
2007+
* where
2008+
*
2009+
* - the `keys` are the class (names) that will be created
2010+
* - the `values` are objects that represent CSS rules (`React.CSSProperties`).
2011+
*/
2012+
export interface StyleRules {
2013+
[displayName: string]: Partial<React.CSSProperties>;
2014+
}
2015+
2016+
export type StyleRulesCallback = (theme: Theme) => StyleRules;
2017+
2018+
export interface WithStylesOptions {
2019+
withTheme?: boolean;
2020+
name?: string;
2021+
}
20542022

20552023
const withStyles: <P = {}, ClassNames = {}>(
2056-
stylesheets: StyleSheet | StyleSheet[],
2057-
options?: Partial<{ withTheme: boolean }>
2024+
style: StyleRules | StyleRulesCallback,
2025+
options?: WithStylesOptions
20582026
) => (
20592027
component: React.ComponentType<P & { classes: ClassNames }>
20602028
) => React.ComponentClass<P>;
@@ -2194,10 +2162,8 @@ declare module 'material-ui/test-utils/createShallow' {
21942162
}
21952163

21962164
declare module 'material-ui/test-utils/getClasses' {
2197-
import { StyleSheet } from 'material-ui/styles/createStyleSheet';
2198-
21992165
export default function getClasses<T = { [name: string]: string }>(
2200-
stylesheets: StyleSheet | StyleSheet[],
2166+
element: React.ReactElement<any>,
22012167
options?: Partial<{ withTheme: boolean }>
22022168
): T;
22032169
}

0 commit comments

Comments
 (0)