-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathforms.ts
More file actions
265 lines (221 loc) · 8.64 KB
/
forms.ts
File metadata and controls
265 lines (221 loc) · 8.64 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import {
EditorUiContext,
EditorUiElement,
EditorContainerUiElement,
EditorUiBuilderDefinition,
isUiBuilderDefinition
} from "./core";
import {uniqueId} from "../../../services/util";
import {el} from "../../utils/dom";
export interface EditorFormFieldDefinition {
label: string;
name: string;
type: 'text' | 'select' | 'textarea' | 'checkbox' | 'hidden';
}
export interface EditorSelectFormFieldDefinition extends EditorFormFieldDefinition {
type: 'select',
valuesByLabel: Record<string, string>
}
export type EditorFormFields = (EditorFormFieldDefinition|EditorUiBuilderDefinition)[];
interface EditorFormTabDefinition {
label: string;
contents: EditorFormFields;
}
export interface EditorFormDefinition {
submitText: string;
action: (formData: FormData, context: EditorUiContext) => Promise<boolean>;
fields: EditorFormFields;
}
export class EditorFormField extends EditorUiElement {
protected definition: EditorFormFieldDefinition;
constructor(definition: EditorFormFieldDefinition) {
super();
this.definition = definition;
}
setValue(value: string) {
const input = this.getDOMElement().querySelector('input,select,textarea') as HTMLInputElement;
if (this.definition.type === 'checkbox') {
input.checked = Boolean(value);
} else {
input.value = value;
}
input.dispatchEvent(new Event('change'));
}
getName(): string {
return this.definition.name;
}
protected buildDOM(): HTMLElement {
const id = `editor-form-field-${this.definition.name}-${Date.now()}`;
let input: HTMLElement;
if (this.definition.type === 'select') {
const options = (this.definition as EditorSelectFormFieldDefinition).valuesByLabel
const labels = Object.keys(options);
const optionElems = labels.map(label => el('option', {value: options[label]}, [this.trans(label)]));
input = el('select', {id, name: this.definition.name, class: 'editor-form-field-input'}, optionElems);
} else if (this.definition.type === 'textarea') {
input = el('textarea', {id, name: this.definition.name, class: 'editor-form-field-input'});
} else if (this.definition.type === 'checkbox') {
input = el('input', {id, name: this.definition.name, type: 'checkbox', class: 'editor-form-field-input-checkbox', value: 'true'});
} else if (this.definition.type === 'hidden') {
input = el('input', {id, name: this.definition.name, type: 'hidden'});
return el('div', {hidden: 'true'}, [input]);
} else {
input = el('input', {id, name: this.definition.name, class: 'editor-form-field-input'});
}
return el('div', {class: 'editor-form-field-wrapper'}, [
el('label', {class: 'editor-form-field-label', for: id}, [this.trans(this.definition.label)]),
input,
]);
}
}
export class EditorForm extends EditorContainerUiElement {
protected definition: EditorFormDefinition;
protected onCancel: null|(() => void) = null;
protected onSuccessfulSubmit: null|(() => void) = null;
constructor(definition: EditorFormDefinition) {
let children: (EditorFormField|EditorUiElement)[] = definition.fields.map(fieldDefinition => {
if (isUiBuilderDefinition(fieldDefinition)) {
return fieldDefinition.build();
}
return new EditorFormField(fieldDefinition)
});
super(children);
this.definition = definition;
}
focusOnFirst() {
const focusable = this.getDOMElement().querySelector('input,select,textarea');
if (focusable) {
(focusable as HTMLElement).focus();
}
}
setValues(values: Record<string, string>) {
for (const name of Object.keys(values)) {
const field = this.getFieldByName(name);
if (field) {
field.setValue(values[name]);
}
}
}
setOnCancel(callback: () => void) {
this.onCancel = callback;
}
setOnSuccessfulSubmit(callback: () => void) {
this.onSuccessfulSubmit = callback;
}
protected getFieldByName(name: string): EditorFormField|null {
const search = (children: EditorUiElement[]): EditorFormField|null => {
for (const child of children) {
if (child instanceof EditorFormField && child.getName() === name) {
return child;
} else if (child instanceof EditorContainerUiElement) {
const matchingChild = search(child.getChildren());
if (matchingChild) {
return matchingChild;
}
}
}
return null;
};
return search(this.getChildren());
}
protected buildDOM(): HTMLElement {
const cancelButton = el('button', {type: 'button', class: 'editor-form-action-secondary'}, [this.trans('Cancel')]);
const form = el('form', {}, [
...this.children.map(child => child.getDOMElement()),
el('div', {class: 'editor-form-actions'}, [
cancelButton,
el('button', {type: 'submit', class: 'editor-form-action-primary'}, [this.trans(this.definition.submitText)]),
])
]);
form.addEventListener('submit', async (event) => {
event.preventDefault();
const formData = new FormData(form as HTMLFormElement);
const result = await this.definition.action(formData, this.getContext());
if (result && this.onSuccessfulSubmit) {
this.onSuccessfulSubmit();
}
});
cancelButton.addEventListener('click', (event) => {
if (this.onCancel) {
this.onCancel();
}
});
return form;
}
}
export class EditorFormTab extends EditorContainerUiElement {
protected definition: EditorFormTabDefinition;
protected fields: EditorUiElement[];
protected id: string;
constructor(definition: EditorFormTabDefinition) {
const fields = definition.contents.map(fieldDef => {
if (isUiBuilderDefinition(fieldDef)) {
return fieldDef.build();
}
return new EditorFormField(fieldDef)
});
super(fields);
this.definition = definition;
this.fields = fields;
this.id = uniqueId();
}
public getLabel(): string {
return this.getContext().translate(this.definition.label);
}
public getId(): string {
return this.id;
}
protected buildDOM(): HTMLElement {
return el(
'div',
{
class: 'editor-form-tab-content',
role: 'tabpanel',
id: `editor-tabpanel-${this.id}`,
'aria-labelledby': `editor-tab-${this.id}`,
},
this.fields.map(f => f.getDOMElement())
);
}
}
export class EditorFormTabs extends EditorContainerUiElement {
protected definitions: EditorFormTabDefinition[] = [];
protected tabs: EditorFormTab[] = [];
constructor(definitions: EditorFormTabDefinition[]) {
const tabs: EditorFormTab[] = definitions.map(d => new EditorFormTab(d));
super(tabs);
this.definitions = definitions;
this.tabs = tabs;
}
protected buildDOM(): HTMLElement {
const controls: HTMLElement[] = [];
const contents: HTMLElement[] = [];
const selectTab = (tabIndex: number) => {
for (let i = 0; i < controls.length; i++) {
controls[i].setAttribute('aria-selected', (i === tabIndex) ? 'true' : 'false');
}
for (let i = 0; i < contents.length; i++) {
contents[i].hidden = !(i === tabIndex);
}
};
for (const tab of this.tabs) {
const button = el('button', {
class: 'editor-form-tab-control',
type: 'button',
role: 'tab',
id: `editor-tab-${tab.getId()}`,
'aria-controls': `editor-tabpanel-${tab.getId()}`
}, [tab.getLabel()]);
contents.push(tab.getDOMElement());
controls.push(button);
button.addEventListener('click', event => {
selectTab(controls.indexOf(button));
});
}
selectTab(0);
return el('div', {class: 'editor-form-tab-container'}, [
el('div', {class: 'editor-form-tab-controls'}, controls),
el('div', {class: 'editor-form-tab-contents'}, contents),
]);
}
}