-
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.
feat(event): support
beforeinput
(#851)
- Loading branch information
1 parent
ca4482a
commit 8890bd6
Showing
55 changed files
with
804 additions
and
934 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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import './click' | ||
import './keydown' | ||
import './keypress' | ||
import './keyup' | ||
|
||
export {behavior} from './registry' | ||
export type {BehaviorPlugin} from './registry' |
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,104 @@ | ||
/* eslint-disable @typescript-eslint/no-use-before-define */ | ||
|
||
import {setUISelection} from '../../document' | ||
import { | ||
focus, | ||
getTabDestination, | ||
getValue, | ||
hasOwnSelection, | ||
input, | ||
isContentEditable, | ||
isEditable, | ||
isElementType, | ||
moveSelection, | ||
selectAll, | ||
setSelectionRange, | ||
} from '../../utils' | ||
import {BehaviorPlugin} from '.' | ||
import {behavior} from './registry' | ||
|
||
behavior.keydown = (event, target, config) => { | ||
return ( | ||
keydownBehavior[event.key]?.(event, target, config) ?? | ||
combinationBehavior(event, target, config) | ||
) | ||
} | ||
|
||
const keydownBehavior: { | ||
[key: string]: BehaviorPlugin<'keydown'> | undefined | ||
} = { | ||
ArrowLeft: (event, target) => () => moveSelection(target, -1), | ||
ArrowRight: (event, target) => () => moveSelection(target, 1), | ||
Backspace: (event, target, config) => { | ||
if (isEditable(target)) { | ||
return () => { | ||
input(config, target, '', 'deleteContentBackward') | ||
} | ||
} | ||
}, | ||
Delete: (event, target, config) => { | ||
if (isEditable(target)) { | ||
return () => { | ||
input(config, target, '', 'deleteContentForward') | ||
} | ||
} | ||
}, | ||
End: (event, target) => { | ||
if ( | ||
isElementType(target, ['input', 'textarea']) || | ||
isContentEditable(target) | ||
) { | ||
return () => { | ||
const newPos = getValue(target)?.length ?? /* istanbul ignore next */ 0 | ||
setSelectionRange(target, newPos, newPos) | ||
} | ||
} | ||
}, | ||
Home: (event, target) => { | ||
if ( | ||
isElementType(target, ['input', 'textarea']) || | ||
isContentEditable(target) | ||
) { | ||
return () => { | ||
setSelectionRange(target, 0, 0) | ||
} | ||
} | ||
}, | ||
PageDown: (event, target) => { | ||
if (isElementType(target, ['input'])) { | ||
return () => { | ||
const newPos = getValue(target).length | ||
setSelectionRange(target, newPos, newPos) | ||
} | ||
} | ||
}, | ||
PageUp: (event, target) => { | ||
if (isElementType(target, ['input'])) { | ||
return () => { | ||
setSelectionRange(target, 0, 0) | ||
} | ||
} | ||
}, | ||
Tab: (event, target, {keyboardState}) => { | ||
return () => { | ||
const dest = getTabDestination(target, keyboardState.modifiers.Shift) | ||
focus(dest) | ||
if (hasOwnSelection(dest)) { | ||
setUISelection(dest, { | ||
anchorOffset: 0, | ||
focusOffset: dest.value.length, | ||
}) | ||
} | ||
} | ||
}, | ||
} | ||
|
||
const combinationBehavior: BehaviorPlugin<'keydown'> = ( | ||
event, | ||
target, | ||
config, | ||
) => { | ||
if (event.code === 'KeyA' && config.keyboardState.modifiers.Control) { | ||
return () => selectAll(target) | ||
} | ||
} |
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,68 @@ | ||
/* eslint-disable @typescript-eslint/no-use-before-define */ | ||
|
||
import {dispatchUIEvent} from '..' | ||
import {input, isContentEditable, isEditable, isElementType} from '../../utils' | ||
import {behavior} from './registry' | ||
|
||
behavior.keypress = (event, target, config) => { | ||
if (event.key === 'Enter') { | ||
if ( | ||
isElementType(target, 'button') || | ||
(isElementType(target, 'input') && | ||
ClickInputOnEnter.includes(target.type)) || | ||
(isElementType(target, 'a') && Boolean(target.href)) | ||
) { | ||
return () => { | ||
dispatchUIEvent(config, target, 'click') | ||
} | ||
} else if (isElementType(target, 'input')) { | ||
const form = target.form | ||
const submit = form?.querySelector( | ||
'input[type="submit"], button:not([type]), button[type="submit"]', | ||
) | ||
if (submit) { | ||
return () => dispatchUIEvent(config, submit, 'click') | ||
} else if ( | ||
form && | ||
SubmitSingleInputOnEnter.includes(target.type) && | ||
form.querySelectorAll('input').length === 1 | ||
) { | ||
return () => dispatchUIEvent(config, form, 'submit') | ||
} else { | ||
return | ||
} | ||
} | ||
} | ||
|
||
if (isEditable(target)) { | ||
const inputType = | ||
event.key === 'Enter' | ||
? isContentEditable(target) && !config.keyboardState.modifiers.Shift | ||
? 'insertParagraph' | ||
: 'insertLineBreak' | ||
: 'insertText' | ||
const inputData = event.key === 'Enter' ? '\n' : event.key | ||
|
||
return () => input(config, target, inputData, inputType) | ||
} | ||
} | ||
|
||
const ClickInputOnEnter = [ | ||
'button', | ||
'color', | ||
'file', | ||
'image', | ||
'reset', | ||
'submit', | ||
] | ||
|
||
const SubmitSingleInputOnEnter = [ | ||
'email', | ||
'month', | ||
'password', | ||
'search', | ||
'tel', | ||
'text', | ||
'url', | ||
'week', | ||
] |
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,20 @@ | ||
/* eslint-disable @typescript-eslint/no-use-before-define */ | ||
|
||
import {isClickableInput} from '../../utils' | ||
import {dispatchUIEvent} from '..' | ||
import {BehaviorPlugin} from '.' | ||
import {behavior} from './registry' | ||
|
||
behavior.keyup = (event, target, config) => { | ||
return keyupBehavior[event.key]?.(event, target, config) | ||
} | ||
|
||
const keyupBehavior: { | ||
[key: string]: BehaviorPlugin<'keyup'> | undefined | ||
} = { | ||
' ': (event, target, config) => { | ||
if (isClickableInput(target)) { | ||
return () => dispatchUIEvent(config, target, 'click') | ||
} | ||
}, | ||
} |
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,10 @@ | ||
import {eventMap as baseEventMap} from '@testing-library/dom/dist/event-map.js' | ||
|
||
export const eventMap = { | ||
...baseEventMap, | ||
|
||
beforeInput: { | ||
EventType: 'InputEvent', | ||
defaultInit: {bubbles: true, cancelable: true, composed: 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
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
Oops, something went wrong.