aw-front-helpers 2.5.4 → 2.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/classes/formControl.js +42 -22
- package/package.json +2 -2
package/classes/formControl.js
CHANGED
|
@@ -100,25 +100,42 @@ export default class FormControl {
|
|
|
100
100
|
|
|
101
101
|
setError(element, message = null) {
|
|
102
102
|
const parent = element.closest(this._options.selectorGroup);
|
|
103
|
-
if(
|
|
103
|
+
if (parent === null) return;
|
|
104
104
|
let error = parent.querySelector(`.${this._options.classErrorElement}`);
|
|
105
|
+
parent.classList.add(this._options.classIsError);
|
|
106
|
+
const id = `error-${[...Array(6)].map(() => Math.floor(Math.random() * 16).toString(16)).join('')}`;
|
|
105
107
|
if (!error) {
|
|
106
108
|
error = Object.assign(
|
|
107
|
-
document.createElement('
|
|
108
|
-
|
|
109
|
+
document.createElement('p'), {
|
|
110
|
+
id,
|
|
111
|
+
className: this._options.classErrorElement,
|
|
112
|
+
innerHTML: message
|
|
109
113
|
});
|
|
114
|
+
element.setAttribute('aria-describedby', id);
|
|
110
115
|
parent.appendChild(error);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
116
|
+
} else {
|
|
117
|
+
if (error.tagName === 'P') {
|
|
118
|
+
if (message === error.innerHTML) return;
|
|
119
|
+
const newError = Object.assign(
|
|
120
|
+
document.createElement('ul'), {
|
|
121
|
+
id,
|
|
122
|
+
className: this._options.classErrorElement,
|
|
123
|
+
innerHTML: `<li>${error.innerHTML}</li>`
|
|
124
|
+
});
|
|
125
|
+
element.setAttribute('aria-describedby', id);
|
|
126
|
+
parent.appendChild(newError);
|
|
127
|
+
error.parentNode.removeChild(error);
|
|
128
|
+
error = newError;
|
|
129
|
+
}
|
|
130
|
+
const previousErrors = [...error.querySelectorAll('li')];
|
|
131
|
+
for (let i = 0; i < previousErrors.length; i++) {
|
|
132
|
+
if (previousErrors[i].innerHTML === message) return;
|
|
133
|
+
}
|
|
134
|
+
if (message) {
|
|
135
|
+
const errorElement = document.createElement('li');
|
|
136
|
+
errorElement.innerHTML = message;
|
|
137
|
+
error.appendChild(errorElement);
|
|
138
|
+
}
|
|
122
139
|
}
|
|
123
140
|
}
|
|
124
141
|
|
|
@@ -149,14 +166,14 @@ export default class FormControl {
|
|
|
149
166
|
/** @private */
|
|
150
167
|
_formCreateBound() {
|
|
151
168
|
['_onChange', '_onFormSubmit', 'onFormSubmit']
|
|
152
|
-
|
|
169
|
+
.forEach((fn) => this[fn] = this[fn].bind(this));
|
|
153
170
|
}
|
|
154
171
|
|
|
155
172
|
/** @private */
|
|
156
173
|
_removeError(el = null) {
|
|
157
174
|
if (el) {
|
|
158
|
-
const group = el.closest(
|
|
159
|
-
if(
|
|
175
|
+
const group = el.closest(this._options.selectorGroup);
|
|
176
|
+
if (group === null) return;
|
|
160
177
|
group.classList.remove(this._options.classIsError);
|
|
161
178
|
const currentErrors = [...group.querySelectorAll(`.${this._options.classErrorElement}`)];
|
|
162
179
|
currentErrors.forEach(error => {
|
|
@@ -219,11 +236,14 @@ export default class FormControl {
|
|
|
219
236
|
if (!isValid && firstErrorElement === null) firstErrorElement = el;
|
|
220
237
|
}
|
|
221
238
|
|
|
222
|
-
if (!isValid && firstErrorElement
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
239
|
+
if (!isValid && firstErrorElement) {
|
|
240
|
+
firstErrorElement.focus();
|
|
241
|
+
if (this._options.animateToFirstError) {
|
|
242
|
+
gsap.to(window, {
|
|
243
|
+
duration: .6,
|
|
244
|
+
scrollTo: (window.pageYOffset + firstErrorElement.getBoundingClientRect().top - 150)
|
|
245
|
+
})
|
|
246
|
+
}
|
|
227
247
|
}
|
|
228
248
|
return isValid;
|
|
229
249
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "aw-front-helpers",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.6",
|
|
4
4
|
"description": "Assets helpers for projects",
|
|
5
5
|
"author": "Yoann Le Poupon",
|
|
6
6
|
"license": "ISC",
|
|
@@ -12,6 +12,6 @@
|
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"es6-symbol": "^3.1.3",
|
|
14
14
|
"exif-js": "^2.3.0",
|
|
15
|
-
"gsap": "^3.
|
|
15
|
+
"gsap": "^3.12.1"
|
|
16
16
|
}
|
|
17
17
|
}
|