Skip to content

Commit 5511404

Browse files
authored
Merge pull request #525 from patternfly/emit-event
add emitEvent and .events
2 parents 0824670 + 59a8b66 commit 5511404

File tree

12 files changed

+317
-114
lines changed

12 files changed

+317
-114
lines changed

CHANGELOG-prerelease.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Tag: [v1.0.0-prerelease.35](https://github.com/patternfly/patternfly-elements/re
1212
- [90855bb](https://github.com/patternfly/patternfly-elements/commit/90855bbc01b800de3280691ee67e61887fd7fe4d) chore: Update license from 2019 to 2020
1313
- [88b26ed](https://github.com/patternfly/patternfly-elements/commit/88b26ed90616651a994890454172be5b4e78db7d) fix: pfe-navigation: Trigger link color, font-size, logo min-width, spacing (#631)
1414
- [ea25cd0](https://github.com/patternfly/patternfly-elements/commit/ea25cd0c87fa853784ffc15329796bda49d192f1) Change direction of disclosure carets (#674) (https://github.com/patternfly/patternfly-elements/issues/662)
15+
- [](https://github.com/patternfly/patternfly-elements/commit/) feat: Add emit events to pfelement base class
1516

1617
## Prerelease 34 ( 2019-12-20 )
1718

elements/pfe-accordion/src/pfe-accordion.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ class PfeAccordion extends PFElement {
3232
};
3333
}
3434

35+
static get events() {
36+
return {
37+
change: `${this.tag}:change`
38+
};
39+
}
40+
3541
// Declare the type of this component
3642
static get PfeType() {
3743
return PFElement.PfeTypes.Container;
@@ -54,7 +60,7 @@ class PfeAccordion extends PFElement {
5460
this.setAttribute("role", "presentation");
5561
this.setAttribute("defined", "");
5662

57-
this.addEventListener(`${PfeAccordion.tag}:change`, this._changeHandler);
63+
this.addEventListener(PfeAccordion.events.change, this._changeHandler);
5864
this.addEventListener("keydown", this._keydownHandler);
5965

6066
Promise.all([
@@ -70,7 +76,7 @@ class PfeAccordion extends PFElement {
7076
}
7177

7278
disconnectedCallback() {
73-
this.removeEventListener(`${PfeAccordion.tag}:change`, this._changeHandler);
79+
this.removeEventListener(PfeAccordion.events.change, this._changeHandler);
7480
this.removeEventListener("keydown", this._keydownHandler);
7581
this._observer.disconnect();
7682
}
@@ -205,7 +211,9 @@ class PfeAccordion extends PFElement {
205211

206212
_expandPanel(panel) {
207213
if (!panel) {
208-
console.error(`${PfeAccordion.tag}: Trying to expand a panel that doesn't exist`);
214+
console.error(
215+
`${PfeAccordion.tag}: Trying to expand a panel that doesn't exist`
216+
);
209217
return;
210218
}
211219

@@ -225,7 +233,9 @@ class PfeAccordion extends PFElement {
225233

226234
_collapsePanel(panel) {
227235
if (!panel) {
228-
console.error(`${PfeAccordion.tag}: Trying to collapse a panel that doesn't exist`);
236+
console.error(
237+
`${PfeAccordion.tag}: Trying to collapse a panel that doesn't exist`
238+
);
229239
return;
230240
}
231241

@@ -466,9 +476,7 @@ class PfeAccordionHeader extends PFElement {
466476

467477
if (!isHeaderTag) {
468478
console.warn(
469-
`${
470-
PfeAccordionHeader.tag
471-
}: The first child in the light DOM must be a Header level tag (h1, h2, h3, h4, h5, or h6)`
479+
`${PfeAccordionHeader.tag}: The first child in the light DOM must be a Header level tag (h1, h2, h3, h4, h5, or h6)`
472480
);
473481
}
474482

@@ -478,12 +486,9 @@ class PfeAccordionHeader extends PFElement {
478486
}
479487

480488
_clickHandler(event) {
481-
this.dispatchEvent(
482-
new CustomEvent(`${PfeAccordion.tag}:change`, {
483-
detail: { expanded: !this.expanded },
484-
bubbles: true
485-
})
486-
);
489+
this.emitEvent(PfeAccordion.events.change, {
490+
detail: { expanded: !this.expanded }
491+
});
487492
}
488493
}
489494

elements/pfe-autocomplete/src/pfe-autocomplete.js

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ class PfeAutocomplete extends PFElement {
2727
return "pfe-autocomplete.scss";
2828
}
2929

30+
static get events() {
31+
return {
32+
search: `pfe-search-event`,
33+
select: `pfe-option-selected`,
34+
slotchange: `slotchange`
35+
};
36+
}
37+
3038
constructor() {
3139
super(PfeAutocomplete);
3240

@@ -59,19 +67,31 @@ class PfeAutocomplete extends PFElement {
5967
this.addEventListener("keyup", this._inputKeyUp.bind(this));
6068

6169
// these two events, fire search
62-
this.addEventListener("pfe-search-event", this._closeDroplist.bind(this));
6370
this.addEventListener(
64-
"pfe-option-selected",
71+
PfeAutocomplete.events.search,
72+
this._closeDroplist.bind(this)
73+
);
74+
this.addEventListener(
75+
PfeAutocomplete.events.select,
6576
this._optionSelected.bind(this)
6677
);
6778
}
6879

6980
disconnectedCallback() {
7081
this.removeEventListener("keyup", this._inputKeyUp);
71-
this.removeEventListener("pfe-search-event", this._closeDroplist);
72-
this.removeEventListener("pfe-option-selected", this._optionSelected);
73-
this._slot.removeEventListener("slotchange", this._slotchangeHandler);
7482

83+
this.removeEventListener(
84+
PfeAutocomplete.events.search,
85+
this._closeDroplist
86+
);
87+
this.removeEventListener(
88+
PfeAutocomplete.events.select,
89+
this._optionSelected
90+
);
91+
this._slot.removeEventListener(
92+
PfeAutocomplete.events.slotchange,
93+
this._slotchangeHandler
94+
);
7595
if (this._input) {
7696
this._input.removeEventListener("input", this._inputChanged);
7797
this._input.removeEventListener("blur", this._closeDroplist);
@@ -281,13 +301,10 @@ class PfeAutocomplete extends PFElement {
281301
}
282302

283303
_doSearch(searchQuery) {
284-
this.dispatchEvent(
285-
new CustomEvent("pfe-search-event", {
286-
detail: { searchValue: searchQuery },
287-
bubbles: true,
288-
composed: true
289-
})
290-
);
304+
this.emitEvent(PfeAutocomplete.events.search, {
305+
detail: { searchValue: searchQuery },
306+
composed: true
307+
});
291308
this._reset();
292309
this.selectedValue = searchQuery;
293310
}
@@ -436,13 +453,10 @@ class PfeSearchDroplist extends PFElement {
436453

437454
_optionSelected(e) {
438455
if (e.target.tagName === "LI") {
439-
this.dispatchEvent(
440-
new CustomEvent("pfe-option-selected", {
441-
detail: { optionValue: e.target.innerText },
442-
bubbles: true,
443-
composed: true
444-
})
445-
);
456+
this.emitEvent(PfeAutocomplete.events.select, {
457+
detail: { optionValue: e.target.innerText },
458+
composed: true
459+
});
446460
}
447461
}
448462

elements/pfe-avatar/src/pfe-avatar.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ class PfeAvatar extends PFElement {
1919
return ["pfe-name", "pfe-pattern", "pfe-src", "pfe-shape"];
2020
}
2121

22+
static get events() {
23+
return {
24+
connected: `${this.tag}:connected`
25+
};
26+
}
27+
2228
static get patterns() {
2329
return {
2430
triangles: "triangles",
@@ -75,11 +81,9 @@ class PfeAvatar extends PFElement {
7581

7682
this._initCanvas();
7783

78-
this.dispatchEvent(
79-
new CustomEvent(`${PfeAvatar.tag}:connected`, {
80-
bubbles: false
81-
})
82-
);
84+
this.emitEvent(PfeAvatar.events.connected, {
85+
bubbles: false
86+
});
8387
}
8488

8589
attributeChangedCallback(attr, oldValue, newValue) {
@@ -88,7 +92,7 @@ class PfeAvatar extends PFElement {
8892
if (this.connected) {
8993
this.update();
9094
} else {
91-
this.addEventListener(`${PfeAvatar.tag}:connected`, () => this.update());
95+
this.addEventListener(PfeAvatar.events.connected, () => this.update());
9296
}
9397
}
9498

elements/pfe-collapse/src/pfe-collapse.js

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,13 @@ class PfeCollapseToggle extends PFElement {
122122
if (this.controlledPanel) {
123123
this.controlledPanel.expanded = this.expanded;
124124

125-
this.dispatchEvent(
126-
new CustomEvent(`${PfeCollapse.tag}:change`, {
127-
detail: {
128-
expanded: this.expanded,
129-
toggle: this,
130-
panel: this.controlledPanel
131-
},
132-
bubbles: true
133-
})
134-
);
125+
this.emitEvent(PfeCollapse.events.change, {
126+
detail: {
127+
expanded: this.expanded,
128+
toggle: this,
129+
panel: this.controlledPanel
130+
}
131+
});
135132
} else {
136133
console.warn(
137134
`${this.tag}: This toggle doesn't have a panel associated with it`
@@ -171,6 +168,13 @@ class PfeCollapsePanel extends PFElement {
171168
return "pfe-collapse-panel";
172169
}
173170

171+
static get events() {
172+
return {
173+
animationStart: `${this.tag}:animation-start`,
174+
animationEnd: `${this.tag}:animation-end`
175+
};
176+
}
177+
174178
get templateUrl() {
175179
return "pfe-collapse-panel.html";
176180
}
@@ -269,27 +273,21 @@ class PfeCollapsePanel extends PFElement {
269273
this._transitionEndHandler
270274
);
271275

272-
this.dispatchEvent(
273-
new CustomEvent(`${PfeCollapsePanel.tag}:animation-end`, {
274-
detail: {
275-
expanded: this.expanded,
276-
panel: this
277-
},
278-
bubbles: true
279-
})
280-
);
276+
this.emitEvent(PfeCollapsePanel.events.animationEnd, {
277+
detail: {
278+
expanded: this.expanded,
279+
panel: this
280+
}
281+
});
281282
}
282283

283284
_fireAnimationEvent(state) {
284-
this.dispatchEvent(
285-
new CustomEvent(`${PfeCollapsePanel.tag}:animation-start`, {
286-
detail: {
287-
state: state,
288-
panel: this
289-
},
290-
bubbles: true
291-
})
292-
);
285+
this.emitEvent(PfeCollapsePanel.events.animationStart, {
286+
detail: {
287+
state: state,
288+
panel: this
289+
}
290+
});
293291
}
294292
}
295293

@@ -318,6 +316,12 @@ class PfeCollapse extends PFElement {
318316
return ["pfe-animation"];
319317
}
320318

319+
static get events() {
320+
return {
321+
change: `${this.tag}:change`
322+
};
323+
}
324+
321325
constructor(pfeClass) {
322326
super(pfeClass || PfeCollapse);
323327

@@ -327,9 +331,15 @@ class PfeCollapse extends PFElement {
327331
this._changeHandler = this._changeHandler.bind(this);
328332
this._observer = new MutationObserver(this._linkControls);
329333

330-
this.addEventListener(`${PfeCollapse.tag}:change`, this._changeHandler);
331-
this.addEventListener(`${PfeCollapsePanel.tag}:animation-start`, this._animationStartHandler);
332-
this.addEventListener(`${PfeCollapsePanel.tag}:animation-end`, this._animationEndHandler);
334+
this.addEventListener(PfeCollapse.events.change, this._changeHandler);
335+
this.addEventListener(
336+
PfeCollapse.events.animationStart,
337+
this._animationStartHandler
338+
);
339+
this.addEventListener(
340+
PfeCollapse.events.animationEnd,
341+
this._animationEndHandler
342+
);
333343
}
334344

335345
connectedCallback() {
@@ -348,9 +358,15 @@ class PfeCollapse extends PFElement {
348358
}
349359

350360
disconnectedCallback() {
351-
this.removeEventListener(`${PfeCollapse.tag}:change`, this._changeHandler);
352-
this.removeEventListener(`${PfeCollapsePanel.tag}:animation-start`, this._animationStartHandler);
353-
this.removeEventListener(`${PfeCollapsePanel.tag}:animation-end`, this._animationEndHandler);
361+
this.removeEventListener(PfeCollapse.events.change, this._changeHandler);
362+
this.removeEventListener(
363+
PfeCollapse.events.animationStart,
364+
this._animationStartHandler
365+
);
366+
this.removeEventListener(
367+
PfeCollapse.events.animationEnd,
368+
this._animationEndHandler
369+
);
354370
this._observer.disconnect();
355371
}
356372

@@ -389,9 +405,7 @@ class PfeCollapse extends PFElement {
389405
this.classList.remove("animating");
390406
}
391407

392-
_changeHandler(event) {
393-
394-
}
408+
_changeHandler(event) {}
395409
}
396410

397411
PFElement.create(PfeCollapse);

0 commit comments

Comments
 (0)