-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
afc8f1a
commit 616e2d9
Showing
9 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
import './component' | ||
|
||
|
||
import { html } from 'rehtm' | ||
|
||
|
||
document.body.innerHTML += '<c-thingy onevenclick="console.log(`clicked!`)"></c-thingy>' | ||
document.body.appendChild(html`<c-thingy onevenclick=${() => console.log('clicked!')}></c-thingy>`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { currentNode, ownerDocument } from './hooks' | ||
import { onAttribute } from './attribute' | ||
|
||
|
||
export function useDispatch<D = any>(name: string, options: EventInit = {}) { | ||
const window = ownerDocument()!.defaultView! | ||
const current = currentNode()! | ||
|
||
let listener: EventListener | undefined | ||
onAttribute('on' + name, (value) => { | ||
if (listener) { | ||
current.removeEventListener(name, listener) | ||
listener = undefined | ||
} | ||
|
||
if (typeof value === 'function') { | ||
listener = value | ||
current.addEventListener(name, value) | ||
} else if (typeof value === 'string') { | ||
listener = new Function('event', value) as EventListener | ||
current.addEventListener(name, listener) | ||
} | ||
}) | ||
|
||
return (detail: D) => current.dispatchEvent(new window.CustomEvent(name, { ...options, detail })) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useDispatch } from '../../dispatch' | ||
import { on } from '../../on' | ||
import { define } from '../../define' | ||
|
||
|
||
describe(useDispatch, () => { | ||
test('dispatches an event.', () => { | ||
define('dispatch-1', () => { | ||
const dispatch = useDispatch('test') | ||
on('click', () => dispatch('Hello!')) | ||
|
||
return '<div>Hi!</div>' | ||
}) | ||
|
||
const element = document.createElement('dispatch-1') | ||
document.body.appendChild(element) | ||
|
||
const cb = jest.fn() | ||
|
||
element.addEventListener('test', cb) | ||
element.click() | ||
|
||
expect(cb).toHaveBeenCalledWith(expect.objectContaining({ detail: 'Hello!' })) | ||
}) | ||
|
||
test('accepts event listeners as attribute.', () => { | ||
define('dispatch-2', () => { | ||
const dispatch = useDispatch('test') | ||
on('click', () => dispatch('Hello!')) | ||
|
||
return '<div>Hi!</div>' | ||
}) | ||
|
||
global.console.log = jest.fn() | ||
document.body.innerHTML = '<dispatch-2 ontest="console.log(event.detail)"></dispatch-2>' | ||
|
||
const element = document.querySelector('dispatch-2') as HTMLElement | ||
const cb = jest.fn() | ||
|
||
element.click() | ||
element.click() | ||
expect(cb).not.toHaveBeenCalled() | ||
expect(global.console.log).toHaveBeenCalledWith('Hello!') | ||
|
||
element.setAttribute('ontest', cb as any) | ||
element.click() | ||
|
||
expect(cb).toHaveBeenCalledWith(expect.objectContaining({ detail: 'Hello!' })) | ||
expect(global.console.log).toHaveBeenCalledTimes(2) | ||
|
||
element.removeAttribute('ontest') | ||
element.click() | ||
|
||
expect(cb).toHaveBeenCalledTimes(1) | ||
expect(global.console.log).toHaveBeenCalledTimes(2) | ||
}) | ||
}) |