-
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: relax typing and throw error for unsupported elements (#649)
`userEvent.paste` and `userEvent.upload` accept `HTMLElement` as first parameter. If the specific element is not supported, throw a runtime error. Co-authored-by: Philipp Fritsche <ph.fritsche@gmail.com>
- Loading branch information
1 parent
5e6d7db
commit dc13160
Showing
5 changed files
with
89 additions
and
49 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,42 +1,45 @@ | ||
import { isElementType } from "../misc/isElementType"; | ||
import { isContentEditable } from './isContentEditable' | ||
import {isElementType} from '../misc/isElementType' | ||
import {isContentEditable} from './isContentEditable' | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
type GuardedType<T> = T extends (x: any) => x is (infer R) ? R : never | ||
type GuardedType<T> = T extends (x: any) => x is infer R ? R : never | ||
|
||
export function isEditable( | ||
element: Element | ||
element: Element, | ||
): element is | ||
GuardedType<typeof isContentEditable> | ||
| GuardedType<typeof isEditableInput> | ||
| HTMLTextAreaElement & {readOnly: false} | ||
{ | ||
return isEditableInput(element) | ||
|| isElementType(element, 'textarea', {readOnly: false}) | ||
|| isContentEditable(element) | ||
| GuardedType<typeof isContentEditable> | ||
| GuardedType<typeof isEditableInput> | ||
| (HTMLTextAreaElement & {readOnly: false}) { | ||
return ( | ||
isEditableInput(element) || | ||
isElementType(element, 'textarea', {readOnly: false}) || | ||
isContentEditable(element) | ||
) | ||
} | ||
|
||
enum editableInputTypes { | ||
'text' = 'text', | ||
'date' = 'date', | ||
'datetime-local' = 'datetime-local', | ||
'email' = 'email', | ||
'month' = 'month', | ||
'number' = 'number', | ||
'password' = 'password', | ||
'search' = 'search', | ||
'tel' = 'tel', | ||
'time' = 'time', | ||
'url' = 'url', | ||
'week' = 'week', | ||
export enum editableInputTypes { | ||
'text' = 'text', | ||
'date' = 'date', | ||
'datetime-local' = 'datetime-local', | ||
'email' = 'email', | ||
'month' = 'month', | ||
'number' = 'number', | ||
'password' = 'password', | ||
'search' = 'search', | ||
'tel' = 'tel', | ||
'time' = 'time', | ||
'url' = 'url', | ||
'week' = 'week', | ||
} | ||
|
||
export function isEditableInput( | ||
element: Element | ||
element: Element, | ||
): element is HTMLInputElement & { | ||
readOnly: false, | ||
type: editableInputTypes | ||
readOnly: false | ||
type: editableInputTypes | ||
} { | ||
return isElementType(element, 'input', {readOnly: false}) | ||
&& Boolean(editableInputTypes[element.type as editableInputTypes]) | ||
return ( | ||
isElementType(element, 'input', {readOnly: false}) && | ||
Boolean(editableInputTypes[element.type as editableInputTypes]) | ||
) | ||
} |