fix(core): update

This commit is contained in:
2021-08-25 13:51:55 +02:00
parent 0adb319616
commit 486b8cb6a6
9 changed files with 104 additions and 15 deletions

View File

@ -5,6 +5,7 @@ import { DeesInputText } from './dees-input-text';
import { DeesInputQuantitySelector } from './dees-input-quantityselector';
import { DeesInputRadio } from './dees-input-radio';
import * as domtools from '@designestate/dees-domtools';
import { DeesFormSubmit } from './dees-form-submit';
export type TFormElement = Array<DeesInputCheckbox | DeesInputText | DeesInputQuantitySelector | DeesInputRadio>;
@ -18,9 +19,9 @@ declare global {
export class DeesForm extends DeesElement {
public static demo = () => html`
<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>
<dees-input-text .required="${true}" key="hello1" label="a text"></dees-input-text>
<dees-input-text .required="${true}" key="hello2" label="also a text"></dees-input-text>
<dees-input-checkbox .required="${true}" key="hello3" label="another text"></dees-input-checkbox>
<dees-form-submit>Submit</dees-form-submit>
</dees-form>
`;
@ -41,12 +42,14 @@ export class DeesForm extends DeesElement {
public firstUpdated() {
const formChildren = this.getFormChildren();
this.checkRequiredStatus();
for (const child of formChildren) {
child.changeSubject.subscribe(async (elementArg: TFormElement) => {
const valueObject = await this.gatherData();
this.changeSubject.next(valueObject);
console.log(valueObject);
})
this.checkRequiredStatus();
});
}
}
@ -62,6 +65,26 @@ export class DeesForm extends DeesElement {
return formChildren;
}
public async checkRequiredStatus() {
console.log('checking the required status.')
const children: Array<DeesElement> = this.children as any;
let submitButton: DeesFormSubmit;
for (const childArg of children) {
if(childArg instanceof DeesFormSubmit) {
submitButton = childArg;
}
}
let requiredOK = true;
for (const childArg of this.getFormChildren()) {
if (childArg.required && !childArg.value) {
requiredOK = false;
}
}
submitButton.disabled = !requiredOK;
console.log(submitButton);
}
public async gatherData() {
const children = this.getFormChildren();
const valueObject: { [key: string]: string | number | boolean} = {};
@ -80,5 +103,7 @@ export class DeesForm extends DeesElement {
bubbles: true
});
this.dispatchEvent(formDataEvent);
console.log('dispatched data:')
console.log(valueObject);
}
}