2021-08-19 22:25:14 +00:00
|
|
|
|
import { customElement, html, TemplateResult, DeesElement } from '@designestate/dees-element';
|
2020-09-13 16:24:48 +00:00
|
|
|
|
|
|
|
|
|
import { DeesInputCheckbox } from './dees-input-checkbox';
|
|
|
|
|
import { DeesInputText } from './dees-input-text';
|
|
|
|
|
import { DeesInputQuantitySelector } from './dees-input-quantityselector';
|
|
|
|
|
import { DeesInputRadio } from './dees-input-radio';
|
2021-08-19 22:25:14 +00:00
|
|
|
|
import * as domtools from '@designestate/dees-domtools';
|
|
|
|
|
|
|
|
|
|
export type TFormElement = Array<DeesInputCheckbox | DeesInputText | DeesInputQuantitySelector | DeesInputRadio>;
|
2020-09-13 16:24:48 +00:00
|
|
|
|
|
2021-02-13 21:52:36 +00:00
|
|
|
|
declare global {
|
|
|
|
|
interface HTMLElementTagNameMap {
|
|
|
|
|
'dees-form': DeesForm;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 16:24:48 +00:00
|
|
|
|
@customElement('dees-form')
|
2021-08-19 22:25:14 +00:00
|
|
|
|
export class DeesForm extends DeesElement {
|
2020-09-13 16:24:48 +00:00
|
|
|
|
public static demo = () => html`
|
2020-12-09 23:05:13 +00:00
|
|
|
|
<dees-form style="display: block; margin:auto; max-width: 500px; padding: 20px">
|
|
|
|
|
<dees-input-text key="hello1" label="a text"></dees-input-text>
|
|
|
|
|
<dees-input-text key="hello2" label="also a text"></dees-input-text>
|
|
|
|
|
<dees-input-checkbox key="hello3" label="another text"></dees-input-checkbox>
|
2020-09-13 16:24:48 +00:00
|
|
|
|
<dees-form-submit>Submit</dees-form-submit>
|
2020-12-09 23:05:13 +00:00
|
|
|
|
</dees-form>
|
|
|
|
|
`;
|
2020-09-13 16:24:48 +00:00
|
|
|
|
|
|
|
|
|
public name: string = 'myform';
|
2021-08-19 22:25:14 +00:00
|
|
|
|
public changeSubject = new domtools.rxjs.Subject();
|
2020-09-13 16:24:48 +00:00
|
|
|
|
|
|
|
|
|
public render(): TemplateResult {
|
|
|
|
|
return html`
|
|
|
|
|
<style>
|
|
|
|
|
:host {
|
|
|
|
|
display: contents;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
<slot></slot>
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 22:25:14 +00:00
|
|
|
|
public firstUpdated() {
|
|
|
|
|
const formChildren = this.getFormChildren();
|
|
|
|
|
for (const child of formChildren) {
|
|
|
|
|
child.changeSubject.subscribe(async (elementArg: TFormElement) => {
|
|
|
|
|
const valueObject = await this.gatherData();
|
|
|
|
|
this.changeSubject.next(valueObject);
|
|
|
|
|
console.log(valueObject);
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-13 16:24:48 +00:00
|
|
|
|
|
2021-08-19 22:25:14 +00:00
|
|
|
|
public getFormChildren() {
|
|
|
|
|
const children: Array<DeesElement> = this.children as any;
|
|
|
|
|
const formChildren: TFormElement = [];
|
|
|
|
|
|
2020-09-13 16:24:48 +00:00
|
|
|
|
for (const child of children) {
|
|
|
|
|
if (child instanceof DeesInputCheckbox || child instanceof DeesInputText || child instanceof DeesInputQuantitySelector) {
|
2021-08-19 22:25:14 +00:00
|
|
|
|
formChildren.push(child);
|
2020-09-13 16:24:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-19 22:25:14 +00:00
|
|
|
|
return formChildren;
|
|
|
|
|
}
|
2020-09-13 16:24:48 +00:00
|
|
|
|
|
2021-08-19 22:25:14 +00:00
|
|
|
|
public async gatherData() {
|
|
|
|
|
const children = this.getFormChildren();
|
|
|
|
|
const valueObject: { [key: string]: string | number | boolean} = {};
|
|
|
|
|
for (const child of children) {
|
|
|
|
|
valueObject[child.key] = child.value;
|
|
|
|
|
}
|
|
|
|
|
return valueObject;
|
|
|
|
|
}
|
2020-09-13 16:24:48 +00:00
|
|
|
|
|
2021-08-19 22:25:14 +00:00
|
|
|
|
public async gatherAndDispatch() {
|
|
|
|
|
const valueObject = await this.gatherData();
|
2020-09-13 16:24:48 +00:00
|
|
|
|
const formDataEvent = new CustomEvent('formData', {
|
|
|
|
|
detail: {
|
|
|
|
|
data: valueObject
|
|
|
|
|
},
|
|
|
|
|
bubbles: true
|
|
|
|
|
});
|
|
|
|
|
this.dispatchEvent(formDataEvent);
|
|
|
|
|
}
|
|
|
|
|
}
|