Skip to content

Commit 759c82c

Browse files
committed
add on get/set for all events
1 parent 14cd6d3 commit 759c82c

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

custom-elements.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@
158158
}
159159
]
160160
},
161+
{
162+
"kind": "field",
163+
"name": "#onloadend",
164+
"privacy": "private",
165+
"type": {
166+
"text": "((event: Event) => void) | null"
167+
},
168+
"default": "null"
169+
},
170+
{
171+
"kind": "field",
172+
"name": "onloadend"
173+
},
161174
{
162175
"kind": "field",
163176
"name": "input",

src/auto-check-element.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ export class AutoCheckElement extends HTMLElement {
7777
return this
7878
}
7979

80+
#onloadend: ((event: Event) => void) | null = null
81+
get onloadend() {
82+
return this.#onloadend
83+
}
84+
85+
set onloadend(listener: ((event: Event) => void) | null) {
86+
if (this.#onloadend) {
87+
this.removeEventListener('loadend', this.#onloadend as unknown as EventListenerOrEventListenerObject)
88+
}
89+
this.#onloadend = typeof listener === 'object' || typeof listener === 'function' ? listener : null
90+
if (typeof listener === 'function') {
91+
this.addEventListener('loadend', listener as unknown as EventListenerOrEventListenerObject)
92+
}
93+
}
94+
8095
connectedCallback(): void {
8196
const input = this.input
8297
if (!input) return

test/auto-check.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,22 @@ describe('auto-check element', function () {
151151

152152
assert.deepEqual(['loadstart', 'load', 'loadend'], events)
153153
})
154+
155+
it('can use setters', async function () {
156+
const events = []
157+
const track = event => events.push(event.type)
158+
159+
checker.onloadstart = track
160+
checker.onload = track
161+
checker.onerror = track
162+
checker.onloadend = track
163+
164+
const completed = Promise.all([once(checker, 'loadstart'), once(checker, 'load'), once(checker, 'loadend')])
165+
triggerInput(input, 'hub')
166+
await completed
167+
168+
assert.deepEqual(['loadstart', 'load', 'loadend'], events)
169+
})
154170
})
155171

156172
describe('auto-check lifecycle events', function () {
@@ -235,7 +251,6 @@ describe('auto-check element', function () {
235251

236252
input.value = 'hub'
237253
input.dispatchEvent(new InputEvent('input'))
238-
input.dispatchEvent(new InputEvent('input'))
239254
})
240255

241256
it('do not emit if essential attributes are missing', async function () {

0 commit comments

Comments
 (0)