@italia/globals 0.1.0-alpha.1 → 1.0.0-alpha.10

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.
Files changed (58) hide show
  1. package/custom-elements.json +2122 -179
  2. package/dist/src/base-component/base-component.d.ts +28 -13
  3. package/dist/src/base-component/base-component.d.ts.map +1 -1
  4. package/dist/src/base-component/base-component.js +27 -17
  5. package/dist/src/base-component/base-component.js.map +1 -1
  6. package/dist/src/controllers/aria-keyboard-accordion-controller.d.ts +15 -0
  7. package/dist/src/controllers/aria-keyboard-accordion-controller.d.ts.map +1 -0
  8. package/dist/src/controllers/aria-keyboard-accordion-controller.js +52 -0
  9. package/dist/src/controllers/aria-keyboard-accordion-controller.js.map +1 -0
  10. package/dist/src/controllers/aria-keyboard-list-controller.d.ts +18 -0
  11. package/dist/src/controllers/aria-keyboard-list-controller.d.ts.map +1 -0
  12. package/dist/src/controllers/aria-keyboard-list-controller.js +58 -0
  13. package/dist/src/controllers/aria-keyboard-list-controller.js.map +1 -0
  14. package/dist/src/controllers/collapse-controller.d.ts +12 -0
  15. package/dist/src/controllers/collapse-controller.d.ts.map +1 -0
  16. package/dist/src/controllers/collapse-controller.js +95 -0
  17. package/dist/src/controllers/collapse-controller.js.map +1 -0
  18. package/dist/src/controllers/focus-trap-controller.d.ts +92 -0
  19. package/dist/src/controllers/focus-trap-controller.d.ts.map +1 -0
  20. package/dist/src/controllers/focus-trap-controller.js +497 -0
  21. package/dist/src/controllers/focus-trap-controller.js.map +1 -0
  22. package/dist/src/controllers/roving-tabindex-controller.d.ts +92 -0
  23. package/dist/src/controllers/roving-tabindex-controller.d.ts.map +1 -0
  24. package/dist/src/controllers/roving-tabindex-controller.js +206 -0
  25. package/dist/src/controllers/roving-tabindex-controller.js.map +1 -0
  26. package/dist/src/form/form-control.d.ts +61 -0
  27. package/dist/src/form/form-control.d.ts.map +1 -0
  28. package/dist/src/form/form-control.js +259 -0
  29. package/dist/src/form/form-control.js.map +1 -0
  30. package/dist/src/form/form-controller.d.ts +68 -0
  31. package/dist/src/form/form-controller.d.ts.map +1 -0
  32. package/dist/src/form/form-controller.js +408 -0
  33. package/dist/src/form/form-controller.js.map +1 -0
  34. package/dist/src/form/locales/it.d.ts +4 -0
  35. package/dist/src/form/locales/it.d.ts.map +1 -0
  36. package/dist/src/form/locales/it.js +12 -0
  37. package/dist/src/form/locales/it.js.map +1 -0
  38. package/dist/src/index.d.ts +20 -4
  39. package/dist/src/index.d.ts.map +1 -1
  40. package/dist/src/index.js +18 -4
  41. package/dist/src/index.js.map +1 -1
  42. package/dist/src/mixins/validity.d.ts +19 -19
  43. package/dist/src/mixins/validity.d.ts.map +1 -1
  44. package/dist/src/mixins/validity.js +6 -68
  45. package/dist/src/mixins/validity.js.map +1 -1
  46. package/dist/src/stories/formControlReusableStories.d.ts +19 -0
  47. package/dist/src/stories/formControlReusableStories.d.ts.map +1 -0
  48. package/dist/src/stories/formControlReusableStories.js +64 -0
  49. package/dist/src/stories/formControlReusableStories.js.map +1 -0
  50. package/dist/src/window-manager.d.ts +33 -0
  51. package/dist/src/window-manager.d.ts.map +1 -0
  52. package/dist/src/window-manager.js +78 -0
  53. package/dist/src/window-manager.js.map +1 -0
  54. package/package.json +10 -8
  55. package/dist/src/mixins/form.d.ts +0 -363
  56. package/dist/src/mixins/form.d.ts.map +0 -1
  57. package/dist/src/mixins/form.js +0 -36
  58. package/dist/src/mixins/form.js.map +0 -1
