Skip to content

Commit

Permalink
Dispatch change event on attribute change (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
nizniz187 committed Sep 5, 2021
1 parent 8e69c95 commit b8fc03c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
4 changes: 0 additions & 4 deletions wc/WComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class WComponent extends HTMLElement{
this.componentWillRender();
this.render();
this.componentDidRender();
this.key = new Date().getTime();
}

componentWillRender() {}
Expand Down Expand Up @@ -61,9 +60,6 @@ class WComponent extends HTMLElement{
});
});
}
equals(component) {
return this.key === component.key;
}
getAttributeParserByName(name) {
if(typeof name !== 'string') {
return undefined;
Expand Down
16 changes: 9 additions & 7 deletions wc/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ class Form extends WComponent{
submitBtn.addEventListener('click', this.submitHandler);

// Bind radio click callback for name group control
this.querySelectorAll('w-radio').forEach(radio => radio.addEventListener('click', this.radioClickCallback));
this.querySelectorAll('w-radio').forEach(radio => {
radio.addEventListener('change', this.radioChangeCallback);
});
}
/**
* Bind direct form access from document by name.
Expand Down Expand Up @@ -116,16 +118,16 @@ class Form extends WComponent{
DOM.create('slot', {}, form);
}

radioClickCallback = e => {
radioChangeCallback = e => {
const radio = e.target;
if(typeof radio.name !== 'string' || radio.disabled) { return; }

const radios = Array.from(this.querySelectorAll(`w-radio[name='${radio.name}']`));
radios.forEach(r => {
if(!r.equals(radio)) {
r.checked = false;
}
});
// radios.forEach(r => {
// if(!r.equals(radio)) {
// r.checked = false;
// }
// });
}
submitHandler = e => {
this.dispatchEvent(this.events.submit);
Expand Down
9 changes: 7 additions & 2 deletions wc/components/checkable/Checkable.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,17 @@ class Checkable extends WComponent{
} else {
input.removeAttribute(name);
}

// Trigger change event
if(name === this.constructor.attributes.checked.name && oldValue !== newValue) {
this.dispatchEvent(this.events.change);
}
}
}
bindEvents() {
this.events = {
change: new Event('change')
change: new Event('change'),
click: new Event('click')
};
this.shadowRoot.addEventListener('click', this.clickHandler);
}
Expand All @@ -115,7 +121,6 @@ class Checkable extends WComponent{
clickHandler = e => {
if(!this.disabled) { // Trigger checked change
this.checked = !this.checked;
this.dispatchEvent(this.events.change);
}
};

Expand Down

0 comments on commit b8fc03c

Please sign in to comment.