Skip to content

Commit 9432af2

Browse files
authored
Merge pull request #5 from treetips/feature/support_makestyles
Migrate withStyles(HOC) to StylesHook without component-based classes
2 parents 78ce64a + 675f7b5 commit 9432af2

File tree

7 files changed

+38
-112
lines changed

7 files changed

+38
-112
lines changed

components/libs/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

components/libs/with-redux-store.tsx

Lines changed: 0 additions & 50 deletions
This file was deleted.

components/molecules/NextListItem.tsx

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@ import {
44
ListItemAvatar,
55
ListItemText,
66
} from "@material-ui/core"
7-
import {
8-
createStyles,
9-
Theme,
10-
withStyles,
11-
WithStyles,
12-
} from "@material-ui/core/styles"
7+
import { createStyles, makeStyles, Theme } from "@material-ui/core/styles"
138
import Link from "next/link"
149
import React from "react"
1510

16-
const styles = (theme: Theme) =>
11+
const useStyles = makeStyles((theme: Theme) =>
1712
createStyles({
1813
root: {},
1914
anchorLink: {
@@ -29,8 +24,9 @@ const styles = (theme: Theme) =>
2924
color: theme.palette.primary.contrastText,
3025
},
3126
})
27+
)
3228

33-
interface IProps extends WithStyles<typeof styles> {
29+
interface IProps {
3430
/**
3531
* <Link href="/">
3632
*/
@@ -61,8 +57,9 @@ interface IProps extends WithStyles<typeof styles> {
6157
* Next.js optimized <ListItem>
6258
* @param props IProps
6359
*/
64-
const NextListItemComponent = (props: IProps) => {
65-
const { classes, className, href, icon, primary, secondary, onClick } = props
60+
export const NextListItem = function(props: IProps) {
61+
const { className, href, icon, primary, secondary, onClick } = props
62+
const classes = useStyles(props)
6663
const AvatorIcon = () => icon
6764
return (
6865
<Link href={href}>
@@ -84,5 +81,3 @@ const NextListItemComponent = (props: IProps) => {
8481
</Link>
8582
)
8683
}
87-
88-
export const NextListItem = withStyles(styles)(NextListItemComponent)

components/molecules/PageHeader.tsx

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
import { Paper, Typography } from "@material-ui/core"
2-
import {
3-
createStyles,
4-
Theme,
5-
withStyles,
6-
WithStyles,
7-
} from "@material-ui/core/styles"
2+
import { createStyles, makeStyles, Theme } from "@material-ui/core/styles"
83
import React from "react"
94
import { connect } from "react-redux"
105
import { Page } from "../../constants"
116
import { IInitialState } from "../../store/states"
127

13-
const styles = (theme: Theme) =>
8+
const useStyles = makeStyles((theme: Theme) =>
149
createStyles({
1510
root: {
1611
backgroundColor: theme.palette.primary.main,
@@ -28,8 +23,9 @@ const styles = (theme: Theme) =>
2823
},
2924
description: {},
3025
})
26+
)
3127

32-
interface IProps extends WithStyles<typeof styles> {
28+
interface IProps {
3329
/**
3430
* from redux
3531
*/
@@ -41,7 +37,8 @@ interface IProps extends WithStyles<typeof styles> {
4137
* @param props IProps
4238
*/
4339
const PageHeaderComponent = (props: IProps) => {
44-
const { classes, selectedPage } = props
40+
const { selectedPage } = props
41+
const classes = useStyles(props)
4542
return (
4643
<Paper square={true} className={classes.root}>
4744
<Typography variant="h1" color="inherit" className={classes.title}>
@@ -62,9 +59,7 @@ const mapStateToProps = (state: IInitialState) => ({
6259
selectedPage: state.page.selectedPage,
6360
})
6461

65-
export const PageHeader = withStyles(styles)(
66-
connect(
67-
mapStateToProps,
68-
undefined
69-
)(PageHeaderComponent as any)
70-
)
62+
export const PageHeader = connect(
63+
mapStateToProps,
64+
undefined
65+
)(PageHeaderComponent as any)
Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
import {
2-
createStyles,
3-
Theme,
4-
withStyles,
5-
WithStyles,
6-
} from "@material-ui/core/styles"
1+
import { createStyles, makeStyles, Theme } from "@material-ui/core/styles"
72
import { PageHeader } from "../molecules"
83

9-
const styles = (theme: Theme) =>
4+
const useStyles = makeStyles((theme: Theme) =>
105
createStyles({
116
root: {},
127
contentsContainer: {
138
padding: theme.spacing(1),
149
},
1510
})
11+
)
1612

17-
interface IProps extends WithStyles<typeof styles> {
13+
interface IProps {
1814
/**
1915
* children
2016
*/
@@ -25,16 +21,13 @@ interface IProps extends WithStyles<typeof styles> {
2521
* Header and article container component
2622
* @param props IProps
2723
*/
28-
const HeaderArticleContainerComponent = (props: IProps) => {
29-
const { classes, children } = props
24+
export const HeaderArticleContainer = function(props: IProps) {
25+
const { children } = props
26+
const classes = useStyles(props)
3027
return (
3128
<>
3229
<PageHeader />
3330
<section className={classes.contentsContainer}>{children}</section>
3431
</>
3532
)
3633
}
37-
38-
export const HeaderArticleContainer = withStyles(styles)(
39-
HeaderArticleContainerComponent
40-
)

components/organisms/Sidenavi.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { List } from "@material-ui/core"
2-
import {
3-
createStyles,
4-
Theme,
5-
withStyles,
6-
WithStyles,
7-
} from "@material-ui/core/styles"
2+
import { createStyles, makeStyles, Theme } from "@material-ui/core/styles"
83
import SvgIcon from "@material-ui/core/SvgIcon"
94
import { connect } from "react-redux"
105
import { bindActionCreators, Dispatch } from "redux"
@@ -14,7 +9,7 @@ import { IPagePayload, PageActions } from "../../store/actions"
149
import { IInitialState } from "../../store/states"
1510
import { NextListItem } from "../molecules"
1611

17-
const styles = (theme: Theme) =>
12+
const useStyles = makeStyles((theme: Theme) =>
1813
createStyles({
1914
root: {
2015
backgroundColor: theme.palette.primary.main,
@@ -50,8 +45,9 @@ const styles = (theme: Theme) =>
5045
backgroundColor: theme.palette.primary.light,
5146
},
5247
})
48+
)
5349

54-
interface IProps extends WithStyles<typeof styles> {
50+
interface IProps {
5551
changePage: (pagePayload: IPagePayload) => number
5652
selectedPage: Page
5753
}
@@ -61,7 +57,8 @@ interface IProps extends WithStyles<typeof styles> {
6157
* @param props IProps
6258
*/
6359
const SidenaviComponent = (props: IProps) => {
64-
const { classes, changePage, selectedPage } = props
60+
const { changePage, selectedPage } = props
61+
const classes = useStyles(props)
6562

6663
const handleChangePage = (page: Page) => () =>
6764
changePage({ selectedPage: page })
@@ -111,4 +108,4 @@ const mapDispatchToProps = (dispatch: Dispatch<Action<IPagePayload>>) =>
111108
export const Sidenavi = connect(
112109
mapStateToProps,
113110
mapDispatchToProps
114-
)(withStyles(styles)(SidenaviComponent as any))
111+
)(SidenaviComponent as any)

components/templates/Layout.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
1-
import {
2-
createStyles,
3-
Theme,
4-
withStyles,
5-
WithStyles,
6-
} from "@material-ui/core/styles"
1+
import { createStyles, makeStyles, Theme } from "@material-ui/core/styles"
72
import Head from "next/head"
83
import * as React from "react"
94
import { connect } from "react-redux"
105
import { Page } from "../../constants"
116
import { IInitialState } from "../../store/states"
127
import { ResponsiveDrawer } from "../organisms"
138

14-
const styles = (theme: Theme) =>
9+
const useStyles = makeStyles((theme: Theme) =>
1510
createStyles({
1611
root: {
1712
height: "100%",
1813
},
1914
})
15+
)
2016

21-
interface IProps extends WithStyles<typeof styles> {
17+
interface IProps {
2218
children: React.ReactNode
2319
selectedPage: Page
2420
}
2521

2622
const LayoutComponent = (props: IProps) => {
27-
const { classes, children, selectedPage } = props
23+
const { children, selectedPage } = props
24+
const classes = useStyles(props)
2825
return (
2926
<section className={classes.root}>
3027
<Head>
@@ -44,4 +41,4 @@ const mapStateToProps = (state: IInitialState) => ({
4441
export const Layout = connect(
4542
mapStateToProps,
4643
undefined
47-
)(withStyles(styles)(LayoutComponent as any))
44+
)(LayoutComponent as any)

0 commit comments

Comments
 (0)