This repository has been archived by the owner on Oct 8, 2023. It is now read-only.
generated from Tim-W-James/frontend-ts-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExampleForm.tsx
119 lines (113 loc) · 3.77 KB
/
ExampleForm.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { UserContext } from "@context/UserContext";
import { FormEventHandler } from "react";
import { Button, Col, Container, Form, InputGroup, Row } from "react-bootstrap";
import { BsFillPersonCheckFill } from "react-icons/bs";
interface FormElements extends HTMLFormControlsCollection {
firstNameInput: HTMLInputElement;
lastNameInput: HTMLInputElement;
usernameInput: HTMLInputElement;
emailInput: HTMLInputElement;
}
interface UserFormElement extends HTMLFormElement {
readonly elements: FormElements;
}
const ExampleForm: React.FC<{
heading: string;
}> = ({ heading }) => {
const { userState, setUserState } = useContext(UserContext);
const [validated, setValidated] = useState(false);
const handleSubmit: FormEventHandler<UserFormElement> = (event) => {
event.preventDefault();
event.stopPropagation();
const form = event.currentTarget;
if (form.checkValidity()) {
setUserState &&
setUserState({
firstName: form.elements.firstNameInput.value,
lastName: form.elements.lastNameInput.value,
username: form.elements.usernameInput.value,
email: form.elements.emailInput.value,
});
}
setValidated(true);
};
return (
<Container>
<h2>{heading}</h2>
<hr />
<Form noValidate onSubmit={handleSubmit} validated={validated}>
<Row className="mb-3">
<Form.Group as={Col} controlId="firstNameInput" md="6">
<Form.Label>First name</Form.Label>
<Form.Control
defaultValue="John"
placeholder="First name"
required
type="text"
/>
<Form.Control.Feedback type="invalid">
Please provide a valid email.
</Form.Control.Feedback>
</Form.Group>
<Form.Group as={Col} controlId="lastNameInput" md="6">
<Form.Label>Last name</Form.Label>
<Form.Control
defaultValue="Doe"
placeholder="Last name"
required
type="text"
/>
<Form.Control.Feedback type="invalid">
Please provide a valid email.
</Form.Control.Feedback>
</Form.Group>
</Row>
<Row className="mb-3">
<Form.Group as={Col} controlId="emailInput" md="6">
<Form.Label>Email</Form.Label>
<Form.Control
placeholder="example@example.com"
required
type="email"
/>
<Form.Control.Feedback type="invalid">
Please provide a valid email.
</Form.Control.Feedback>
</Form.Group>
<Form.Group as={Col} controlId="usernameInput" md="6">
<Form.Label>Username</Form.Label>
<InputGroup hasValidation>
<InputGroup.Text id="inputGroupPrepend">@</InputGroup.Text>
<Form.Control
aria-describedby="inputGroupPrepend"
placeholder="Username"
required
type="text"
/>
<Form.Control.Feedback type="invalid">
Please choose a username.
</Form.Control.Feedback>
</InputGroup>
</Form.Group>
</Row>
<Form.Group className="mb-3">
<Form.Check
feedback="You must agree before submitting."
feedbackType="invalid"
label="Agree to terms and conditions"
required
/>
</Form.Group>
<Button type="submit">Submit form</Button>
</Form>
{userState && (
<>
<br />
<BsFillPersonCheckFill />
{` Welcome ${userState.firstName} ${userState.lastName}`}
</>
)}
</Container>
);
};
export default ExampleForm;