|
1 | 1 | import { defineElement } from '@umbraco-ui/uui-base/lib/registration';
|
| 2 | +import { html, LitElement } from 'lit'; |
2 | 3 |
|
3 | 4 | /**
|
4 | 5 | * @element uui-form
|
| 6 | + * @description uui-form must wrap a native form element. This ensures the User Experiences of the form fits with the Umbraco standard. |
5 | 7 | */
|
6 |
| -@defineElement('uui-form', { extends: 'form' }) |
7 |
| -export class UUIFormElement extends HTMLFormElement { |
8 |
| - constructor() { |
9 |
| - super(); |
10 |
| - this.setAttribute('novalidate', ''); |
11 |
| - this.addEventListener('submit', this._onSubmit); |
12 |
| - this.addEventListener('reset', this._onReset); |
| 8 | +@defineElement('uui-form') |
| 9 | +export class UUIFormElement extends LitElement { |
| 10 | + private _formElement: HTMLFormElement | null = null; |
| 11 | + |
| 12 | + public getFormElement(): HTMLFormElement | null { |
| 13 | + return this._formElement; |
13 | 14 | }
|
14 | 15 |
|
15 |
| - private _onSubmit(event: Event) { |
16 |
| - event.preventDefault(); |
| 16 | + private _onSlotChanged(event: Event) { |
| 17 | + if (this._formElement) { |
| 18 | + this._formElement.removeEventListener('submit', this._onSubmit); |
| 19 | + this._formElement.removeEventListener('reset', this._onReset); |
| 20 | + } |
| 21 | + |
| 22 | + const formElements = (event.target as HTMLSlotElement) |
| 23 | + .assignedNodes({ flatten: true }) |
| 24 | + .filter(x => x instanceof HTMLFormElement) as HTMLFormElement[]; |
| 25 | + this._formElement = formElements.length > 0 ? formElements[0] : null; |
17 | 26 |
|
18 |
| - const isValid = this.checkValidity(); |
| 27 | + if (this._formElement) { |
| 28 | + this._formElement.setAttribute('novalidate', ''); |
| 29 | + this._formElement.addEventListener('submit', this._onSubmit); |
| 30 | + this._formElement.addEventListener('reset', this._onReset); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + private _onSubmit(event: SubmitEvent) { |
| 35 | + if (event.target === null) { |
| 36 | + return; |
| 37 | + } |
| 38 | + const formNode = event.target as HTMLFormElement; |
| 39 | + const isValid = formNode.checkValidity(); |
19 | 40 |
|
20 | 41 | if (!isValid) {
|
21 | 42 | // submit-invalid attribute is used by descendant form controls to check wether submit has been requested without succeeding.
|
22 |
| - this.setAttribute('submit-invalid', ''); |
| 43 | + formNode.setAttribute('submit-invalid', ''); |
23 | 44 | return;
|
24 | 45 | }
|
25 |
| - this.removeAttribute('submit-invalid'); |
26 |
| - |
27 |
| - const formData = new FormData(this); |
| 46 | + formNode.removeAttribute('submit-invalid'); |
| 47 | + } |
28 | 48 |
|
29 |
| - for (const value of formData.values()) { |
30 |
| - console.log(value); |
| 49 | + private _onReset(event: Event) { |
| 50 | + if (event.target === null) { |
| 51 | + return; |
31 | 52 | }
|
| 53 | + (event.target as HTMLFormElement).removeAttribute('submit-invalid'); |
32 | 54 | }
|
33 | 55 |
|
34 |
| - private _onReset() { |
35 |
| - this.removeAttribute('submit-invalid'); |
| 56 | + render() { |
| 57 | + return html`<slot @slotchange=${this._onSlotChanged}></slot>`; |
36 | 58 | }
|
37 | 59 | }
|
38 | 60 |
|
|
0 commit comments