-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployeeForm.vue
More file actions
92 lines (89 loc) · 1.83 KB
/
Copy pathEmployeeForm.vue
File metadata and controls
92 lines (89 loc) · 1.83 KB
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
<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 {
submitting:false,
error :false,
success:false,
employee: {
name: '',
email: '',
},
}
},
methods: {
handleSubmit() {
this.submitting=true
this.clearStatus()
if(this.invalidName||this.invalidEmail){
this.error=true
return
}
this.$emit('add:employee', this.employee)
this.employee = {
name: '',
email: '',
}
this.error = false
this.success = true
this.submitting = false
},
clearStatus() {
this.success = false
this.error = false
}
},
computed: {
invalidName() {
return this.employee.name===''
},
invalidEmail() {
return this.employee.email===''
},
},
}
</script>
<style scoped>
form {
margin-bottom: 2rem;
}
[class*='-message'] {
font-weight: 500;
}
.error-message {
color: #d33c40;
}
.success-message {
color: #32a95d;
}
</style>