@rxdi/forms 0.7.147 → 0.7.150

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.
@@ -21,17 +21,17 @@ export declare class FormGroup<T = FormInputOptions, E = {
21
21
  setOptions(options: FormOptions): this;
22
22
  getOptions(): FormOptions;
23
23
  get valueChanges(): import("rxjs").Observable<T>;
24
- updateValueAndValidity(): ErrorObject[];
24
+ updateValueAndValidity(): Promise<ErrorObject[]>;
25
25
  private updateValueAndValidityOnEvent;
26
26
  applyValidationContext({ errors, element }: ErrorObject): boolean;
27
27
  querySelectForm(shadowRoot: HTMLElement | ShadowRoot): HTMLFormElement;
28
28
  private querySelectAll;
29
29
  querySelectorAllInputs(): AbstractInput[];
30
30
  mapEventToInputs(inputs?: HTMLElement[]): AbstractInput[];
31
- setElementValidity(el: AbstractInput, validity?: boolean): void;
31
+ setElementValidity(el: AbstractInput, validity?: boolean): Promise<void>;
32
32
  setElementDirty(input: AbstractInput): void;
33
33
  isInputPresentOnStage(input: AbstractInput): number;
34
- validate(element: AbstractInput): ErrorObject;
34
+ validate(element: AbstractInput): Promise<ErrorObject>;
35
35
  private mapInputErrors;
36
36
  get(name: keyof T): AbstractInput;
37
37
  getError(inputName: keyof T, errorKey: keyof E): never;
@@ -1,4 +1,13 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.FormGroup = void 0;
4
13
  const form_tokens_1 = require("./form.tokens");
@@ -58,63 +67,65 @@ class FormGroup {
58
67
  return this._valueChanges.asObservable();
59
68
  }
60
69
  updateValueAndValidity() {
61
- this.resetErrors();
62
- const inputs = this.querySelectorAllInputs()
63
- .map(i => {
64
- i.setCustomValidity('');
65
- this.setElementValidity(i);
66
- this.setElementDirty(i);
67
- return i;
68
- })
69
- .map(input => this.validate(input))
70
- .filter(e => e.errors.length);
71
- this.getParentElement().requestUpdate();
72
- return inputs;
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ this.resetErrors();
72
+ const inputs = yield Promise.all(this.querySelectorAllInputs()
73
+ .map((i) => __awaiter(this, void 0, void 0, function* () {
74
+ i.setCustomValidity('');
75
+ this.setElementValidity(i);
76
+ this.setElementDirty(i);
77
+ return yield this.validate(i);
78
+ })));
79
+ this.getParentElement().requestUpdate();
80
+ return inputs.filter(e => e.errors.length);
81
+ });
73
82
  }
