From fc03118970d4fc949609c85b832144abff0b1232 Mon Sep 17 00:00:00 2001 From: Raphael Gruber Date: Wed, 22 Mar 2017 23:13:53 +0100 Subject: [PATCH] adds Search onSelect callback when dropdown is not active (#1260) * adds Search onSelect callback when dropdown is not active Signed-off-by: Raphael Gruber * Added onSelect inside onEnter too --- src/js/components/Search.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/js/components/Search.js b/src/js/components/Search.js index f3c4ee10ae..8f4727941e 100644 --- a/src/js/components/Search.js +++ b/src/js/components/Search.js @@ -163,7 +163,10 @@ export default class Search extends Component { } _onInputKeyDown (event) { - const { inline, suggestions, onKeyDown } = this.props; + const { + inline, onSelect, suggestions, activeSuggestionIndex, onKeyDown + } = this.props; + const enter = 13; const { dropActive } = this.state; if (suggestions) { const up = 38; @@ -177,6 +180,14 @@ export default class Search extends Component { } } } + if (!dropActive && onSelect && event.keyCode === enter) { + const suggestion = suggestions[activeSuggestionIndex]; + + onSelect({ + target: this._inputRef || this._controlRef, + suggestion: suggestion + }, false); + } if (onKeyDown) { onKeyDown(event); } @@ -284,6 +295,10 @@ export default class Search extends Component { suggestion: suggestion }, true); } + } else { + onSelect({ + target: this._inputRef || this._controlRef + }, false); } } @@ -482,6 +497,7 @@ Search.propTypes = { inline: PropTypes.bool, onDOMChange: PropTypes.func, onSelect: PropTypes.func, + onKeyDown: PropTypes.func, pad: PropTypes.oneOf(['small', 'medium']), placeHolder: PropTypes.string, responsive: PropTypes.bool,