aw-front-helpers 2.5.4 → 2.5.5

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,39 @@ 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);
105
106
  if (!error) {
106
107
  error = Object.assign(
107
- document.createElement('ul'), {
108
- className: this._options.classErrorElement
108
+ document.createElement('p'), {
109
+ className: this._options.classErrorElement,
110
+ innerHTML: message
109
111
  });
112
+ error.setAttribute('aria-describedby', element.id);
110
113
  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);
114
+ } else {
115
+ if (error.tagName === 'P') {
116
+ if (message === error.innerHTML) return;
117
+ const newError = Object.assign(
118
+ document.createElement('ul'), {
119
+ className: this._options.classErrorElement,
120
+ innerHTML: `<li>${error.innerHTML}</li>`
121
+ });
122
+ newError.setAttribute('aria-describedby', element.id);
123
+ parent.appendChild(newError);
124
+ error.parentNode.removeChild(error);
125
+ error = newError;
126
+ }
127
+ const previousErrors = [...error.querySelectorAll('li')];
128
+ for (let i = 0; i < previousErrors.length; i++) {
129
+ if (previousErrors[i].innerHTML === message) return;
130
+ }
131
+ if (message) {
132
+ const errorElement = document.createElement('li');
133
+ errorElement.innerHTML = message;
134
+ error.appendChild(errorElement);
135
+ }
122
136
  }
123
137
  }
124
138
 
@@ -149,14 +163,14 @@ export default class FormControl {
149
163
  /** @private */
150
164
  _formCreateBound() {
151
165
  ['_onChange', '_onFormSubmit', 'onFormSubmit']
152
- .forEach((fn) => this[fn] = this[fn].bind(this));
166
+ .forEach((fn) => this[fn] = this[fn].bind(this));
153
167
  }
154
168
 
155
169
  /** @private */
156
170
  _removeError(el = null) {
157
171
  if (el) {
158
- const group = el.closest( this._options.selectorGroup );
159
- if( group === null ) return;
172
+ const group = el.closest(this._options.selectorGroup);
173
+ if (group === null) return;
160
174
  group.classList.remove(this._options.classIsError);
161
175
  const currentErrors = [...group.querySelectorAll(`.${this._options.classErrorElement}`)];
162
176
  currentErrors.forEach(error => {
@@ -219,11 +233,14 @@ export default class FormControl {
219
233
  if (!isValid && firstErrorElement === null) firstErrorElement = el;
220
234
  }
221
235
 
222
- if (!isValid && firstErrorElement && this._options.animateToFirstError) {
223
- gsap.to(window, {
224
- duration: .6,
225
- scrollTo: (window.pageYOffset + firstErrorElement.getBoundingClientRect().top - 150)
226
- })
236
+ if (!isValid && firstErrorElement) {
237
+ firstErrorElement.focus();
238
+ if (this._options.animateToFirstError) {
239
+ gsap.to(window, {
240
+ duration: .6,
241
+ scrollTo: (window.pageYOffset + firstErrorElement.getBoundingClientRect().top - 150)
242
+ })
243
+ }
227
244
  }
228
245
  return isValid;
229
246
  }
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.5",
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
  }