-
Notifications
You must be signed in to change notification settings - Fork 81
/
EmployeeForm.vue
102 lines (91 loc) · 1.86 KB
/
EmployeeForm.vue
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
<template>
<div id="employee-form">
<form @submit.prevent="handleSubmit">
<label>Employee name</label>
<input
ref="first"
type="text"
:class="{ 'has-error': submitting && invalidName }"
v-model="employee.name"
@focus="clearStatus"
@keypress="clearStatus"
>
<label>Employee Email</label>
<input
type="text"
:class="{ 'has-error': submitting && invalidEmail }"
v-model="employee.email"
@focus="clearStatus"
>
<p
v-if="error && submitting"
class="error-message"
>❗Please fill out all required fields</p>
<p
v-if="success"
class="success-message"
>✅ Employee successfully added</p>
<button>Add Employee</button>
</form>
</div>
</template>
<script>
export default {
name: 'employee-form',
data() {
return {
error: false,
submitting: false,
success: false,
employee: {
name: '',
email: '',
}
}
},
computed: {
invalidName() {
return this.employee.name === ''
},
invalidEmail() {
return this.employee.email === ''
},
},
methods: {
handleSubmit() {
this.clearStatus()
this.submitting = true
if (this.invalidName || this.invalidEmail) {
this.error = true
return
}
this.$emit('add:employee', this.employee)
this.$refs.first.focus()
this.employee = {
name: '',
email: '',
}
this.success = true
this.error = false
this.submitting = false
},
clearStatus() {
this.success = false
this.error = false
}
}}
</script>
<style scoped>
form {
margin-bottom: 2rem;
}
[class*="-message"] {
font-weight: 500;
}
.error-message {
color: #d33c40;
}
.success-message {
color: #32a95d;
}
</style>