fix(field): cleaned up old register code

This commit is contained in:
Thijs Louisse 2019-08-13 09:00:49 +02:00
parent 085895ee94
commit 95a9ce7b0b
3 changed files with 2 additions and 54 deletions

View file

@ -29,19 +29,14 @@ export const FormRegisteringMixin = dedupeMixin(
__setupRegistrationHook() { __setupRegistrationHook() {
if (formRegistrarManager.ready) { if (formRegistrarManager.ready) {
this._registerFormElement(); this._dispatchRegistration();
} else { } else {
formRegistrarManager.addEventListener('all-forms-open-for-registration', () => { formRegistrarManager.addEventListener('all-forms-open-for-registration', () => {
this._registerFormElement(); this._dispatchRegistration();
}); });
} }
} }
_registerFormElement() {
this._dispatchRegistration();
this._requestParentFormGroupUpdateOfResetModelValue();
}
_dispatchRegistration() { _dispatchRegistration() {
this.dispatchEvent( this.dispatchEvent(
new CustomEvent('form-element-register', { new CustomEvent('form-element-register', {
@ -56,16 +51,5 @@ export const FormRegisteringMixin = dedupeMixin(
this.__parentFormGroup.removeFormElement(this); this.__parentFormGroup.removeFormElement(this);
} }
} }
/**
* Makes sure our parentFormGroup has the most up to date resetModelValue
* FormGroups will call the same on their parentFormGroup so the full tree gets the correct
* values.
*/
_requestParentFormGroupUpdateOfResetModelValue() {
if (this.__parentFormGroup && this.__parentFormGroup._updateResetModelValue) {
this.__parentFormGroup._updateResetModelValue();
}
}
}, },
); );

View file

@ -122,14 +122,6 @@ export class LionField extends FormControlMixin(
disconnectedCallback() { disconnectedCallback() {
super.disconnectedCallback(); super.disconnectedCallback();
if (this.__parentFormGroup) {
const event = new CustomEvent('form-element-unregister', {
detail: { element: this },
bubbles: true,
});
this.__parentFormGroup.dispatchEvent(event);
}
this.inputElement.removeEventListener('change', this._onChange); this.inputElement.removeEventListener('change', this._onChange);
} }

View file

@ -117,33 +117,5 @@ export const runRegistrationSuite = customConfig => {
el.removeChild(newField); el.removeChild(newField);
expect(el.formElements.length).to.equal(1); expect(el.formElements.length).to.equal(1);
}); });
describe('Unregister', () => {
it.skip('requests update of the resetModelValue function of its parent formGroup on unregister', async () => {
const ParentFormGroupClass = class extends FormRegistrarMixin(LitElement) {
_updateResetModelValue() {
this.resetModelValue = this.formElements.length;
}
};
const ChildFormGroupClass = class extends FormRegisteringMixin(LitElement) {
constructor() {
super();
this.__parentFormGroup = this.parentNode;
}
};
const formGroupTag = unsafeStatic(defineCE(ParentFormGroupClass));
const childFormGroupTag = unsafeStatic(defineCE(ChildFormGroupClass));
const parentFormEl = await fixture(html`
<${formGroupTag}>
<${childFormGroupTag} name="child[]"></${childFormGroupTag}>
<${childFormGroupTag} name="child[]"></${childFormGroupTag}>
</${formGroupTag}>
`);
expect(parentFormEl.resetModelValue.length).to.equal(2);
parentFormEl.removeChild(parentFormEl.children[0]);
expect(parentFormEl.resetModelValue.length).to.equal(1);
});
});
}); });
}; };