1 import {EditorUiContext, EditorUiElement} from "./core";
2 import {EditorContainerUiElement} from "./containers";
3 import {el} from "../../helpers";
5 export interface EditorFormFieldDefinition {
8 type: 'text' | 'select';
11 export interface EditorSelectFormFieldDefinition extends EditorFormFieldDefinition {
13 valuesByLabel: Record<string, string>
16 export interface EditorFormDefinition {
19 action: (formData: FormData, context: EditorUiContext) => boolean;
21 fields: EditorFormFieldDefinition[];
24 export class EditorFormField extends EditorUiElement {
25 protected definition: EditorFormFieldDefinition;
27 constructor(definition: EditorFormFieldDefinition) {
29 this.definition = definition;
32 protected buildDOM(): HTMLElement {
33 const id = `editor-form-field-${this.definition.name}-${Date.now()}`;
34 let input: HTMLElement;
36 if (this.definition.type === 'select') {
37 const options = (this.definition as EditorSelectFormFieldDefinition).valuesByLabel
38 const labels = Object.keys(options);
39 const optionElems = labels.map(label => el('option', {value: options[label]}, [label]));
40 input = el('select', {id, name: this.definition.name, class: 'editor-form-field-input'}, optionElems);
42 input = el('input', {id, name: this.definition.name, class: 'editor-form-field-input'});
45 return el('div', {class: 'editor-form-field-wrapper'}, [
46 el('label', {class: 'editor-form-field-label', for: id}, [this.trans(this.definition.label)]),
52 export class EditorForm extends EditorContainerUiElement {
53 protected definition: EditorFormDefinition;
55 constructor(definition: EditorFormDefinition) {
56 super(definition.fields.map(fieldDefinition => new EditorFormField(fieldDefinition)));
57 this.definition = definition;
60 protected buildDOM(): HTMLElement {
61 const cancelButton = el('button', {type: 'button', class: 'editor-form-action-secondary'}, [this.trans(this.definition.cancelText)]);
62 const form = el('form', {}, [
63 ...this.children.map(child => child.getDOMElement()),
64 el('div', {class: 'editor-form-actions'}, [
66 el('button', {type: 'submit', class: 'editor-form-action-primary'}, [this.trans(this.definition.submitText)]),
70 form.addEventListener('submit', (event) => {
71 event.preventDefault();
72 const formData = new FormData(form as HTMLFormElement);
73 this.definition.action(formData, this.getContext());
76 cancelButton.addEventListener('click', (event) => {
77 this.definition.cancel();