74
83
  updateValueAndValidityOnEvent(method) {
75
84
  const self = this;
76
85
  return function (event) {
77
- self.setElementDirty(this);
78
- const selector = `input[name="${this.name}"]`;
79
- const hasMultipleBindings = [
80
- ...self
81
- .getFormElement()
82
- .querySelectorAll(selector).values()
83
- ].length;
84
- let value = this.value;
85
- if (hasMultipleBindings === 1 &&
86
- (this.type === 'checkbox' || this.type === 'radio')) {
87
- value = String(this.checked);
88
- }
89
- if (this.type === 'number') {
90
- value = Number(value);
91
- }
92
- const inputsWithBindings = [
93
- ...(self.getFormElement().querySelectorAll(`input[name="${this.name}"]:checked`)).values()
94
- ];
95
- if (hasMultipleBindings > 1) {
96
- if (!self.options.multi && this.type === 'checkbox') {
97
- value = inputsWithBindings.map(e => e.value);
86
+ return __awaiter(this, void 0, void 0, function* () {
87
+ self.setElementDirty(this);
88
+ const selector = `input[name="${this.name}"]`;
89
+ const hasMultipleBindings = [
90
+ ...self
91
+ .getFormElement()
92
+ .querySelectorAll(selector).values()
93
+ ].length;
94
+ let value = this.value;
95
+ if (hasMultipleBindings === 1 &&
96
+ (this.type === 'checkbox' || this.type === 'radio')) {
97
+ value = String(this.checked);
98
98
  }
99
- if (self.options.multi) {
100
- inputsWithBindings.forEach(el => (el.checked = false));
101
- this.checked = true;
99
+ if (this.type === 'number') {
100
+ value = Number(value);
102
101
  }
103
- }
104
- self.resetErrors();
105
- const isValid = self.applyValidationContext(self.validate(this));
106
- if (self.options.strict) {
107
- if (isValid) {
108
- self.setElementValidity(this, isValid);
109
- self.setValue(this.name, value);
102
+ const inputsWithBindings = [
103
+ ...(self.getFormElement().querySelectorAll(`input[name="${this.name}"]:checked`)).values()
104
+ ];
105
+ if (hasMultipleBindings > 1) {
106
+ if (!self.options.multi && this.type === 'checkbox') {
107
+ value = inputsWithBindings.map(e => e.value);
108
+ }
109
+ if (self.options.multi) {
110
+ inputsWithBindings.forEach(el => (el.checked = false));
111
+ this.checked = true;
112
+ }
110
113
  }
114
+ self.resetErrors();
115
+ const isValid = self.applyValidationContext(yield self.validate(this));
116
+ if (self.options.strict) {
117
+ if (isValid) {
118
+ yield self.setElementValidity(this, isValid);
119
+ self.setValue(this.name, value);
120
+ }
121
+ self.parentElement.requestUpdate();
122
+ return method.call(self.parentElement, event);
123
+ }
124
+ yield self.setElementValidity(this, isValid);
125
+ self.setValue(this.name, value);
111
126
  self.parentElement.requestUpdate();
112
127
  return method.call(self.parentElement, event);
113
- }
114
- self.setElementValidity(this, isValid);
115
- self.setValue(this.name, value);
116
- self.parentElement.requestUpdate();
117
- return method.call(self.parentElement, event);
128
+ });
118
129
  };
119
130
  }
120
131
  applyValidationContext({ errors, element }) {
@@ -166,19 +177,21 @@ class FormGroup {
166
177
  const attr = customAttributes.find(a => a.name.startsWith('#'));
167
178
  this.parentElement[attr.name.replace('#', '')] = el;
168
179
  }
169
- el.addEventListener('blur', () => {
180
+ el.addEventListener('blur', () => __awaiter(this, void 0, void 0, function* () {
170
181
  this.setElementDirty(el);
171
- this.parentElement.requestUpdate();
172
- this.setElementValidity(el);
173
- });
182
+ yield this.parentElement.requestUpdate();
183
+ yield this.setElementValidity(el);
184
+ }));
174
185
  el[strategy] = this.updateValueAndValidityOnEvent(el[strategy]);
175
186
  return el;
176
187
  });
177
188
  }
178
189
  setElementValidity(el, validity) {
179
- const isValid = validity || this.applyValidationContext(this.validate(el));
180
- el['valid'] = isValid;
181
- el['invalid'] = !isValid;
190
+ return __awaiter(this, void 0, void 0, function* () {
191
+ const isValid = validity || this.applyValidationContext(yield this.validate(el));
192
+ el['valid'] = isValid;
193
+ el['invalid'] = !isValid;
194
+ });
182
195
  }
183
196
  setElementDirty(input) {
184
197
  input['touched'] = true;
@@ -195,41 +208,47 @@ class FormGroup {
195
208
  return isInputPresent.length;
196
209
  }
197
210
  validate(element) {
198
- let errors = [];
199
- element.setCustomValidity('');
200
- if (!element.checkValidity()) {
201
- return {
202
- errors: errors.concat(Object.keys(form_tokens_1.InputValidityState)
203
- .map(key => element.validity[key]
204
- ? { key, message: element.validationMessage }
205
- : null)
206
- .filter(i => !!i)),
207
- element
208
- };
209
- }
210
- errors = this.mapInputErrors(element);
211
- if (!errors.length) {
212
- return { errors: [], element };
213
- }
214
- this.setFormValidity(false);
215
- element.setCustomValidity(errors[0].message);
216
- return { element, errors };
217
- }
218
- mapInputErrors(element) {
219
- return (this.validators.get(element.name) || [])
220
- .map(v => {
221
- this.errors[element.name] = this.errors[element.name] || {};
222
- const error = v.bind(this.getParentElement())(element);
223
- if (error && error.key) {
224
- this.errors[element.name][error.key] = error.message;
225
- this.errorMap.set(v, error.key);
226
- return { key: error.key, message: error.message };
211
+ return __awaiter(this, void 0, void 0, function* () {
212
+ let errors = [];
213
+ element.setCustomValidity('');
214
+ if (!element.checkValidity()) {
215
+ return {
216
+ errors: errors.concat(Object.keys(form_tokens_1.InputValidityState)
217
+ .map(key => element.validity[key]
218
+ ? { key, message: element.validationMessage }
219
+ : null)
220
+ .filter(i => !!i)),
221
+ element
222
+ };
227
223
  }
228
- else if (this.errorMap.has(v)) {
229
- delete this.errors[element.name][this.errorMap.get(v)];
224
+ errors = yield this.mapInputErrors(element);
225
+ if (!errors.length) {
226
+ return { errors: [], element };
230
227
  }
231
- })
232
- .filter(i => !!i);
228
+ this.setFormValidity(false);
229
+ element.setCustomValidity(errors[0].message);
230
+ return { element, errors };
231
+ });
232
+ }
233
+ mapInputErrors(element) {
234
+ return __awaiter(this, void 0, void 0, function* () {
235
+ const res = yield Promise.all((this.validators.get(element.name) || []).map((v) => __awaiter(this, void 0, void 0, function* () {
236
+ this.errors[element.name] = this.errors[element.name] || {};
237
+ const error = yield v.bind(this.getParentElement())(element);
238
+ // if (error) {
239
+ // element.focus();
240
+ // }
241
+ if (error && error.key) {
242
+ this.errors[element.name][error.key] = error.message;
243
+ this.errorMap.set(v, error.key);
244
+ return { key: error.key, message: error.message };
245
+ }
246
+ else if (this.errorMap.has(v)) {
247
+ delete this.errors[element.name][this.errorMap.get(v)];
248
+ }
249
+ })));
250
+ return res.filter(i => !!i);
251
+ });
233
252
  }
234
253
  get(name) {
235
254
  return this.inputs.get(name);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rxdi/forms",
3
- "version": "0.7.147",
3
+ "version": "0.7.150",
4
4
  "main": "./dist/index.js",
5
5
  "author": "Kristiyan Tachev",
6
6
  "license": "MIT",
@@ -12,7 +12,7 @@
12
12
  "build": "tsc || true"
13
13
  },
14
14
  "devDependencies": {
15
- "@rxdi/lit-html": "^0.7.146",
15
+ "@rxdi/lit-html": "^0.7.149",
16
16
  "@types/node": "^12.0.10",
17
17
  "rxjs": "^6.5.3",
18
18
  "typescript": "^4.3.5"