Skip to content

Commit a980858

Browse files
author
Víctor Borrego Pérez
committed
Adding material-ui hooks to 14_FormValidation example
1 parent 2fd8c1a commit a980858

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

hooks/14_FormValidation/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/14_FormValidation/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/14_FormValidation/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 } 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 [loginInfo, setLoginInfo] = React.useState<LoginEntity>(
@@ -37,7 +39,7 @@ const LoginPageInner = (props: Props) => {
3739
createDefaultLoginFormErrors()
3840
);
3941
const [showLoginFailedMsg, setShowLoginFailedMsg] = React.useState(false);
40-
const { classes } = props;
42+
const classes = useStyles();
4143

4244
const onLogin = () => {
4345
loginFormValidation.validateForm(loginInfo).then(formValidationResult => {
@@ -96,7 +98,7 @@ const LoginPageInner = (props: Props) => {
9698
);
9799
};
98100

99-
export const LoginPage = withStyles(styles)(withRouter<Props>(LoginPageInner));
101+
export const LoginPage = withRouter<Props>(LoginPageInner);
100102

101103
interface PropsForm {
102104
onLogin: () => void;
@@ -105,7 +107,19 @@ interface PropsForm {
105107
loginFormErrors: LoginFormErrors;
106108
}
107109

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

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

116130
return (
117-
<div
118-
style={{
119-
display: "flex",
120-
flexDirection: "column",
121-
justifyContent: "center"
122-
}}
123-
>
131+
<div className={classes.formContainer}>
124132
<TextFieldForm
125133
label="Name"
126134
name="login"

0 commit comments

Comments
 (0)