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.
@@ -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( parent === null ) return;
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('ul'), {
108
- className: this._options.classErrorElement
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
- parent.classList.add(this._options.classIsError);
113
-
114
- const previousErrors = [...error.querySelectorAll('li')];
115
- for (let i = 0; i < previousErrors.length; i++) {
116
- if (previousErrors[i].innerHTML === message) return;
117
- }
118
- if (message) {
119
- const errorElement = document.createElement('li');
120
- errorElement.innerHTML = message;
121
- error.appendChild(errorElement);
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
- .forEach((fn) => this[fn] = this[fn].bind(this));
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( this._options.selectorGroup );
159
- if( group === null ) return;
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 && this._options.animateToFirstError) {
223
- gsap.to(window, {
224
- duration: .6,
225
- scrollTo: (window.pageYOffset + firstErrorElement.getBoundingClientRect().top - 150)
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.4",
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.10.4"
15
+ "gsap": "^3.12.1"
16
16
  }
17
17
  }