-
Notifications
You must be signed in to change notification settings - Fork 162
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
Bugfix nonprintable keys #199
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Dictionary of keys whose name is not a single symbol or character | ||
*/ | ||
import dictionaryFrom from '../utils/object/dictionaryFrom'; | ||
import translateToKey from '../vendor/react-dom/translateToKey'; | ||
|
||
const NonPrintableKeysDictionary = | ||
dictionaryFrom(Object.values(translateToKey), true); | ||
|
||
export default NonPrintableKeysDictionary; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import NonPrintableKeysDictionary from '../../const/NonPrintableKeysDictionary'; | ||
|
||
/** | ||
* Whether the specified key is a valid key name that is not a single character or | ||
* symbol | ||
* @param {ReactKeyName} keyName Name of the key | ||
* @returns {boolean} Whether the key is a valid special key | ||
*/ | ||
function isNonPrintableKeyName(keyName) { | ||
return !!NonPrintableKeysDictionary[keyName]; | ||
} | ||
|
||
export default isNonPrintableKeyName; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import isSpecialKey from './isSpecialKey'; | ||
import isNonPrintableKeyName from './isNonPrintableKeyName'; | ||
import Configuration from '../../lib/Configuration'; | ||
|
||
function isValidKey(keyName) { | ||
return isSpecialKey(keyName) || String.fromCharCode(keyName.charCodeAt(0)) === keyName; | ||
return isNonPrintableKeyName(keyName) || | ||
String.fromCharCode(keyName.charCodeAt(0)) === keyName || | ||
Configuration.option('customKeyCodes')[keyName]; | ||
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. ✅🐛Fix this line - incorrect way of accepting custom keys (it's a map of key codes to key names and this lookup will always fail). |
||
} | ||
|
||
export class InvalidKeyNameError extends Error { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import KeysWithoutPressEventDictionary from '../../const/KeysWithoutPressEventDictionary'; | ||
import isNonPrintableKeyName from '../parsing-key-maps/isNonPrintableKeyName'; | ||
|
||
/** | ||
* Whether the specified key name is for a key that has a native keypress event | ||
* @param {NormalizedKeyName} keyName Name of the key | ||
* @returns {Boolean} Whether the key has a native keypress event | ||
*/ | ||
function hasKeyPressEvent(keyName) { | ||
return !KeysWithoutPressEventDictionary[keyName]; | ||
return !isNonPrintableKeyName(keyName); | ||
} | ||
|
||
export default hasKeyPressEvent; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
*/ | ||
|
||
import getEventCharCode from './getEventCharCode'; | ||
import translateToKey from './translateToKey'; | ||
|
||
/** | ||
* Normalization of deprecated HTML5 `key` values | ||
|
@@ -28,50 +29,6 @@ const normalizeKey = { | |
MozPrintableKey: 'Unidentified', | ||
}; | ||
|
||
/** | ||
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. 🐥☂️Abstract React's list of non-printable keys into a separate file, so it can be re-used by |
||
* Translation from legacy `keyCode` to HTML5 `key` | ||
* Only special keys supported, all others depend on keyboard layout or browser | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Key_names | ||
*/ | ||
const translateToKey = { | ||
'8': 'Backspace', | ||
'9': 'Tab', | ||
'12': 'Clear', | ||
'13': 'Enter', | ||
'16': 'Shift', | ||
'17': 'Control', | ||
'18': 'Alt', | ||
'19': 'Pause', | ||
'20': 'CapsLock', | ||
'27': 'Escape', | ||
'32': ' ', | ||
'33': 'PageUp', | ||
'34': 'PageDown', | ||
'35': 'End', | ||
'36': 'Home', | ||
'37': 'ArrowLeft', | ||
'38': 'ArrowUp', | ||
'39': 'ArrowRight', | ||
'40': 'ArrowDown', | ||
'45': 'Insert', | ||
'46': 'Delete', | ||
'112': 'F1', | ||
'113': 'F2', | ||
'114': 'F3', | ||
'115': 'F4', | ||
'116': 'F5', | ||
'117': 'F6', | ||
'118': 'F7', | ||
'119': 'F8', | ||
'120': 'F9', | ||
'121': 'F10', | ||
'122': 'F11', | ||
'123': 'F12', | ||
'144': 'NumLock', | ||
'145': 'ScrollLock', | ||
'224': 'Meta', | ||
}; | ||
|
||
/** | ||
* @param {object} nativeEvent Native browser event. | ||
* @return {string} Normalized `key` property. | ||
|
@@ -97,6 +54,7 @@ function reactsGetEventKey(nativeEvent) { | |
// thus be captured by `keypress`, no other non-printable key should. | ||
return charCode === 13 ? 'Enter' : String.fromCharCode(charCode); | ||
} | ||
|
||
if (nativeEvent.type === 'keydown' || nativeEvent.type === 'keyup') { | ||
// While user keyboard layout determines the actual meaning of each | ||
// `keyCode` value, almost all function keys have a universal value. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Translation from legacy `keyCode` to HTML5 `key` | ||
* Only special keys supported, all others depend on keyboard layout or browser | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Key_names | ||
*/ | ||
const translateToKey = { | ||
'8': 'Backspace', | ||
'9': 'Tab', | ||
'12': 'Clear', | ||
'13': 'Enter', | ||
'16': 'Shift', | ||
'17': 'Control', | ||
'18': 'Alt', | ||
'19': 'Pause', | ||
'20': 'CapsLock', | ||
'27': 'Escape', | ||
'32': ' ', | ||
'33': 'PageUp', | ||
'34': 'PageDown', | ||
'35': 'End', | ||
'36': 'Home', | ||
'37': 'ArrowLeft', | ||
'38': 'ArrowUp', | ||
'39': 'ArrowRight', | ||
'40': 'ArrowDown', | ||
'45': 'Insert', | ||
'46': 'Delete', | ||
'112': 'F1', | ||
'113': 'F2', | ||
'114': 'F3', | ||
'115': 'F4', | ||
'116': 'F5', | ||
'117': 'F6', | ||
'118': 'F7', | ||
'119': 'F8', | ||
'120': 'F9', | ||
'121': 'F10', | ||
'122': 'F11', | ||
'123': 'F12', | ||
'144': 'NumLock', | ||
'145': 'ScrollLock', | ||
'224': 'Meta', | ||
}; | ||
|
||
export default translateToKey; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐥 New dictionary of non-printable keys, that takes its values from the same list that the React normalization algorithm uses.