@knowark/componarkjs 1.7.10 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -279,6 +279,28 @@ describe('Component', () => {
|
|
|
279
279
|
expect(event.detail.code).toEqual(['XYZ890'])
|
|
280
280
|
})
|
|
281
281
|
|
|
282
|
+
it('listens to events and pipes them to target components', async () => {
|
|
283
|
+
container.innerHTML = `
|
|
284
|
+
<mock-component code="ABC123">
|
|
285
|
+
<div type="text" listen on-change="replaceChildren|detail.name@#child">
|
|
286
|
+
<button id="action">Action</button>
|
|
287
|
+
</div>
|
|
288
|
+
<div id="child">
|
|
289
|
+
Hello
|
|
290
|
+
</div>
|
|
291
|
+
</mock-component>
|
|
292
|
+
`
|
|
293
|
+
const component = container.querySelector('mock-component')
|
|
294
|
+
const button = component.select('#action')
|
|
295
|
+
|
|
296
|
+
const event = new globalThis.CustomEvent(
|
|
297
|
+
'change', { bubbles: true, detail: { name: 'World' } })
|
|
298
|
+
button.dispatchEvent(event)
|
|
299
|
+
|
|
300
|
+
const child = container.querySelector('#child')
|
|
301
|
+
expect(child.textContent.trim()).toEqual('World')
|
|
302
|
+
})
|
|
303
|
+
|
|
282
304
|
it('emits an error event on declared listeners', async () => {
|
|
283
305
|
container.innerHTML = `
|
|
284
306
|
<mock-component>
|
|
@@ -2,12 +2,16 @@ import { camelToKebab } from './format.js'
|
|
|
2
2
|
|
|
3
3
|
/** @param {HTMLElement} self */
|
|
4
4
|
export function listen (self) {
|
|
5
|
+
/** @ts-ignore */
|
|
5
6
|
const binding = self.binding
|
|
6
7
|
const elements = self.querySelectorAll(`[${binding}]`)
|
|
7
8
|
for (const element of elements) {
|
|
8
9
|
for (const attribute of Array.from(element.attributes)) {
|
|
9
10
|
if (attribute.name.startsWith('on-')) {
|
|
10
|
-
const [
|
|
11
|
+
const [reference, at] = attribute.value.split('@').map(
|
|
12
|
+
item => item.trim())
|
|
13
|
+
const [value, pipe] = reference.split('|').map(
|
|
14
|
+
item => item.trim())
|
|
11
15
|
|
|
12
16
|
let handler = self[value]
|
|
13
17
|
const [assignment] = value.match(/[^{{]+(?=\}})/g) || []
|
|
@@ -31,7 +35,8 @@ export function listen (self) {
|
|
|
31
35
|
if (!receiver) return
|
|
32
36
|
handler = receiver[value]
|
|
33
37
|
}
|
|
34
|
-
|
|
38
|
+
const data = pipe ? get(event, pipe) : event
|
|
39
|
+
return await Promise.resolve(handler.bind(receiver)(data))
|
|
35
40
|
} catch (error) {
|
|
36
41
|
this.dispatchEvent(
|
|
37
42
|
new globalThis.CustomEvent(
|
|
@@ -50,9 +55,11 @@ export function listen (self) {
|
|
|
50
55
|
|
|
51
56
|
/** @param {HTMLElement} self */
|
|
52
57
|
function provide (self) {
|
|
58
|
+
/** @ts-ignore */
|
|
53
59
|
if (!self.provide) return
|
|
54
60
|
self.addEventListener('resolve', (/** @type {CustomEvent} */ event) => {
|
|
55
61
|
const resource = event.detail.resource
|
|
62
|
+
/** @ts-ignore */
|
|
56
63
|
const dependency = self.provide(resource)
|
|
57
64
|
if (dependency === undefined) return
|
|
58
65
|
event.detail[resource] = dependency
|