-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmployeeForm.js
67 lines (61 loc) · 1.63 KB
/
EmployeeForm.js
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
// import the necessary libraries
import React, { useState } from 'react';
import '../Content/employee.css';
// define the function
function EmployeeForm(props) {
// define the varibales and setter functions, and set them to the default state
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [phone, setPhone] = useState('');
// define a submit function
const handleSubmit = (e) => {
e.preventDefault();
const employee = {
EmployeeId: Math.floor(Math.random() * 10000),
name,
email,
phone,
};
props.onSubmit(employee);
setName('');
setEmail('');
setPhone('');
};
// Render the HTML
return (
// define the form
<form className="employee-form" onSubmit={handleSubmit}>
<h2>Add Employee</h2>
<div>
<label htmlFor="name">Name: </label>
<input
type="text"
id="name"
value={name}
onChange={(e) => setName(e.target.value)}
/>
</div>
<div>
<label htmlFor="email">Email: </label>
<input
type="email"
id="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
</div>
<div>
<label htmlFor="phone">Phone: </label>
<input
type="tel"
id="phone"
value={phone}
onChange={(e) => setPhone(e.target.value)}
/>
</div>
<button type="submit">Add</button>
</form>
);
}
// allow the Component to be called from elsewhere
export default EmployeeForm;