-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle selectionRange on different input types (#619)
* wip: selectionRange * test: remove testPathIgnorePattern for utils * fix: refactor selection handling * fix: {selectall} on number input
- Loading branch information
1 parent
7ff9a9a
commit d5aa3ee
Showing
18 changed files
with
284 additions
and
140 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {setup} from '__tests__/helpers/utils' | ||
import {isContentEditable} from '../../../utils' | ||
|
||
test('report if element is contenteditable', () => { | ||
const {elements} = setup( | ||
`<div></div><div contenteditable="false"></div><div contenteditable></div><div contenteditable="true"></div>`, | ||
) | ||
|
||
expect(isContentEditable(elements[0])).toBe(false) | ||
expect(isContentEditable(elements[1])).toBe(false) | ||
expect(isContentEditable(elements[2])).toBe(true) | ||
expect(isContentEditable(elements[3])).toBe(true) | ||
}) |
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,72 @@ | ||
import {getSelectionRange, setSelectionRange} from 'utils' | ||
import {setup} from '__tests__/helpers/utils' | ||
|
||
test('range on input', () => { | ||
const {element} = setup('<input value="foo"/>') | ||
|
||
expect(getSelectionRange(element as HTMLInputElement)).toEqual({ | ||
selectionStart: 0, | ||
selectionEnd: 0, | ||
}) | ||
|
||
setSelectionRange(element as HTMLInputElement, 0, 0) | ||
|
||
expect(element).toHaveProperty('selectionStart', 0) | ||
expect(element).toHaveProperty('selectionEnd', 0) | ||
expect(getSelectionRange(element as HTMLInputElement)).toEqual({ | ||
selectionStart: 0, | ||
selectionEnd: 0, | ||
}) | ||
|
||
setSelectionRange(element as HTMLInputElement, 2, 3) | ||
|
||
expect(element).toHaveProperty('selectionStart', 2) | ||
expect(element).toHaveProperty('selectionEnd', 3) | ||
expect(getSelectionRange(element as HTMLInputElement)).toEqual({ | ||
selectionStart: 2, | ||
selectionEnd: 3, | ||
}) | ||
}) | ||
|
||
test('range on contenteditable', () => { | ||
const {element} = setup('<div contenteditable="true">foo</div>') | ||
|
||
expect(getSelectionRange(element as HTMLInputElement)).toEqual({ | ||
selectionStart: null, | ||
selectionEnd: null, | ||
}) | ||
|
||
setSelectionRange(element as HTMLDivElement, 0, 0) | ||
|
||
expect(getSelectionRange(element as HTMLInputElement)).toEqual({ | ||
selectionStart: 0, | ||
selectionEnd: 0, | ||
}) | ||
|
||
setSelectionRange(element as HTMLDivElement, 2, 3) | ||
|
||
expect(document.getSelection()?.anchorNode).toBe(element?.firstChild) | ||
expect(document.getSelection()?.focusNode).toBe(element?.firstChild) | ||
expect(document.getSelection()?.anchorOffset).toBe(2) | ||
expect(document.getSelection()?.focusOffset).toBe(3) | ||
expect(getSelectionRange(element as HTMLInputElement)).toEqual({ | ||
selectionStart: 2, | ||
selectionEnd: 3, | ||
}) | ||
}) | ||
|
||
test('range on input without selection support', () => { | ||
const {element} = setup(`<input type="number" value="123"/>`) | ||
|
||
expect(getSelectionRange(element as HTMLInputElement)).toEqual({ | ||
selectionStart: null, | ||
selectionEnd: null, | ||
}) | ||
|
||
setSelectionRange(element as HTMLInputElement, 1, 2) | ||
|
||
expect(getSelectionRange(element as HTMLInputElement)).toEqual({ | ||
selectionStart: 1, | ||
selectionEnd: 2, | ||
}) | ||
}) |
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,3 +1,2 @@ | ||
export * from './fireChangeForInputTimeIfValid' | ||
export * from './fireInputEventIfNeeded' | ||
export * from './setSelectionRange' |
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,21 @@ | ||
import {isElementType} from 'utils/misc/isElementType' | ||
|
||
enum unreliableValueInputTypes { | ||
'number' = 'number', | ||
} | ||
|
||
/** | ||
* Check if an empty IDL value on the element could mean a derivation of displayed value and IDL value | ||
*/ | ||
export function hasUnreliableEmptyValue( | ||
element: Element, | ||
): element is HTMLInputElement & {type: unreliableValueInputTypes} { | ||
return ( | ||
isElementType(element, 'input') && | ||
Boolean( | ||
unreliableValueInputTypes[ | ||
element.type as keyof typeof unreliableValueInputTypes | ||
], | ||
) | ||
) | ||
} |
Oops, something went wrong.