-
Notifications
You must be signed in to change notification settings - Fork 794
/
cmp.tsx
47 lines (40 loc) · 1.16 KB
/
cmp.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { Component, Element, State, h } from '@stencil/core';
@Component({
tag: 'custom-event-root',
})
export class CustomEventCmp {
@Element() elm!: HTMLElement;
@State() output = '';
componentDidLoad() {
this.elm.addEventListener('eventNoDetail', this.receiveEvent.bind(this));
this.elm.addEventListener('eventWithDetail', this.receiveEvent.bind(this));
}
receiveEvent(ev: any) {
this.output = `${ev.type} ${ev.detail || ''}`.trim();
}
fireCustomEventNoDetail() {
const ev = new CustomEvent('eventNoDetail');
this.elm.dispatchEvent(ev);
}
fireCustomEventWithDetail() {
const ev = new CustomEvent('eventWithDetail', { detail: 88 });
this.elm.dispatchEvent(ev);
}
render() {
return (
<div>
<div>
<button id="btnNoDetail" onClick={this.fireCustomEventNoDetail.bind(this)}>
Fire Custom Event, no detail
</button>
</div>
<div>
<button id="btnWithDetail" onClick={this.fireCustomEventWithDetail.bind(this)}>
Fire Custom Event, with detail
</button>
</div>
<pre id="output">{this.output}</pre>
</div>
);
}
}