aw-front-helpers 2.4.0 → 2.4.1

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.
@@ -1,9 +1,11 @@
1
- import { gsap } from 'gsap/all';
1
+ import {
2
+ gsap
3
+ } from 'gsap/all';
2
4
  import ScrollToPlugin from 'gsap/ScrollToPlugin';
3
- gsap.registerPlugin( ScrollToPlugin );
4
- import create from 'dom-create-element';
5
- import getClosest from '../utils/dom/getClosest';
6
- import regex_email from '../utils/regex/email';
5
+ gsap.registerPlugin(ScrollToPlugin);
6
+ import create from 'dom-create-element';
7
+ import getClosest from '../utils/dom/getClosest';
8
+ import regex_email from '../utils/regex/email';
7
9
 
8
10
  const defaultOptions = {
9
11
  selectorGroup: '.form-group',
@@ -40,14 +42,17 @@ export default class FormControl {
40
42
  * @param {Object} options.i18n.checkbox - The employee's department.
41
43
  * @param {Object} options.i18n.radio - The employee's department.
42
44
  * @param {Object} options.i18n.pattern - The employee's department.
43
- **/
45
+ **/
44
46
  constructor(form, options = {}) {
45
47
  /** @private */
46
48
  this._form = form;
47
49
  /** @private */
48
50
  this._dom = {};
49
51
  /** @private */
50
- this._options = {...defaultOptions, ...options};
52
+ this._options = {
53
+ ...defaultOptions,
54
+ ...options
55
+ };
51
56
 
52
57
  this._form.setAttribute('novalidate', 'novalidate');
53
58
  this._formCreateBound();
@@ -85,10 +90,10 @@ export default class FormControl {
85
90
  // -
86
91
  destroy() {
87
92
  this._form.removeEventListener('submit', this._onFormSubmit);
88
- this._domForm.inputs.forEach( input => {
93
+ this._domForm.inputs.forEach(input => {
89
94
  input.removeEventListener('keyup', this._onChange);
90
95
  });
91
- this._domForm.choices.forEach( input => {
96
+ this._domForm.choices.forEach(input => {
92
97
  input.removeEventListener('change', this._onChange);
93
98
  });
94
99
  }
@@ -99,9 +104,9 @@ export default class FormControl {
99
104
 
100
105
  setError(element, message = null) {
101
106
  const parent = getClosest(element, this._options.selectorGroup);
102
- if( parent) {
107
+ if (parent) {
103
108
  let error = parent.querySelector(`.${this._options.classErrorElement}`);
104
- if(!error) {
109
+ if (!error) {
105
110
  error = create({
106
111
  selector: 'ul',
107
112
  styles: this._options.classErrorElement
@@ -111,10 +116,10 @@ export default class FormControl {
111
116
  parent.classList.add(this._options.classIsError);
112
117
 
113
118
  const previousErrors = [...error.querySelectorAll('li')];
114
- for(let i = 0; i < previousErrors.length; i++) {
115
- if(previousErrors[i].innerHTML === message) return;
119
+ for (let i = 0; i < previousErrors.length; i++) {
120
+ if (previousErrors[i].innerHTML === message) return;
116
121
  }
117
- if(message) {
122
+ if (message) {
118
123
  const errorElement = document.createElement('li');
119
124
  errorElement.innerHTML = message;
120
125
  error.appendChild(errorElement);
@@ -124,13 +129,20 @@ export default class FormControl {
124
129
 
125
130
  //________________________________________________________ events
126
131
  // -
127
- /** @private */
128
- _onFormSubmit(pEvt) {
132
+ /** @protected */
133
+ onFormSubmit(pEvt) {
129
134
  if (!this._isValid()) {
130
135
  pEvt.preventDefault();
131
136
  }
132
137
  }
133
138
 
139
+ /**
140
+ * @deprecated use onFormSubmit
141
+ */
142
+ _onFormSubmit(pEvt) {
143
+ this.onFormSubmit(pEvt);
144
+ }
145
+
134
146
  /** @private */
135
147
  _onChange(pEvt) {
136
148
  const el = pEvt.target;
@@ -141,13 +153,13 @@ export default class FormControl {
141
153
  // -
142
154
  /** @private */
143
155
  _formCreateBound() {
144
- ['_onChange', '_onFormSubmit']
145
- .forEach((fn) => this[fn] = this[fn].bind(this));
156
+ ['_onChange', '_onFormSubmit', 'onFormSubmit']
157
+ .forEach((fn) => this[fn] = this[fn].bind(this));
146
158
  }
147
159
 
148
160
  /** @private */
149
161
  _removeError(el = null) {
150
- if(el) {
162
+ if (el) {
151
163
  const group = getClosest(el, this._options.selectorGroup);
152
164
  group.classList.remove(this._options.classIsError);
153
165
  const currentErrors = [...group.querySelectorAll(`.${this._options.classErrorElement}`)];
@@ -174,22 +186,22 @@ export default class FormControl {
174
186
  if (el.getAttribute('disabled') !== null) return false;
175
187
 
176
188
  let invalid = false;
177
- if( !testRequired(el, this._form) ) {
189
+ if (!testRequired(el, this._form)) {
178
190
  invalid = true;
179
191
  const type = el.getAttribute('type');
180
- if(type === 'radio') this.setError(el, this._options.i18n.radio);
181
- else if(type === 'checkbox') this.setError(el, this._options.i18n.checkbox);
182
- else if(type !== 'hidden') this.setError(el, this._options.i18n.empty);
192
+ if (type === 'radio') this.setError(el, this._options.i18n.radio);
193
+ else if (type === 'checkbox') this.setError(el, this._options.i18n.checkbox);
194
+ else if (type !== 'hidden') this.setError(el, this._options.i18n.empty);
183
195
  }
184
- if( !testEmail(el, this._form) ) {
196
+ if (!testEmail(el, this._form)) {
185
197
  invalid = true;
186
198
  this.setError(el, this._options.i18n.email);
187
199
  }
188
- if( !testPattern(el, this._form) ) {
200
+ if (!testPattern(el, this._form)) {
189
201
  invalid = true;
190
202
  this.setError(el, this._options.i18n.pattern);
191
203
  }
192
- if( !testSame(el, this._form) ) {
204
+ if (!testSame(el, this._form)) {
193
205
  invalid = true;
194
206
  this.setError(el, this._options.i18n.same);
195
207
  }
@@ -199,13 +211,14 @@ export default class FormControl {
199
211
 
200
212
  /** @private */
201
213
  _isValid() {
202
- let isValid = true, firstErrorElement = null;
214
+ let isValid = true,
215
+ firstErrorElement = null;
203
216
  this._removeError();
204
217
 
205
218
  for (let i = 0; i < this._domForm.els.length; i++) {
206
219
  const el = this._domForm.els[i];
207
220
  const error = this._checkError(el);
208
- if( !error ) continue;
221
+ if (!error) continue;
209
222
  else isValid = false;
210
223
  if (!isValid && firstErrorElement === null) firstErrorElement = el;
211
224
  }
@@ -231,10 +244,10 @@ export default class FormControl {
231
244
  /** @private */
232
245
  _formAddEvents() {
233
246
  this._form.addEventListener('submit', this._onFormSubmit);
234
- this._domForm.inputs.forEach( input => {
247
+ this._domForm.inputs.forEach(input => {
235
248
  input.addEventListener('keyup', this._onChange);
236
249
  });
237
- this._domForm.choices.forEach( input => {
250
+ this._domForm.choices.forEach(input => {
238
251
  input.addEventListener('change', this._onChange);
239
252
  });
240
253
  }
@@ -251,13 +264,13 @@ export function testRequired(el, form) {
251
264
  if (isRequired === null) return true;
252
265
 
253
266
  const type = el.getAttribute('type');
254
- if(type === 'radio') {
267
+ if (type === 'radio') {
255
268
  const radios = [...form.querySelectorAll(`input[type="radio"][name="${el.getAttribute('name')}"]`)];
256
269
  for (let i = 0; i < radios.length; i++) {
257
270
  if (radios[i].checked) return true;
258
271
  }
259
272
  return false;
260
- } else if( type === 'checkbox') return el.checked;
273
+ } else if (type === 'checkbox') return el.checked;
261
274
  return el.value !== '';
262
275
  }
263
276
 
@@ -269,8 +282,8 @@ export function testRequired(el, form) {
269
282
  */
270
283
  export function testEmail(el, form) {
271
284
  const type = el.getAttribute('type');
272
- if(type !== 'email') return true;
273
- return el.value === '' && !regex_email(el.value);
285
+ if (type !== 'email' || el.value === '') return true;
286
+ return regex_email(el.value);
274
287
  }
275
288
 
276
289
  /**
@@ -281,7 +294,7 @@ export function testEmail(el, form) {
281
294
  */
282
295
  export function testSame(el, form) {
283
296
  const hasSame = el.getAttribute('data-formvalidate-same');
284
- if(hasSame === null) return true;
297
+ if (hasSame === null) return true;
285
298
  const twinEl = form.querySelector(`[data-formvalidate-same=${el.getAttribute('data-formvalidate-same')}]`);
286
299
  return twinEl.value === el.value;
287
300
  }
@@ -294,7 +307,7 @@ export function testSame(el, form) {
294
307
  */
295
308
  export function testPattern(el, form) {
296
309
  const pattern = el.getAttribute('pattern');
297
- if(pattern === null) return true;
310
+ if (pattern === null || el.value === '') return true;
298
311
  const rgx = new RegExp(pattern);
299
- return el.value === '' && !rgx.test(el.value);
312
+ return rgx.test(el.value);
300
313
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aw-front-helpers",
3
- "version": "2.4.0",
3
+ "version": "2.4.1",
4
4
  "description": "Assets helpers for projects",
5
5
  "author": "Yoann Le Poupon",
6
6
  "license": "ISC",