@exmg/exm-form 1.0.5 → 1.0.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.
package/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { ExmForm } from './src/exm-form.js';
2
+ export { ExmFormValidateMixin } from './src/exm-form-validate-mixin.js';
2
3
  export { ExmFormBase, serializeForm } from './src/exm-form-base.js';
3
4
  export { style as formStyles } from './src/styles/exm-form-css.js';
4
5
  export { style as formBaseStyles } from './src/styles/exm-form-base-css.js';
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export { ExmForm } from './src/exm-form.js';
2
+ export { ExmFormValidateMixin } from './src/exm-form-validate-mixin.js';
2
3
  export { ExmFormBase, serializeForm } from './src/exm-form-base.js';
3
4
  export { style as formStyles } from './src/styles/exm-form-css.js';
4
5
  export { style as formBaseStyles } from './src/styles/exm-form-base-css.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exmg/exm-form",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "module": "index.js",
@@ -43,5 +43,5 @@
43
43
  "publishConfig": {
44
44
  "access": "public"
45
45
  },
46
- "gitHead": "ef887aabf2d9e8e0572e850f3db7c80a697f0edf"
46
+ "gitHead": "3a7e80814357f7b89d46a44cc821834a9131f25b"
47
47
  }
@@ -4,7 +4,8 @@ import '@material/web/divider/divider.js';
4
4
  import { ExmgElement } from '@exmg/lit-base';
5
5
  export declare const CLOSE_ACTION = "close";
6
6
  export declare const serializeForm: (form: any) => {};
7
- export declare class ExmFormBase extends ExmgElement {
7
+ declare const ExmFormBase_base: import("./exm-form-validate-mixin.js").Constructor<import("./exm-form-validate-mixin.js").FormValidateMixinInterface> & typeof ExmgElement;
8
+ export declare class ExmFormBase extends ExmFormBase_base {
8
9
  /**
9
10
  * Submit button copy
10
11
  */
@@ -17,19 +18,12 @@ export declare class ExmFormBase extends ExmgElement {
17
18
  * Internall used to show button spinner.
18
19
  */
19
20
  submitting: boolean;
20
- formValid: boolean;
21
21
  boundHandleBlur?: (e: Event) => void;
22
22
  boundHandleKeyup?: (e: Event) => void;
23
23
  hasAsideContent: boolean;
24
24
  errorMessage?: string | null;
25
- protected getForm(): HTMLFormElement | null;
26
25
  reset(): void;
27
26
  submit(): void;
28
- protected _handleKeyup(): void;
29
- protected _handleBlur(e: Event): void;
30
- protected firstUpdated(): Promise<void>;
31
- disconnectedCallback(): void;
32
- protected _checkFormValidity(): void;
33
27
  /**
34
28
  * Action method that needs to be implemented
35
29
  * @param {CustomEvent} e
@@ -46,3 +40,4 @@ export declare class ExmFormBase extends ExmgElement {
46
40
  protected renderContainer(): import("lit-html").TemplateResult<1>;
47
41
  protected render(): import("lit-html").TemplateResult<1>;
48
42
  }
43
+ export {};
@@ -6,6 +6,7 @@ import '@material/web/button/text-button.js';
6
6
  import '@material/web/divider/divider.js';
7
7
  import { ExmgElement } from '@exmg/lit-base';
8
8
  import { classMap } from 'lit/directives/class-map.js';
9
+ import { ExmFormValidateMixin } from './exm-form-validate-mixin.js';
9
10
  export const CLOSE_ACTION = 'close';
10
11
  export const serializeForm = (form) => {
11
12
  const obj = {};
@@ -72,7 +73,7 @@ export const serializeForm = (form) => {
72
73
  }
73
74
  return obj;
74
75
  };
75
- export class ExmFormBase extends ExmgElement {
76
+ export class ExmFormBase extends ExmFormValidateMixin(ExmgElement) {
76
77
  constructor() {
77
78
  super(...arguments);
78
79
  /**
@@ -87,12 +88,8 @@ export class ExmFormBase extends ExmgElement {
87
88
  * Internall used to show button spinner.
88
89
  */
89
90
  this.submitting = false;
90
- this.formValid = false;
91
91
  this.hasAsideContent = false;
92
92
  }
93
- getForm() {
94
- return this.shadowRoot.querySelector('form');
95
- }
96
93
  reset() {
97
94
  const form = this.getForm();
98
95
  form === null || form === void 0 ? void 0 : form.reset();
@@ -100,51 +97,11 @@ export class ExmFormBase extends ExmgElement {
100
97
  submit() {
101
98
  this.handleSubmit();
102
99
  }
103
- _handleKeyup() {
104
- this._checkFormValidity();
105
- }
106
- _handleBlur(e) {
107
- // @ts-ignore
108
- typeof e.target.reportValidity === 'function' && e.target.reportValidity();
109
- this._checkFormValidity();
110
- }
111
- async firstUpdated() {
112
- const form = this.getForm();
113
- this.boundHandleBlur = this._handleBlur.bind(this);
114
- form.addEventListener('blur', this.boundHandleBlur, true);
115
- this.boundHandleKeyup = this._handleKeyup.bind(this);
116
- form.addEventListener('keyup', this.boundHandleKeyup, true);
117
- await this.updateComplete;
118
- this._checkFormValidity();
119
- }
120
- disconnectedCallback() {
121
- const form = this.getForm();
122
- this.boundHandleBlur && form.removeEventListener('blur', this.boundHandleBlur, true);
123
- this.boundHandleKeyup && form.removeEventListener('keyup', this.boundHandleKeyup, true);
124
- super.disconnectedCallback();
125
- }
126
- _checkFormValidity() {
127
- const form = this.getForm();
128
- const formElements = form === null || form === void 0 ? void 0 : form.elements;
129
- let allValid = true;
130
- for (const el of formElements || []) {
131
- let isValid = true;
132
- // @ts-ignore
133
- if (typeof el.reportValidity === 'function') {
134
- // @ts-ignore
135
- isValid = el.checkValidity();
136
- }
137
- if (!isValid) {
138
- allValid = false;
139
- }
140
- }
141
- this.formValid = allValid;
142
- }
143
100
  async handleSubmit() {
144
101
  this.errorMessage = null;
145
102
  const form = this.getForm();
146
103
  // Check form validity
147
- this._checkFormValidity();
104
+ this.checkFormValidity();
148
105
  // Return when there are invalid fields
149
106
  if (!this.formValid) {
150
107
  return;
@@ -228,9 +185,6 @@ __decorate([
228
185
  __decorate([
229
186
  property({ type: Boolean })
230
187
  ], ExmFormBase.prototype, "submitting", void 0);
231
- __decorate([
232
- property({ type: Boolean })
233
- ], ExmFormBase.prototype, "formValid", void 0);
234
188
  __decorate([
235
189
  property({ type: Boolean })
236
190
  ], ExmFormBase.prototype, "hasAsideContent", void 0);
@@ -0,0 +1,10 @@
1
+ import { LitElement } from 'lit';
2
+ export type Constructor<T> = new (...args: any[]) => T;
3
+ export declare abstract class FormValidateMixinClass extends LitElement {
4
+ }
5
+ export declare class FormValidateMixinInterface {
6
+ formValid: boolean;
7
+ getForm(): HTMLFormElement | null;
8
+ checkFormValidity(): void;
9
+ }
10
+ export declare const ExmFormValidateMixin: <T extends Constructor<LitElement & FormValidateMixinClass>>(superClass: T) => Constructor<FormValidateMixinInterface> & T;
@@ -0,0 +1,60 @@
1
+ import { __decorate } from "tslib";
2
+ import { LitElement } from 'lit';
3
+ import { async, debounce } from '@exmg/lit-base/index.js';
4
+ import { state } from 'lit/decorators.js';
5
+ export class FormValidateMixinClass extends LitElement {
6
+ }
7
+ export const ExmFormValidateMixin = (superClass) => {
8
+ class FormvalidateMixinElement extends superClass {
9
+ constructor() {
10
+ super(...arguments);
11
+ this.formValid = false;
12
+ }
13
+ getForm() {
14
+ return this.shadowRoot.querySelector('form');
15
+ }
16
+ checkFormValidity() {
17
+ const form = this.getForm();
18
+ const formElements = form === null || form === void 0 ? void 0 : form.elements;
19
+ let allValid = true;
20
+ for (const el of formElements || []) {
21
+ let isValid = true;
22
+ // @ts-ignore
23
+ if (typeof el.reportValidity === 'function') {
24
+ // @ts-ignore
25
+ isValid = el.checkValidity();
26
+ }
27
+ if (!isValid) {
28
+ allValid = false;
29
+ }
30
+ }
31
+ this.formValid = allValid;
32
+ }
33
+ _handleInputChange(e) {
34
+ const target = e.target;
35
+ // Only check validation every 200ms max
36
+ this._debouncer = debounce.Debouncer.debounce(this._debouncer, async.timeOut.after(200), () => {
37
+ // @ts-ignore
38
+ typeof target.reportValidity === 'function' && target.reportValidity();
39
+ this.checkFormValidity();
40
+ });
41
+ }
42
+ firstUpdated() {
43
+ const form = this.getForm();
44
+ this.boundHandleChange = this._handleInputChange.bind(this);
45
+ form.addEventListener('keyup', this.boundHandleChange, true);
46
+ form.addEventListener('input', this.boundHandleChange, true);
47
+ }
48
+ disconnectedCallback() {
49
+ const form = this.getForm();
50
+ this.boundHandleChange && form.addEventListener('keyup', this.boundHandleChange, true);
51
+ this.boundHandleChange && form.addEventListener('input', this.boundHandleChange, true);
52
+ super.disconnectedCallback();
53
+ }
54
+ }
55
+ __decorate([
56
+ state()
57
+ ], FormvalidateMixinElement.prototype, "formValid", void 0);
58
+ return FormvalidateMixinElement;
59
+ };
60
+ //# sourceMappingURL=exm-form-validate-mixin.js.map