-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
81 lines (69 loc) · 2.07 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const componentOne = document.getElementById('component-1');
const form = document.getElementById('form');
const emailField = form.email;
const emailParent = emailField.parentNode;
const componentTwo = document.getElementById('component-2');
const dismissBtn = componentTwo.querySelector('button');
const emailOutput = document.getElementById('emailOutput');
const showError = () => {
emailParent.classList.add('invalid');
emailField.setAttribute('aria-invalid', 'true');
emailField.setAttribute('aria-describedby', 'emailError');
};
const hideError = () => {
emailParent.classList.remove('invalid');
emailField.removeAttribute('aria-invalid');
emailField.removeAttribute('aria-describedby');
};
const showComponent = component => {
component.classList.remove('d-none');
component.classList.add('slide');
component.querySelector('.heading').focus();
};
const hideComponent = component => {
component.classList.add('d-none');
component.classList.remove('slide');
}
const printEmailValue = () => {
emailOutput.innerHTML = emailField.value;
emailField.value = '';
};
const handleInput = () => {
if (emailField.validity.valid || emailField.validity.valueMissing) {
hideError();
} else {
showError();
}
};
const handleBlur = () => {
if (emailField.validity.valueMissing) {
hideError();
}
};
const handleSubmit = (event) => {
event.preventDefault();
if (emailField.validity.valid) {
hideError();
hideComponent(componentOne);
printEmailValue();
showComponent(componentTwo);
} else {
showError();
}
};
const handleDismiss = () => {
hideComponent(componentTwo);
showComponent(componentOne);
};
const addMoveAnimation = () => {
const selector = '[class^="move-"]';
const moves = document.querySelectorAll(selector);
moves.forEach(move => {
move.classList.remove('move');
move.classList.add('move');
});
};
emailField.addEventListener('input', handleInput);
form.addEventListener('submit', handleSubmit);
dismissBtn.addEventListener('click', handleDismiss);
window.addEventListener('DOMContentLoaded', addMoveAnimation);