Skip to content

Instantly share code, notes, and snippets.

@ericdfields
Last active August 29, 2015 14:19
Show Gist options
  • Save ericdfields/f6bdd8118229a55499af to your computer and use it in GitHub Desktop.
Save ericdfields/f6bdd8118229a55499af to your computer and use it in GitHub Desktop.
Disable backspace navigation when textarea has content react mixin
module.exports = {
disableBackspaceNavEvent: function(event) {
if (event.keyCode == 8) {
if (event.target.type && event.target.type.match(/text/)) {
return true
} else {
event.preventDefault()
}
}
},
disableBackspaceNav: function() {
window.removeEventListener('keydown', this.disableBackspaceNavEvent)
window.addEventListener('keydown', this.disableBackspaceNavEvent)
},
enableBackspaceNav: function() {
window.removeEventListener('keydown',this.disableBackspaceNavEvent)
},
componentWillUnmount: function() {
this.enableBackspaceNav()
}
}
var React = require('react')
var BackspaceNavHandler = require('backspace_nav_handler_mixins')
module.exports = React.createClass({
mixins: [ BackspaceNavHandler ],
render: function() {
return (
<textarea onBlur={this.disableBackspaceNav} onFocus={this.enableBackspaceNav} />
)
}
})
@ericdfields
Copy link
Author

Ran into a situation where the component was visible but user brought focus to an entirely unrelated field on the page and of course backspace wouldn't work. This should fix that but will probably fail in contenteditables. Needs a good "is DOM element and input" test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment