-
Notifications
You must be signed in to change notification settings - Fork 25
/
form.js
233 lines (204 loc) · 6.46 KB
/
form.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { css, html, LitElement } from 'lit';
import { findFormElements, flattenMap, getFormElementData, isCustomFormElement, isNativeFormElement } from './form-helper.js';
import { findComposedAncestor } from '../../helpers/dom.js';
import { FormMixin } from './form-mixin.js';
/**
* A component that can be used to build sections containing interactive controls that are validated and submitted as a group.
* Values of these interactive controls are aggregated but the user is responsible for handling submission via the @d2l-form-submit event.
* @slot - The native and custom form elements that participate in validation and submission
* @fires d2l-form-connect - Internal event
*/
class Form extends FormMixin(LitElement) {
static get properties() {
return {
/**
* Indicates that the form should opt-out of nesting.
* This means that it will not be submitted or validated if an ancestor form is submitted or validated.
* However, directly submitting or validating a form with `no-nesting` will still trigger submission and validation for its descendant forms unless they also opt-out using `no-nesting`.
* @type {boolean}
*/
noNesting: { type: Boolean, attribute: 'no-nesting', reflect: true },
};
}
static get styles() {
return css`
:host {
display: block;
}
:host([hidden]) {
display: none;
}
`;
}
constructor() {
super();
this._isSubForm = false;
this._nestedForms = new Map();
/** @ignore */
this.addEventListener('d2l-form-connect', this._onFormConnect);
}
connectedCallback() {
super.connectedCallback();
/** @ignore */
this._isSubForm = !this.dispatchEvent(new CustomEvent('d2l-form-connect', { bubbles: true, composed: true, cancelable: true }));
}
disconnectedCallback() {
super.disconnectedCallback();
/** @ignore */
this.dispatchEvent(new CustomEvent('d2l-form-disconnect'));
this._isSubForm = false;
}
firstUpdated(changedProperties) {
super.firstUpdated(changedProperties);
this._setupDialogValidationReset();
}
render() {
let errorSummary = null;
if (this._isRootForm()) {
const errors = [...flattenMap(this._errors)]
.filter(([, eleErrors]) => eleErrors.length > 0)
.map(([ele, eleErrors]) => ({ href: `#${ele.id}`, message: eleErrors[0], onClick: () => ele.focus() }));
errorSummary = html`<d2l-form-error-summary .errors=${errors}></d2l-form-error-summary>`;
}
return html`
${errorSummary}
<slot></slot>
`;
}
async requestSubmit(submitter) {
const errors = await this.validate();
if (errors.size > 0) {
return;
}
this._submitData(submitter);
}
resetValidation() {
const formElements = this._findFormElements();
for (const ele of formElements) {
if (this._hasSubForms(ele)) {
const forms = this._getSubForms(ele);
for (const form of forms) {
form.resetValidation();
}
} else {
if (isCustomFormElement(ele)) {
ele.resetValidation();
} else if (isNativeFormElement(ele)) {
this._displayValid(ele);
}
}
}
this._errors = new Map();
this._tooltips = new Map();
}
async submit() {
return this.requestSubmit(null);
}
async validate() {
const errorMap = new Map();
const formElements = this._findFormElements();
for (const ele of formElements) {
if (this._hasSubForms(ele)) {
const forms = this._getSubForms(ele);
for (const form of forms) {
if (!form.noNesting) {
const formErrors = await form.validate();
for (const [key, val] of formErrors) {
errorMap.set(key, val);
}
}
}
} else {
const eleErrors = await this._validateFormElement(ele, true);
if (eleErrors.length > 0) {
errorMap.set(ele, eleErrors);
}
}
}
const flattenedErrorMap = flattenMap(errorMap);
this._errors = errorMap;
if (errorMap.size > 0) {
const errorSummary = this.shadowRoot && this.shadowRoot.querySelector('d2l-form-error-summary');
if (errorSummary) {
this.updateComplete.then(() => errorSummary.focus());
}
/** Dispatched when the form fails validation. The error map can be obtained from the `detail`'s `errors` property. */
this.dispatchEvent(new CustomEvent('d2l-form-invalid', { detail: { errors: flattenedErrorMap } }));
}
return flattenedErrorMap;
}
_findFormElements() {
const isFormElementPredicate = ele => this._hasSubForms(ele);
const visitChildrenPredicate = ele => !this._hasSubForms(ele);
return findFormElements(this, isFormElementPredicate, visitChildrenPredicate);
}
_getSubForms(ele) {
return this._nestedForms.get(ele);
}
_hasSubForms(ele) {
return this._nestedForms.has(ele);
}
_isRootForm() {
return !this._isSubForm || this.noNesting;
}
_onFormConnect(e) {
if (e.target === this) {
return;
}
e.stopPropagation();
e.preventDefault();
const form = e.composedPath()[0];
const target = e.target;
if (!this._nestedForms.has(target)) {
this._nestedForms.set(target, []);
}
this._nestedForms.get(target).push(form);
const onFormDisconnect = () => {
form.removeEventListener('d2l-form-disconnect', onFormDisconnect);
if (this._nestedForms.has(target)) {
const forms = this._nestedForms.get(target);
const index = forms.indexOf(form);
if (index > -1) {
forms.splice(index, 1);
}
if (forms.length === 0) {
this._nestedForms.delete(target);
}
}
};
form.addEventListener('d2l-form-disconnect', onFormDisconnect);
}
_setupDialogValidationReset() {
const flag = window.D2L?.LP?.Web?.UI?.Flags.Flag('GAUD-6979-dialog-close-reset-validation', true) ?? true;
if (!flag) return;
const dialogAncestor = findComposedAncestor(
this,
node => node?._isDialogMixin
);
if (!dialogAncestor) return;
dialogAncestor.addEventListener('d2l-dialog-close', () => {
this.resetValidation();
});
}
async _submitData(submitter) {
this._dirty = false;
let formData = {};
const formElements = this._findFormElements(this);
for (const ele of formElements) {
const eleData = getFormElementData(ele, submitter);
if (isCustomFormElement(ele) || isNativeFormElement(ele)) {
formData = { ...formData, ...eleData };
} else if (this._hasSubForms(ele)) {
const forms = this._getSubForms(ele);
forms.forEach(form => {
if (!form.noNesting) {
form._submitData(submitter);
}
});
}
}
/** Dispatched when the form is submitted. The form data can be obtained from the `detail`'s `formData` property. */
this.dispatchEvent(new CustomEvent('d2l-form-submit', { detail: { formData } }));
}
}
customElements.define('d2l-form', Form);