|
| 1 | +import * as React from "react"; |
| 2 | +import makeStyles from "@material-ui/styles/makeStyles"; |
| 3 | +import createStyles from "@material-ui/styles/createStyles"; |
| 4 | +import TextField from "@material-ui/core/TextField"; |
| 5 | +import Button from "@material-ui/core/Button"; |
| 6 | +import Card from "@material-ui/core/Card"; |
| 7 | +import CardHeader from "@material-ui/core/CardHeader"; |
| 8 | +import CardContent from "@material-ui/core/CardContent"; |
| 9 | +import { LoginEntity, createEmptyLogin } from "../model/login"; |
| 10 | + |
| 11 | +interface PropsForm { |
| 12 | + onLogin: (login: LoginEntity) => void; |
| 13 | +} |
| 14 | + |
| 15 | +// https://material-ui.com/styles/api/#makestyles-styles-options-hook |
| 16 | +const useFormStyles = makeStyles((theme) => |
| 17 | + createStyles({ |
| 18 | + formContainer: { |
| 19 | + display: "flex", |
| 20 | + flexDirection: "column", |
| 21 | + justifyContent: "center", |
| 22 | + }, |
| 23 | + card: { |
| 24 | + maxWidth: 400, |
| 25 | + margin: "0 auto", |
| 26 | + }, |
| 27 | + }) |
| 28 | +); |
| 29 | + |
| 30 | +export const LoginComponent: React.FC<PropsForm> = (props) => { |
| 31 | + const { onLogin } = props; |
| 32 | + const [loginInfo, setLoginInfo] = React.useState<LoginEntity>( |
| 33 | + createEmptyLogin() |
| 34 | + ); |
| 35 | + const classes = useFormStyles(); |
| 36 | + const onTexFieldChange = (fieldId) => (e) => { |
| 37 | + setLoginInfo({ |
| 38 | + ...loginInfo, |
| 39 | + [fieldId]: e.target.value, |
| 40 | + }); |
| 41 | + }; |
| 42 | + |
| 43 | + return ( |
| 44 | + <Card className={classes.card}> |
| 45 | + <CardHeader title="Login" /> |
| 46 | + <CardContent> |
| 47 | + <div className={classes.formContainer}> |
| 48 | + <TextField |
| 49 | + label="Name" |
| 50 | + margin="normal" |
| 51 | + value={loginInfo.login} |
| 52 | + onChange={onTexFieldChange("login")} |
| 53 | + /> |
| 54 | + <TextField |
| 55 | + label="Password" |
| 56 | + type="password" |
| 57 | + margin="normal" |
| 58 | + value={loginInfo.password} |
| 59 | + onChange={onTexFieldChange("password")} |
| 60 | + /> |
| 61 | + <Button |
| 62 | + variant="contained" |
| 63 | + color="primary" |
| 64 | + onClick={() => onLogin(loginInfo)} |
| 65 | + > |
| 66 | + Login |
| 67 | + </Button> |
| 68 | + </div> |
| 69 | + </CardContent> |
| 70 | + </Card> |
| 71 | + ); |
| 72 | +}; |
0 commit comments