-
Notifications
You must be signed in to change notification settings - Fork 199
feat: add util function for common keyboard event handling #715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zombieJ
merged 6 commits into
react-component:master
from
aojunhao123:feat/keycode-util
Dec 30, 2025
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
91c7769
feat: add util function for better keyboard event handling
aojunhao123 db5b908
chore: adjust
aojunhao123 12a1346
chore: adjust
aojunhao123 d07a208
add test case
aojunhao123 88d5bbb
test case
aojunhao123 2bda4bc
chore: adjust
aojunhao123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -517,6 +517,27 @@ const KeyCode = { | |
| return false; | ||
| } | ||
| }, | ||
|
|
||
| isEditableTarget: function isEditableTarget(e: KeyboardEvent) { | ||
| const target = e.target; | ||
|
|
||
| if (!(target instanceof HTMLElement)) { | ||
| return false; | ||
| } | ||
|
|
||
| const tagName = target.tagName; | ||
| if ( | ||
| e.isComposing || | ||
|
aojunhao123 marked this conversation as resolved.
Outdated
|
||
| tagName === 'INPUT' || | ||
| tagName === 'TEXTAREA' || | ||
| tagName === 'SELECT' || | ||
| target.isContentEditable | ||
| ) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
|
Comment on lines
+529
to
+538
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| }, | ||
| }; | ||
|
|
||
| export default KeyCode; | ||
This file contains hidden or 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,54 @@ | ||
| import KeyCode from '../src/KeyCode'; | ||
|
|
||
| describe('KeyCode.isEditableTarget', () => { | ||
| function testIsEditableTarget( | ||
| listenTarget: EventTarget, | ||
| dispatchTarget: EventTarget, | ||
| init?: KeyboardEventInit, | ||
| ) { | ||
| let result: boolean | undefined; | ||
|
|
||
| const handler = (e: KeyboardEvent) => { | ||
| result = KeyCode.isEditableTarget(e); | ||
| }; | ||
|
|
||
| listenTarget.addEventListener('keydown', handler); | ||
| dispatchTarget.dispatchEvent(new KeyboardEvent('keydown', init)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. testinglib 有创建 event 的函数,直接写一个 element 再 createEvent 就行了,测试不用太散,直接写一个 lement 数组,遍历测试就行: const eleList = [
{
element: 'div',
isContentEditable: true,
expect: true,
},
...
];另外 IME composing 这个不用测,因为你代码里也没写 |
||
| listenTarget.removeEventListener('keydown', handler); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| it('check non-editable target', () => { | ||
| const result = testIsEditableTarget(window, window); | ||
|
|
||
| expect(result).toBe(false); | ||
| }); | ||
|
|
||
| it('IME composing', () => { | ||
| const input = document.createElement('input'); | ||
| const result = testIsEditableTarget(input, input, { isComposing: true }); | ||
|
|
||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| it('check input target', () => { | ||
| const input = document.createElement('input'); | ||
| document.body.appendChild(input); | ||
|
|
||
| const result = testIsEditableTarget(window, input, { bubbles: true }); | ||
|
|
||
| document.body.removeChild(input); | ||
|
|
||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| it('check contentEditable target', () => { | ||
| const editable = document.createElement('div'); | ||
| // mock isContentEditable cause JSDOM don't support it | ||
| Object.defineProperty(editable, 'isContentEditable', { value: true }); | ||
| const result = testIsEditableTarget(editable, editable); | ||
|
|
||
| expect(result).toBe(true); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.