Skip to content

Commit 66c86e9

Browse files
author
Víctor Borrego Pérez
committed
Adding material-ui hooks to 15_Context example
1 parent 666c18c commit 66c86e9

File tree

5 files changed

+46
-34
lines changed

5 files changed

+46
-34
lines changed

hooks/13_LoginForm/Readme.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,13 @@ _./src/index.html_
111111
- Let's build a proper _login_ layout, _loginPage.tsx_. To build a nice layout, we will install _@material-ui/core_ and _@material-ui/icons_
112112

113113
```bash
114-
npm install @material-ui/core @material-ui/icons --save
114+
npm install @material-ui/core@^4.0.1 @material-ui/icons@^4.0.1 --save
115+
```
116+
117+
- However, we must be careful with the compatibility of certain versions of _typescript_ with the new _hooks_ of _material-ui_. For this example, we can install _typescript@3.3.3_ version.
118+
119+
```bash
120+
npm install typescript@3.3.3 --save
115121
```
116122

117123
- Now we could create a login form it could look something like:

hooks/15_Context/Readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const createDefaultUser = (): SessionContextProps => ({
3232
});
3333

3434
export const SessionContext =
35-
React.createContext < SessionContextProps >(createDefaultUser());
35+
React.createContext < SessionContextProps > createDefaultUser();
3636
```
3737

3838
- This session context will expose a _provider_ (it will serve us to set the login name in the context), and a _consumer_ (that will let us consume the login name from the context at any point of the application).
@@ -140,7 +140,7 @@ const LoginPageInner = (props: Props) => {
140140
createDefaultLoginFormErrors()
141141
);
142142
const [showLoginFailedMsg, setShowLoginFailedMsg] = React.useState(false);
143-
const { classes } = props;
143+
const classes = useStyles();
144144

145145
const onLogin = () => {
146146
loginFormValidation.validateForm(loginInfo).then(formValidationResult => {

hooks/15_Context/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@
2929
"html-webpack-plugin": "^3.2.0",
3030
"mini-css-extract-plugin": "^0.5.0",
3131
"style-loader": "^0.23.1",
32-
"typescript": "^3.3.3",
32+
"typescript": "3.3.3",
3333
"url-loader": "^1.1.2",
3434
"webpack": "^4.29.3",
3535
"webpack-cli": "^3.2.3",
3636
"webpack-dev-server": "^3.1.14"
3737
},
3838
"dependencies": {
39-
"@material-ui/core": "^3.9.2",
40-
"@material-ui/icons": "^3.0.2",
39+
"@material-ui/core": "^4.0.1",
40+
"@material-ui/icons": "^4.0.1",
4141
"lc-form-validation": "^2.0.0",
4242
"react": "^16.8.2",
4343
"react-dom": "^16.8.2",

hooks/15_Context/src/common/notification.tsx

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
import * as React from "react";
2-
import Button from "@material-ui/core/Button";
32
import Snackbar from "@material-ui/core/Snackbar";
43
import IconButton from "@material-ui/core/IconButton";
54
import CloseIcon from "@material-ui/icons/Close";
6-
import { withStyles } from "@material-ui/core";
5+
import createStyles from "@material-ui/core/styles/createStyles";
6+
import makeStyles from "@material-ui/core/styles/makeStyles";
77

88
interface Props {
9-
classes?: any;
109
message: string;
1110
show: boolean;
1211
onClose: () => void;
1312
}
1413

15-
const styles = theme => ({
16-
close: {
17-
padding: theme.spacing.unit / 2
18-
}
19-
});
14+
const useStyles = makeStyles(theme =>
15+
createStyles({
16+
close: {
17+
padding: theme.spacing(0.5)
18+
}
19+
})
20+
);
2021

21-
const NotificationComponentInner = (props: Props) => {
22-
const { classes, message, show, onClose } = props;
22+
export const NotificationComponent = (props: Props) => {
23+
const classes = useStyles();
24+
const { message, show, onClose } = props;
2325

2426
return (
2527
<Snackbar
@@ -48,7 +50,3 @@ const NotificationComponentInner = (props: Props) => {
4850
/>
4951
);
5052
};
51-
52-
export const NotificationComponent = withStyles(styles)(
53-
NotificationComponentInner
54-
);

hooks/15_Context/src/pages/loginPage.tsx

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as React from "react";
22
import { Link } from "react-router-dom";
33
import { withRouter, RouteComponentProps } from "react-router-dom";
4-
import { withStyles, createStyles, WithStyles } from "@material-ui/core/styles";
4+
import makeStyles from "@material-ui/styles/makestyles";
5+
import createStyles from "@material-ui/styles/createStyles";
56
import Card from "@material-ui/core/Card";
67
import CardHeader from "@material-ui/core/CardHeader";
78
import CardContent from "@material-ui/core/CardContent";
@@ -18,16 +19,17 @@ import {
1819
import { loginFormValidation } from "./loginPage.validation";
1920
import { TextFieldForm, SessionContext } from "../common";
2021

21-
// https://material-ui.com/guides/typescript/
22-
const styles = theme =>
22+
// https://material-ui.com/styles/api/#makestyles-styles-options-hook
23+
const useStyles = makeStyles(theme =>
2324
createStyles({
2425
card: {
2526
maxWidth: 400,
2627
margin: "0 auto"
2728
}
28-
});
29+
})
30+
);
2931

30-
interface Props extends RouteComponentProps, WithStyles<typeof styles> {}
32+
interface Props extends RouteComponentProps {}
3133

3234
const LoginPageInner = (props: Props) => {
3335
const loginContext = React.useContext(SessionContext);
@@ -39,7 +41,7 @@ const LoginPageInner = (props: Props) => {
3941
createDefaultLoginFormErrors()
4042
);
4143
const [showLoginFailedMsg, setShowLoginFailedMsg] = React.useState(false);
42-
const { classes } = props;
44+
const classes = useStyles();
4345

4446
const onLogin = () => {
4547
loginFormValidation.validateForm(loginInfo).then(formValidationResult => {
@@ -99,7 +101,7 @@ const LoginPageInner = (props: Props) => {
99101
);
100102
};
101103

102-
export const LoginPage = withStyles(styles)(withRouter<Props>(LoginPageInner));
104+
export const LoginPage = withRouter<Props>(LoginPageInner);
103105

104106
interface PropsForm {
105107
onLogin: () => void;
@@ -108,7 +110,19 @@ interface PropsForm {
108110
loginFormErrors: LoginFormErrors;
109111
}
110112

113+
// https://material-ui.com/styles/api/#makestyles-styles-options-hook
114+
const useFormStyles = makeStyles(theme =>
115+
createStyles({
116+
formContainer: {
117+
display: "flex",
118+
flexDirection: "column",
119+
justifyContent: "center"
120+
}
121+
})
122+
);
123+
111124
const LoginForm = (props: PropsForm) => {
125+
const classes = useFormStyles();
112126
const { onLogin, onUpdateField, loginInfo, loginFormErrors } = props;
113127

114128
// TODO: Enhacement move this outside the stateless component discuss why is a good idea
@@ -117,13 +131,7 @@ const LoginForm = (props: PropsForm) => {
117131
};
118132

119133
return (
120-
<div
121-
style={{
122-
display: "flex",
123-
flexDirection: "column",
124-
justifyContent: "center"
125-
}}
126-
>
134+
<div className={classes.formContainer}>
127135
<TextFieldForm
128136
label="Name"
129137
name="login"

0 commit comments

Comments
 (0)