Skip to content

Commit

Permalink
feat: add preventEventDefault, stopEventPropagation
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 17, 2018
1 parent e6886ca commit e61b23c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export { default as isString } from './isString'
export { default as isUndefined } from './isUndefined'
export { default as noop } from './noop'
export { default as parseCssValue } from './parseCssValue'
export { default as preventEventDefault } from './preventEventDefault'
export { default as reduce } from './reduce'
export { default as repeat } from './repeat'
export { default as shuffle } from './shuffle'
export { default as stopEventPropagation } from './stopEventPropagation'
export { default as supportPassiveEventListener } from './supportPassiveEventListener'
11 changes: 11 additions & 0 deletions src/preventEventDefault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* 阻止事件的默认行为。
*
* @param e 要阻止的事件
*/
export default function preventEventDefault(e: Event): void {
/* istanbul ignore else */
if (e instanceof Event) {
e.preventDefault()
}
}
11 changes: 11 additions & 0 deletions src/stopEventPropagation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* 阻止事件的进一步传播。
*
* @param e 要阻止的事件
*/
export default function stopEventPropagation(e: Event) {
/* istanbul ignore else */
if (e instanceof Event) {
e.stopPropagation()
}
}
26 changes: 26 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,3 +563,29 @@ describe('shuffle', () => {
expect(vtils.shuffle(arr2).sort()).toEqual(arr2.sort())
})
})

describe('preventEventDefault', () => {
test('阻止成功', done => {
const div = document.createElement('div')
div.onclick = e => {
expect(e.defaultPrevented).toBeFalsy()
vtils.preventEventDefault(e)
expect(e.defaultPrevented).toBeTruthy()
done()
}
div.click()
})
})

describe('stopEventPropagation', () => {
test('阻止成功', done => {
const div = document.createElement('div')
div.onclick = e => {
expect(e.cancelBubble).toBeFalsy()
vtils.stopEventPropagation(e)
expect(e.cancelBubble).toBeTruthy()
done()
}
div.click()
})
})

0 comments on commit e61b23c

Please sign in to comment.