@rxdi/forms 0.7.148 → 0.7.151

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
+ }
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);
110
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 }) {
@@ -149,6 +160,7 @@ class FormGroup {
149
160
  return [
150
161
  ...this.querySelectAll('input'),
151
162
  ...this.querySelectAll('select'),
163
+ ...this.querySelectAll('textarea'),
152
164
  ]
153
165
  .filter(el => this.isInputPresentOnStage(el))
154
166
  .filter(el => !!el.name);
@@ -166,19 +178,21 @@ class FormGroup {
166
178
  const attr = customAttributes.find(a => a.name.startsWith('#'));
167
179
  this.parentElement[attr.name.replace('#', '')] = el;
168
180
  }
169
- el.addEventListener('blur', () => {
181
+ el.addEventListener('blur', () => __awaiter(this, void 0, void 0, function* () {
170
182
  this.setElementDirty(el);
171
- this.parentElement.requestUpdate();
172
- this.setElementValidity(el);
173
- });
183
+ yield this.parentElement.requestUpdate();
184
+ yield this.setElementValidity(el);
185
+ }));
174
186
  el[strategy] = this.updateValueAndValidityOnEvent(el[strategy]);
175
187
  return el;
176
188
  });
177
189
  }
178
190
  setElementValidity(el, validity) {
179
- const isValid = validity || this.applyValidationContext(this.validate(el));
180
- el['valid'] = isValid;
181
- el['invalid'] = !isValid;
191
+ return __awaiter(this, void 0, void 0, function* () {
192
+ const isValid = validity || this.applyValidationContext(yield this.validate(el));
193
+ el['valid'] = isValid;
194
+ el['invalid'] = !isValid;
195
+ });
182
196
  }
183
197
  setElementDirty(input) {
184
198
  input['touched'] = true;
@@ -195,44 +209,47 @@ class FormGroup {
195
209
  return isInputPresent.length;
196
210
  }
197
211
  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) {
224
- element.focus();
212
+ return __awaiter(this, void 0, void 0, function* () {
213
+ let errors = [];
214
+ element.setCustomValidity('');
215
+ if (!element.checkValidity()) {
216
+ return {
217
+ errors: errors.concat(Object.keys(form_tokens_1.InputValidityState)
218
+ .map(key => element.validity[key]
219
+ ? { key, message: element.validationMessage }
220
+ : null)
221
+ .filter(i => !!i)),
222
+ element
223
+ };
225
224
  }
226
- if (error && error.key) {
227
- this.errors[element.name][error.key] = error.message;
228
- this.errorMap.set(v, error.key);
229
- return { key: error.key, message: error.message };
225
+ errors = yield this.mapInputErrors(element);
226
+ if (!errors.length) {
227
+ return { errors: [], element };
230
228
  }
231
- else if (this.errorMap.has(v)) {
232
- delete this.errors[element.name][this.errorMap.get(v)];
233
- }
234
- })
235
- .filter(i => !!i);
229
+ this.setFormValidity(false);
230
+ element.setCustomValidity(errors[0].message);
231
+ return { element, errors };
232
+ });
233
+ }
234
+ mapInputErrors(element) {
235
+ return __awaiter(this, void 0, void 0, function* () {
236
+ const res = yield Promise.all((this.validators.get(element.name) || []).map((v) => __awaiter(this, void 0, void 0, function* () {
237
+ this.errors[element.name] = this.errors[element.name] || {};
238
+ const error = yield v.bind(this.getParentElement())(element);
239
+ // if (error) {
240
+ // element.focus();
241
+ // }
242
+ if (error && error.key) {
243
+ this.errors[element.name][error.key] = error.message;
244
+ this.errorMap.set(v, error.key);
245
+ return { key: error.key, message: error.message };
246
+ }
247
+ else if (this.errorMap.has(v)) {
248
+ delete this.errors[element.name][this.errorMap.get(v)];
249
+ }
250
+ })));
251
+ return res.filter(i => !!i);
252
+ });
236
253
  }
237
254
  get(name) {
238
255
  return this.inputs.get(name);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rxdi/forms",
3
- "version": "0.7.148",
3
+ "version": "0.7.151",
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.147",
15
+ "@rxdi/lit-html": "^0.7.150",
16
16
  "@types/node": "^12.0.10",
17
17
  "rxjs": "^6.5.3",
18
18
  "typescript": "^4.3.5"