@@ -16,12 +16,13 @@ limitations under the License.
1616
1717'use strict' ;
1818
19- var React = require ( 'react' ) ;
19+ import React from 'react' ;
2020import { _t } from 'matrix-react-sdk/lib/languageHandler' ;
21- var sdk = require ( 'matrix-react-sdk' )
22- var dis = require ( 'matrix-react-sdk/lib/dispatcher' ) ;
23- var rate_limited_func = require ( 'matrix-react-sdk/lib/ratelimitedfunc' ) ;
24- var AccessibleButton = require ( 'matrix-react-sdk/lib/components/views/elements/AccessibleButton' ) ;
21+ import KeyCode from 'matrix-react-sdk/lib/KeyCode' ;
22+ import sdk from 'matrix-react-sdk' ;
23+ import dis from 'matrix-react-sdk/lib/dispatcher' ;
24+ import RateLimitedFunc from 'matrix-react-sdk/lib/ratelimitedfunc' ;
25+ import AccessibleButton from 'matrix-react-sdk/lib/components/views/elements/AccessibleButton' ;
2526
2627module . exports = React . createClass ( {
2728 displayName : 'SearchBox' ,
@@ -39,26 +40,25 @@ module.exports = React.createClass({
3940
4041 componentDidMount : function ( ) {
4142 this . dispatcherRef = dis . register ( this . onAction ) ;
43+ document . addEventListener ( 'keydown' , this . _onKeyDown ) ;
4244 } ,
4345
4446 componentWillUnmount : function ( ) {
4547 dis . unregister ( this . dispatcherRef ) ;
48+ document . removeEventListener ( 'keydown' , this . _onKeyDown ) ;
4649 } ,
4750
4851 onAction : function ( payload ) {
4952 switch ( payload . action ) {
50- /* Disabling this as I find it really really annoying, and was used to the
51- previous behaviour - see https://github.com/vector-im/riot-web/issues/3348
52- // Clear up the text field when a room is selected.
5353 case 'view_room' :
54- if (this.refs.search) {
54+ if ( this . refs . search && payload . clear_search ) {
5555 this . _clearSearch ( ) ;
5656 }
5757 break ;
58- */
5958 case 'focus_room_filter' :
6059 if ( this . refs . search ) {
6160 this . refs . search . focus ( ) ;
61+ this . refs . search . select ( ) ;
6262 }
6363 break ;
6464 }
@@ -70,7 +70,7 @@ module.exports = React.createClass({
7070 this . onSearch ( ) ;
7171 } ,
7272
73- onSearch : new rate_limited_func (
73+ onSearch : new RateLimitedFunc (
7474 function ( ) {
7575 this . props . onSearch ( this . refs . search . value ) ;
7676 } ,
@@ -90,6 +90,17 @@ module.exports = React.createClass({
9090 }
9191 } ,
9292
93+ _onKeyDown : function ( ev ) {
94+ // Only do anything when the key event target is the search input
95+ if ( ev . target !== this . refs . search ) return ;
96+ switch ( ev . keyCode ) {
97+ case KeyCode . ESCAPE :
98+ this . _clearSearch ( ) ;
99+ dis . dispatch ( { action : 'focus_composer' } ) ;
100+ break ;
101+ }
102+ } ,
103+
93104 _clearSearch : function ( ) {
94105 this . refs . search . value = "" ;
95106 this . onChange ( ) ;
0 commit comments