Skip to content

Commit

Permalink
fix: convert modules to typescript (#609)
Browse files Browse the repository at this point in the history
* fix: convert modules to typescript

* fix: convert modules to typescript

* test: istanbul ignore activeElement being null
  • Loading branch information
ph-fritsche authored Mar 22, 2021
1 parent 74d191c commit 0fe1fec
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/blur.js → src/blur.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {getActiveElement, isFocusable, eventWrapper} from './utils'

function blur(element) {
function blur(element: Element) {
if (!isFocusable(element)) return

const wasActive = getActiveElement(element.ownerDocument) === element
Expand Down
2 changes: 1 addition & 1 deletion src/focus.js → src/focus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {getActiveElement, isFocusable, eventWrapper} from './utils'

function focus(element) {
function focus(element: Element) {
if (!isFocusable(element)) return

const isAlreadyActive = getActiveElement(element.ownerDocument) === element
Expand Down
17 changes: 9 additions & 8 deletions src/hover.js → src/hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import {fireEvent} from '@testing-library/dom'
import {
isLabelWithInternallyDisabledControl,
getMouseEventOptions,
isDisabled,
} from './utils'

// includes `element`
function getParentElements(element) {
function getParentElements(element: Element) {
const parentElements = [element]
let currentElement = element
let currentElement: Element | null = element
while ((currentElement = currentElement.parentElement) != null) {
parentElements.push(currentElement)
}
return parentElements
}

function hover(element, init) {
function hover(element: Element, init?: MouseEventInit) {
if (isLabelWithInternallyDisabledControl(element)) return

const parentElements = getParentElements(element).reverse()
Expand All @@ -23,32 +24,32 @@ function hover(element, init) {
for (const el of parentElements) {
fireEvent.pointerEnter(el, init)
}
if (!element.disabled) {
if (!isDisabled(element)) {
fireEvent.mouseOver(element, getMouseEventOptions('mouseover', init))
for (const el of parentElements) {
fireEvent.mouseEnter(el, getMouseEventOptions('mouseenter', init))
}
}
fireEvent.pointerMove(element, init)
if (!element.disabled) {
if (!isDisabled(element)) {
fireEvent.mouseMove(element, getMouseEventOptions('mousemove', init))
}
}

function unhover(element, init) {
function unhover(element: Element, init?: MouseEventInit) {
if (isLabelWithInternallyDisabledControl(element)) return

const parentElements = getParentElements(element)

fireEvent.pointerMove(element, init)
if (!element.disabled) {
if (!isDisabled(element)) {
fireEvent.mouseMove(element, getMouseEventOptions('mousemove', init))
}
fireEvent.pointerOut(element, init)
for (const el of parentElements) {
fireEvent.pointerLeave(el, init)
}
if (!element.disabled) {
if (!isDisabled(element)) {
fireEvent.mouseOut(element, getMouseEventOptions('mouseout', init))
for (const el of parentElements) {
fireEvent.mouseLeave(el, getMouseEventOptions('mouseleave', init))
Expand Down
5 changes: 4 additions & 1 deletion src/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ function tab({shift = false, focusTrap}: tabOptions = {}) {

if (continueToTab) {
if (nextElement === document.body) {
blur(previousElement)
/* istanbul ignore else */
if (previousElement) {
blur(previousElement)
}
} else {
focus(nextElement)
}
Expand Down

0 comments on commit 0fe1fec

Please sign in to comment.