Skip to content
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

Optionally propagate keydown events #140

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ Default: true

If false, the default classNames are removed from the typeahead.

#### props.propagateKeyDownEvents

Type: `boolean`
Default: false

If true, allows keyDown events to propagate. This is useful if you want the `tab` key to focus the next element, for example.

#### props.customListComponent

Type: `React Component`
Expand Down
33 changes: 26 additions & 7 deletions src/tokenizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ var TypeaheadTokenizer = React.createClass({
React.PropTypes.func
]),
maxVisible: React.PropTypes.number,
defaultClassNames: React.PropTypes.bool
defaultClassNames: React.PropTypes.bool,
propagateKeyDownEvents: React.PropTypes.bool
},

getInitialState: function() {
Expand Down Expand Up @@ -78,6 +79,7 @@ var TypeaheadTokenizer = React.createClass({
onBlur: function(event) {},
onTokenAdd: function() {},
onTokenRemove: function() {}
propagateKeyDownEvents: false,
};
},

Expand Down Expand Up @@ -126,6 +128,10 @@ var TypeaheadTokenizer = React.createClass({
if (event.keyCode === KeyEvent.DOM_VK_BACK_SPACE) {
return this._handleBackspace(event);
}
// or tabs
if (event.keyCode === KeyEvent.DOM_VK_TAB) {
this._handleTab(event);
}
this.props.onKeyDown(event);
},

Expand All @@ -135,17 +141,29 @@ var TypeaheadTokenizer = React.createClass({
return;
}

// Remove token ONLY when bksp pressed at beginning of line
// without a selection
var entry = this.refs.typeahead.refs.entry;
if (entry.selectionStart == entry.selectionEnd &&
entry.selectionStart == 0) {
if (this._inputIsEmpty()) {
this._removeTokenForValue(
this.state.selected[this.state.selected.length - 1]);
event.preventDefault();
}
},

_handleTab: function(event) {
// Intercept tab to prevent focusing on next element, unless
// nothing is selected and the input is empty
var entry = this.refs.typeahead.refs.entry;
if (!this._inputIsEmpty()) {
event.preventDefault();
}
},

_inputIsEmpty: function() {
// beginning of line, without a selection
var entry = this.refs.typeahead.refs.entry;
return entry.selectionStart == entry.selectionEnd &&
entry.selectionStart == 0;
},

_removeTokenForValue: function(value) {
var index = this.state.selected.indexOf(value);
if (index == -1) {
Expand Down Expand Up @@ -195,7 +213,8 @@ var TypeaheadTokenizer = React.createClass({
onBlur={this.props.onBlur}
displayOption={this.props.displayOption}
defaultClassNames={this.props.defaultClassNames}
filterOption={this.props.filterOption} />
filterOption={this.props.filterOption}
propagateKeyDownEvents={this.props.propagateKeyDownEvents} />
</div>
);
}
Expand Down
8 changes: 6 additions & 2 deletions src/typeahead/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var Typeahead = React.createClass({
React.PropTypes.func
]),
defaultClassNames: React.PropTypes.bool,
propagateKeyDownEvents: React.PropTypes.bool,
customListComponent: React.PropTypes.oneOfType([
React.PropTypes.element,
React.PropTypes.func
Expand All @@ -75,6 +76,7 @@ var Typeahead = React.createClass({
onBlur: function(event) {},
filterOption: null,
defaultClassNames: true,
propagateKeyDownEvents: false,
customListComponent: TypeaheadSelector
};
},
Expand Down Expand Up @@ -279,8 +281,10 @@ var Typeahead = React.createClass({
} else {
return this.props.onKeyDown(event);
}
// Don't propagate the keystroke back to the DOM/browser
event.preventDefault();
// By default, don't propagate the keystroke back to the DOM/browser
if (!this.props.propagateKeyDownEvents) {
event.preventDefault();
}
},

componentWillReceiveProps: function(nextProps) {
Expand Down