@@ -0,0 +1,259 @@
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { property, query, state } from 'lit/decorators.js';
3
+ import { registerTranslation } from '@italia/i18n';
4
+ import { BaseLocalizedComponent } from '../base-component/base-component.js';
5
+ import { FormControlController } from './form-controller.js';
6
+ import it from './locales/it.js';
7
+ registerTranslation(it);
8
+ export class FormControl extends BaseLocalizedComponent {
9
+ constructor() {
10
+ super(...arguments);
11
+ this.formControlController = new FormControlController(this, {
12
+ assumeInteractionOn: ['it-input', 'it-blur', 'it-change'],
13
+ });
14
+ // TODO: verificare se serve davvero con il fatto che usiamo form-controller
15
+ // static formAssociated = true;
16
+ // @property()
17
+ // internals = this.attachInternals();
18
+ this._touched = false;
19
+ /** The name of the input, submitted as a name/value pair with form data. */
20
+ this.name = '';
21
+ /** The current value of the input, submitted as a name/value pair with form data. */
22
+ this.value = '';
23
+ /** If the input is disabled. */
24
+ this.disabled = false;
25
+ /**
26
+ * By default, form controls are associated with the nearest containing `<form>` element. This attribute allows you
27
+ * to place the form control outside of a form and associate it with the form that has this `id`. The form must be in
28
+ * the same document or shadow root for this to work.
29
+ */
30
+ this.form = '';
31
+ /** If you implement your custom validation and you won't to trigger default validation */
32
+ this.customValidation = false;
33
+ /** If your input is invalid from your custom validition, set this attribute with message validation */
34
+ this.validationText = '';
35
+ /**
36
+ * Specifies the granularity that the value must adhere to, or the special value `any` which means no stepping is
37
+ * implied, allowing any numeric value. Only applies to date and number input types.
38
+ */
39
+ this.step = 'any';
40
+ /** The input's minimum length. */
41
+ this.minlength = -1;
42
+ /** The input's maximum length. */
43
+ this.maxlength = -1;
44
+ /** If the input is required. */
45
+ this.required = false;
46
+ /* For grouped input, like checkbox-group */
47
+ this.isInGroup = false;
48
+ this.validationMessage = '';
49
+ }
50
+ /** Gets the validity state object */
51
+ get validity() {
52
+ return this.inputElement?.validity;
53
+ }
54
+ /** Gets the associated form, if one exists. */
55
+ getForm() {
56
+ return this.formControlController.getForm();
57
+ }
58
+ // Form validation methods
59
+ checkValidity() {
60
+ const inputValid = this.inputElement?.checkValidity() ?? true; // this.inputElement.checkValidity() è la validazione del browser
61
+ return inputValid;
62
+ }
63
+ /** Checks for validity and shows the browser's validation message if the control is invalid. */
64
+ reportValidity() {
65
+ // const ret = this.inputElement.checkValidity();
66
+ const ret = this.checkValidity(); // chiama la checkValidity, che se è stata overridata chiama quella
67
+ this.handleValidationMessages();
68
+ return ret; // this.inputElement.reportValidity();
69
+ }
70
+ /** Sets a custom validation message. Pass an empty string to restore validity. */
71
+ setCustomValidity(message) {
72
+ this.inputElement.setCustomValidity(message);
73
+ this.validationMessage = this.inputElement.validationMessage;
74
+ this.formControlController.updateValidity();
75
+ }
76
+ // Handlers
77
+ _handleReady() {
78
+ requestAnimationFrame(() => {
79
+ this.dispatchEvent(new CustomEvent('it-input-ready', { bubbles: true, detail: { el: this.inputElement } }));
80
+ });
81
+ }
82
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
83
+ _handleInput(e) {
84
+ this.handleValidationMessages();
85
+ this.dispatchEvent(new CustomEvent('it-input', {
86
+ detail: { value: this.inputElement.value, el: this.inputElement },
87
+ bubbles: true,
88
+ composed: true,
89
+ }));
90
+ }
91
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
92
+ _handleBlur(e) {
93
+ this._touched = true;
94
+ this.handleValidationMessages();
95
+ this.dispatchEvent(new FocusEvent('it-blur', { bubbles: true, composed: true }));
96
+ }
97
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
98
+ _handleFocus(e) {
99
+ this.dispatchEvent(new FocusEvent('it-focus', { bubbles: true, composed: true }));
100
+ }
101
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
102
+ _handleClick(e) {
103
+ this.dispatchEvent(new MouseEvent('it-click', { bubbles: true, composed: true }));
104
+ }
105
+ /*
106
+ Override default browser validation messages
107
+ */
108
+ handleValidationMessages() {
109
+ if (!this.customValidation) {
110
+ const _v = this.inputElement.validity;
111
+ const isRequiredHandledByGroup = this.isInGroup === true;
112
+ if (_v.valueMissing && !isRequiredHandledByGroup) {
113
+ this.setCustomValidity(this.$t('validityRequired'));
114
+ }
115
+ else if (_v.patternMismatch) {
116
+ this.setCustomValidity(this.$t('validityPattern'));
117
+ }
118
+ else if (_v.tooShort) {
119
+ this.setCustomValidity(this.$t('validityMinlength').replace('{minlength}', this.minlength.toString()));
120
+ }
121
+ else if (_v.tooLong) {
122
+ this.setCustomValidity(this.$t('validityMaxlength').replace('{maxlength}', this.maxlength.toString()));
123
+ }
124
+ else {
125
+ /* nothing. Usa il messaggio di errore della validazione
126
+ di default del browser per altri errori di validità come:
127
+ - typeMismatch
128
+ - rangeUnderflow
129
+ - rangeOverflow
130
+ - stepMismatch
131
+ - badInput */
132
+ const otherConstraintErrors = _v.typeMismatch || _v.rangeUnderflow || _v.rangeOverflow || _v.stepMismatch || _v.badInput;
133
+ if (!otherConstraintErrors) {
134
+ this.setCustomValidity('');
135
+ }
136
+ }
137
+ }
138
+ this.validationMessage = this.inputElement.validationMessage;
139
+ }
140
+ _handleInvalid(event) {
141
+ this.formControlController.setValidity(false);
142
+ this.formControlController.emitInvalidEvent(event);
143
+ }
144
+ _handleChange(e) {
145
+ const target = e.target;
146
+ let value;
147
+ if (target instanceof HTMLInputElement) {
148
+ switch (target.type) {
149
+ case 'checkbox':
150
+ case 'radio':
151
+ value = target.checked;
152
+ break;
153
+ case 'file':
154
+ value = target.files; // FileList
155
+ break;
156
+ default:
157
+ value = target.value;
158
+ }
159
+ }
160
+ else if (target instanceof HTMLSelectElement) {
161
+ if (target.multiple) {
162
+ value = Array.from(target.selectedOptions).map((o) => o.value);
163
+ }
164
+ else {
165
+ value = target.value;
166
+ }
167
+ }
168
+ else {
169
+ // textarea o altri input con value
170
+ value = target.value;
171
+ }
172
+ this.dispatchEvent(new CustomEvent('it-change', {
173
+ detail: { value, el: target },
174
+ bubbles: true,
175
+ composed: true,
176
+ }));
177
+ }
178
+ updated(changedProperties) {
179
+ super.updated?.(changedProperties);
180
+ if (this.customValidation) {
181
+ this.setCustomValidity(this.validationText ?? '');
182
+ }
183
+ else if (this.formControlController.userInteracted()) {
184
+ this.formControlController.updateValidity();
185
+ }
186
+ }
187
+ }
188
+ __decorate([
189
+ state(),
190
+ __metadata("design:type", Object)
191
+ ], FormControl.prototype, "_touched", void 0);
192
+ __decorate([
193
+ query('.it-form__control'),
194
+ __metadata("design:type", HTMLInputElement)
195
+ ], FormControl.prototype, "inputElement", void 0);
196
+ __decorate([
197
+ property({ type: String, reflect: true }) // from FormControl
198
+ ,
199
+ __metadata("design:type", Object)
200
+ ], FormControl.prototype, "name", void 0);
201
+ __decorate([
202
+ property({ reflect: true }),
203
+ __metadata("design:type", Object)
204
+ ], FormControl.prototype, "value", void 0);
205
+ __decorate([
206
+ property({ type: Boolean, reflect: true }) // from FormControl
207
+ ,
208
+ __metadata("design:type", Object)
209
+ ], FormControl.prototype, "disabled", void 0);
210
+ __decorate([
211
+ property({ reflect: true, type: String }),
212
+ __metadata("design:type", Object)
213
+ ], FormControl.prototype, "form", void 0);
214
+ __decorate([
215
+ property({ type: Boolean, reflect: true, attribute: 'custom-validation' }),
216
+ __metadata("design:type", Object)
217
+ ], FormControl.prototype, "customValidation", void 0);
218
+ __decorate([
219
+ property({ attribute: 'validity-message', reflect: true }),
220
+ __metadata("design:type", String)
221
+ ], FormControl.prototype, "validationText", void 0);
222
+ __decorate([
223
+ property(),
224
+ __metadata("design:type", String)
225
+ ], FormControl.prototype, "pattern", void 0);
226
+ __decorate([
227
+ property(),
228
+ __metadata("design:type", Object)
229
+ ], FormControl.prototype, "min", void 0);
230
+ __decorate([
231
+ property(),
232
+ __metadata("design:type", Object)
233
+ ], FormControl.prototype, "max", void 0);
234
+ __decorate([
235
+ property(),
236
+ __metadata("design:type", Object)
237
+ ], FormControl.prototype, "step", void 0);
238
+ __decorate([
239
+ property({ type: Number }),
240
+ __metadata("design:type", Object)
241
+ ], FormControl.prototype, "minlength", void 0);
242
+ __decorate([
243
+ property({ type: Number }),
244
+ __metadata("design:type", Object)
245
+ ], FormControl.prototype, "maxlength", void 0);
246
+ __decorate([
247
+ property({ type: Boolean, reflect: true }) // from FormControl
248
+ ,
249
+ __metadata("design:type", Object)
250
+ ], FormControl.prototype, "required", void 0);
251
+ __decorate([
252
+ property({ type: Boolean }),
253
+ __metadata("design:type", Object)
254
+ ], FormControl.prototype, "isInGroup", void 0);
255
+ __decorate([
256
+ state(),
257
+ __metadata("design:type", Object)
258
+ ], FormControl.prototype, "validationMessage", void 0);
259
+ //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js';\nimport { registerTranslation } from '@italia/i18n';\nimport { BaseLocalizedComponent } from '../base-component/base-component.js';\nimport { FormControlController } from './form-controller.js';\nimport it from './locales/it.js';\n\nregisterTranslation(it);\n\nexport class FormControl extends BaseLocalizedComponent {\n protected readonly formControlController = new FormControlController(this, {\n assumeInteractionOn: ['it-input', 'it-blur', 'it-change'],\n });\n\n // TODO: verificare se serve davvero con il fatto che usiamo form-controller\n // static formAssociated = true;\n\n // @property()\n // internals = this.attachInternals();\n\n @state()\n _touched = false;\n\n @query('.it-form__control')\n inputElement!: HTMLInputElement; // from FormControl\n\n /** The name of the input, submitted as a name/value pair with form data. */\n @property({ type: String, reflect: true }) // from FormControl\n name = '';\n\n /** The current value of the input, submitted as a name/value pair with form data. */\n @property({ reflect: true })\n value = '';\n\n /** If the input is disabled. */\n @property({ type: Boolean, reflect: true }) // from FormControl\n disabled = false;\n\n /**\n * By default, form controls are associated with the nearest containing `<form>` element. This attribute allows you\n * to place the form control outside of a form and associate it with the form that has this `id`. The form must be in\n * the same document or shadow root for this to work.\n */\n @property({ reflect: true, type: String }) form = '';\n\n /** If you implement your custom validation and you won't to trigger default validation */\n @property({ type: Boolean, reflect: true, attribute: 'custom-validation' })\n customValidation = false;\n\n /** If your input is invalid from your custom validition, set this attribute with message validation */\n @property({ attribute: 'validity-message', reflect: true })\n validationText: string = '';\n\n /** Pattern the `value` must match to be valid */\n @property()\n pattern?: string;\n\n /** The input's minimum value. Only applies to date and number input types. */\n @property()\n min: number | string | Date | undefined;\n\n /** The input's maximum value. Only applies to date and number input types. */\n @property()\n max: number | string | Date | undefined;\n\n /**\n * Specifies the granularity that the value must adhere to, or the special value `any` which means no stepping is\n * implied, allowing any numeric value. Only applies to date and number input types.\n */\n @property()\n step: number | 'any' = 'any';\n\n /** The input's minimum length. */\n @property({ type: Number })\n minlength = -1;\n\n /** The input's maximum length. */\n @property({ type: Number })\n maxlength = -1;\n\n /** If the input is required. */\n @property({ type: Boolean, reflect: true }) // from FormControl\n required = false;\n\n /* For grouped input, like checkbox-group */\n @property({ type: Boolean })\n protected isInGroup = false;\n\n /** Gets the validity state object */\n public get validity(): ValidityState {\n return this.inputElement?.validity;\n }\n\n @state()\n public validationMessage = '';\n\n /** Gets the associated form, if one exists. */\n public getForm(): HTMLFormElement | null {\n return this.formControlController.getForm();\n }\n\n // Form validation methods\n public checkValidity(): boolean {\n const inputValid = this.inputElement?.checkValidity() ?? true; // this.inputElement.checkValidity() è la validazione del browser\n return inputValid;\n }\n\n /** Checks for validity and shows the browser's validation message if the control is invalid. */\n public reportValidity() {\n // const ret = this.inputElement.checkValidity();\n const ret = this.checkValidity(); // chiama la checkValidity, che se è stata overridata chiama quella\n this.handleValidationMessages();\n return ret; // this.inputElement.reportValidity();\n }\n\n /** Sets a custom validation message. Pass an empty string to restore validity. */\n public setCustomValidity(message: string) {\n this.inputElement.setCustomValidity(message);\n this.validationMessage = this.inputElement.validationMessage;\n this.formControlController.updateValidity();\n }\n\n // Handlers\n protected _handleReady() {\n requestAnimationFrame(() => {\n this.dispatchEvent(new CustomEvent('it-input-ready', { bubbles: true, detail: { el: this.inputElement } }));\n });\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n protected _handleInput(e: Event) {\n this.handleValidationMessages();\n this.dispatchEvent(\n new CustomEvent('it-input', {\n detail: { value: this.inputElement.value, el: this.inputElement },\n bubbles: true,\n composed: true,\n }),\n );\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n protected _handleBlur(e: Event) {\n this._touched = true;\n this.handleValidationMessages();\n this.dispatchEvent(new FocusEvent('it-blur', { bubbles: true, composed: true }));\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n protected _handleFocus(e: Event) {\n this.dispatchEvent(new FocusEvent('it-focus', { bubbles: true, composed: true }));\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n protected _handleClick(e: Event) {\n this.dispatchEvent(new MouseEvent('it-click', { bubbles: true, composed: true }));\n }\n\n /*\n Override default browser validation messages\n */\n protected handleValidationMessages() {\n if (!this.customValidation) {\n const _v = this.inputElement.validity;\n\n const isRequiredHandledByGroup = this.isInGroup === true;\n\n if (_v.valueMissing && !isRequiredHandledByGroup) {\n this.setCustomValidity(this.$t('validityRequired'));\n } else if (_v.patternMismatch) {\n this.setCustomValidity(this.$t('validityPattern'));\n } else if (_v.tooShort) {\n this.setCustomValidity(this.$t('validityMinlength').replace('{minlength}', this.minlength.toString()));\n } else if (_v.tooLong) {\n this.setCustomValidity(this.$t('validityMaxlength').replace('{maxlength}', this.maxlength.toString()));\n } else {\n /* nothing. Usa il messaggio di errore della validazione\n di default del browser per altri errori di validità come:\n - typeMismatch\n - rangeUnderflow\n - rangeOverflow\n - stepMismatch\n - badInput */\n\n const otherConstraintErrors =\n _v.typeMismatch || _v.rangeUnderflow || _v.rangeOverflow || _v.stepMismatch || _v.badInput;\n\n if (!otherConstraintErrors) {\n this.setCustomValidity('');\n }\n }\n }\n\n this.validationMessage = this.inputElement.validationMessage;\n }\n\n protected _handleInvalid(event: Event) {\n this.formControlController.setValidity(false);\n this.formControlController.emitInvalidEvent(event);\n }\n\n protected _handleChange(e: Event) {\n const target = e.target as HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;\n let value: unknown;\n\n if (target instanceof HTMLInputElement) {\n switch (target.type) {\n case 'checkbox':\n case 'radio':\n value = target.checked;\n break;\n case 'file':\n value = target.files; // FileList\n break;\n default:\n value = target.value;\n }\n } else if (target instanceof HTMLSelectElement) {\n if (target.multiple) {\n value = Array.from(target.selectedOptions).map((o) => o.value);\n } else {\n value = target.value;\n }\n } else {\n // textarea o altri input con value\n value = (target as any).value;\n }\n\n this.dispatchEvent(\n new CustomEvent('it-change', {\n detail: { value, el: target },\n bubbles: true,\n composed: true,\n }),\n );\n }\n\n override updated(changedProperties: Map<string | number | symbol, unknown>) {\n super.updated?.(changedProperties);\n\n if (this.customValidation) {\n this.setCustomValidity(this.validationText ?? '');\n } else if (this.formControlController.userInteracted()) {\n this.formControlController.updateValidity();\n }\n }\n}\n"]}
@@ -0,0 +1,68 @@
1
+ import type { ReactiveController, ReactiveControllerHost } from 'lit';
2
+ import { FormControl } from './form-control.js';
3
+ export declare const formCollections: WeakMap<HTMLFormElement, Set<FormControl>>;
4
+ export interface FormControlControllerOptions {
5
+ /** A function that returns the form containing the form control. */
6
+ form: (input: FormControl) => HTMLFormElement | null;
7
+ /** A function that returns the form control's name, which will be submitted with the form data. */
8
+ name: (input: FormControl) => string;
9
+ /** A function that returns the form control's current value. */
10
+ value: (input: FormControl) => unknown | unknown[];
11
+ /** A function that returns the form control's current disabled state. If disabled, the value won't be submitted. */
12
+ disabled: (input: FormControl) => boolean;
13
+ reportValidity: (input: FormControl) => boolean;
14
+ checkValidity: (input: FormControl) => boolean;
15
+ /** A function that sets the form control's value */
16
+ setValue: (input: FormControl, value: unknown) => void;
17
+ /**
18
+ * An array of event names to listen to. When all events in the list are emitted, the control will receive validity
19
+ * states such as user-valid and user-invalid.user interacted validity states. */
20
+ assumeInteractionOn: string[];
21
+ }
22
+ /** A reactive controller to allow form controls to participate in form submission, validation, etc. */
23
+ export declare class FormControlController implements ReactiveController {
24
+ host: FormControl & ReactiveControllerHost;
25
+ form?: HTMLFormElement | null;
26
+ options: FormControlControllerOptions;
27
+ submittedOnce: boolean;
28
+ constructor(host: ReactiveControllerHost & FormControl, options?: Partial<FormControlControllerOptions>);
29
+ hostConnected(): void;
30
+ hostDisconnected(): void;
31
+ hostUpdated(): void;
32
+ private attachForm;
33
+ private detachForm;
34
+ private handleFormData;
35
+ private handleFormSubmit;
36
+ private handleFormReset;
37
+ private handleInteraction;
38
+ private checkFormValidity;
39
+ private reportFormValidity;
40
+ private setUserInteracted;
41
+ private doAction;
42
+ /** Returns the associated `<form>` element, if one exists. */
43
+ getForm(): HTMLFormElement | null;
44
+ /** Resets the form, restoring all the control to their default value */
45
+ reset(submitter?: HTMLInputElement | any): void;
46
+ /** Submits the form, triggering validation and form data injection. */
47
+ submit(submitter?: HTMLInputElement | any): void;
48
+ /**
49
+ * Synchronously sets the form control's validity. Call this when you know the future validity but need to update
50
+ * the host element immediately, i.e. before Lit updates the component in the next update.
51
+ */
52
+ setValidity(isValid: boolean): void;
53
+ userInteracted(): boolean;
54
+ /**
55
+ * Updates the form control's validity based on the current value of `host.validity.valid`. Call this when anything
56
+ * that affects constraint validation changes so the component receives the correct validity states.
57
+ */
58
+ updateValidity(): void;
59
+ /**
60
+ * Dispatches a non-bubbling, cancelable custom event of type `it-invalid`.
61
+ * If the `it-invalid` event will be cancelled then the original `invalid`
62
+ * event (which may have been passed as argument) will also be cancelled.
63
+ * If no original `invalid` event has been passed then the `it-invalid`
64
+ * event will be cancelled before being dispatched.
65
+ */
66
+ emitInvalidEvent(originalInvalidEvent?: Event): void;
67
+ }
68
+ //# sourceMappingURL=form-controller.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"form-controller.d.ts","sourceRoot":"","sources":["../../../src/form/form-controller.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,KAAK,CAAC;AACtE,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAOhD,eAAO,MAAM,eAAe,EAAE,OAAO,CAAC,eAAe,EAAE,GAAG,CAAC,WAAW,CAAC,CAAiB,CAAC;AAoBzF,MAAM,WAAW,4BAA4B;IAC3C,oEAAoE;IACpE,IAAI,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,eAAe,GAAG,IAAI,CAAC;IACrD,mGAAmG;IACnG,IAAI,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,MAAM,CAAC;IACrC,gEAAgE;IAChE,KAAK,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,OAAO,GAAG,OAAO,EAAE,CAAC;IACnD,oHAAoH;IACpH,QAAQ,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,OAAO,CAAC;IAK1C,cAAc,EAAE,CAAC,KAAK,EAA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