From 90eb2fec5e6e28b05f0b9511e8e539bcc42e0033 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Wed, 7 Feb 2018 15:21:34 +0100 Subject: Escape resets the search box --- src/client/react/components/container/Search.js | 57 ++++++++++++++----------- 1 file changed, 32 insertions(+), 25 deletions(-) (limited to 'src/client/react/components/container/Search.js') diff --git a/src/client/react/components/container/Search.js b/src/client/react/components/container/Search.js index 32996e3..8ca386c 100644 --- a/src/client/react/components/container/Search.js +++ b/src/client/react/components/container/Search.js @@ -51,32 +51,39 @@ class Search extends React.Component { } onKeyDown(event) { - if (event.key === 'ArrowUp' || event.key === 'ArrowDown' || event.key === 'Enter') { - event.preventDefault(); - switch (event.key) { - case 'ArrowUp': - this.props.dispatch(changeSelectedResult(-1)); - break; - case 'ArrowDown': - this.props.dispatch(changeSelectedResult(+1)); - break; - case 'Enter': { - const result = this.props.selectedResult || this.props.results[0]; - const urlUser = userFromMatch(this.props.match); - - if (result === urlUser) { - // EDGE CASE: The user is set if the user changes, but it doesn't - // change if the result is already the one we are viewing. - // Therefor, we need to dispatch the SET_USER command manually. - this.props.dispatch(setUser(urlUser)); - } else if (result) { - this.props.history.push(`/${result}`); - } - break; + const urlUser = userFromMatch(this.props.match); + const result = this.props.selectedResult || this.props.results[0]; + + switch (event.key) { + case 'ArrowUp': + event.preventDefault(); + this.props.dispatch(changeSelectedResult(-1)); + break; + + case 'ArrowDown': + event.preventDefault(); + this.props.dispatch(changeSelectedResult(+1)); + break; + + case 'Escape': + event.preventDefault(); + this.props.dispatch(setUser(urlUser)); + break; + + case 'Enter': + event.preventDefault(); + if (result === urlUser) { + // EDGE CASE: The user is set if the user changes, but it doesn't + // change if the result is already the one we are viewing. + // Therefor, we need to dispatch the SET_USER command manually. + this.props.dispatch(setUser(urlUser)); + } else if (result) { + this.props.history.push(`/${result}`); } - default: - throw new Error('This should never happen... pls?'); - } + break; + + default: + // Do nothing } } -- cgit v1.1