-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: π click on label works on textarea
- Loading branch information
Showing
2 changed files
with
176 additions
and
164 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,61 @@ | ||
import { fireEvent } from "dom-testing-library"; | ||
import { fireEvent } from 'dom-testing-library' | ||
|
||
function findTagInParents(element, tagName) { | ||
if (element.parentNode == null) return undefined; | ||
if (element.parentNode.tagName === tagName) return element.parentNode; | ||
return findTagInParents(element.parentNode, tagName); | ||
if (element.parentNode == null) return undefined | ||
if (element.parentNode.tagName === tagName) return element.parentNode | ||
return findTagInParents(element.parentNode, tagName) | ||
} | ||
|
||
function clickLabel(label) { | ||
fireEvent.mouseOver(label); | ||
fireEvent.mouseMove(label); | ||
fireEvent.mouseDown(label); | ||
fireEvent.mouseUp(label); | ||
fireEvent.click(label); | ||
fireEvent.mouseOver(label) | ||
fireEvent.mouseMove(label) | ||
fireEvent.mouseDown(label) | ||
fireEvent.mouseUp(label) | ||
fireEvent.click(label) | ||
|
||
if (label.htmlFor) { | ||
const input = document.getElementById(label.htmlFor); | ||
input.focus(); | ||
fireEvent.click(label); | ||
const input = document.getElementById(label.htmlFor) | ||
input.focus() | ||
fireEvent.click(label) | ||
} else { | ||
const input = label.querySelector("input"); | ||
input.focus(); | ||
label.focus(); | ||
fireEvent.click(input); | ||
fireEvent.click(label); | ||
const input = label.querySelector('input,textarea') | ||
input.focus() | ||
label.focus() | ||
fireEvent.click(input) | ||
fireEvent.click(label) | ||
} | ||
} | ||
|
||
function clickElement(element) { | ||
fireEvent.mouseOver(element); | ||
fireEvent.mouseMove(element); | ||
fireEvent.mouseDown(element); | ||
element.focus(); | ||
fireEvent.mouseUp(element); | ||
fireEvent.click(element); | ||
|
||
const labelAncestor = findTagInParents(element, "LABEL"); | ||
labelAncestor && clickLabel(labelAncestor); | ||
fireEvent.mouseOver(element) | ||
fireEvent.mouseMove(element) | ||
fireEvent.mouseDown(element) | ||
element.focus() | ||
fireEvent.mouseUp(element) | ||
fireEvent.click(element) | ||
|
||
const labelAncestor = findTagInParents(element, 'LABEL') | ||
labelAncestor && clickLabel(labelAncestor) | ||
} | ||
|
||
const userEvent = { | ||
click(element) { | ||
const focusedElement = document.activeElement; | ||
const focusedElement = document.activeElement | ||
const wasAnotherElementFocused = | ||
focusedElement !== document.body && focusedElement !== element; | ||
focusedElement !== document.body && focusedElement !== element | ||
if (wasAnotherElementFocused) { | ||
fireEvent.mouseMove(focusedElement); | ||
fireEvent.mouseLeave(focusedElement); | ||
fireEvent.mouseMove(focusedElement) | ||
fireEvent.mouseLeave(focusedElement) | ||
} | ||
|
||
if (element.tagName === "LABEL") { | ||
clickLabel(element); | ||
if (element.tagName === 'LABEL') { | ||
clickLabel(element) | ||
} else { | ||
clickElement(element); | ||
clickElement(element) | ||
} | ||
|
||
wasAnotherElementFocused && focusedElement.blur(); | ||
} | ||
}; | ||
wasAnotherElementFocused && focusedElement.blur() | ||
}, | ||
} | ||
|
||
export default userEvent; | ||
export default userEvent |