Chromium Code Reviews| Index: ui/webui/resources/cr_elements/cr_toolbar/cr_toolbar_search_field.js |
| diff --git a/ui/webui/resources/cr_elements/cr_toolbar/cr_toolbar_search_field.js b/ui/webui/resources/cr_elements/cr_toolbar/cr_toolbar_search_field.js |
| index 51603defca0243af2c0bdc3ce25ad7bbb9c6f152..cfe35a966625e9583af90680d6f6c70101b9e767 100644 |
| --- a/ui/webui/resources/cr_elements/cr_toolbar/cr_toolbar_search_field.js |
| +++ b/ui/webui/resources/cr_elements/cr_toolbar/cr_toolbar_search_field.js |
| @@ -2,7 +2,6 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -// TODO(tsergeant): Add tests for cr-toolbar-search-field. |
| Polymer({ |
| is: 'cr-toolbar-search-field', |
| @@ -14,6 +13,14 @@ Polymer({ |
| reflectToAttribute: true, |
| }, |
| + showingSearch: { |
| + type: Boolean, |
| + value: false, |
| + notify: true, |
| + observer: 'showingSearchChanged_', |
| + reflectToAttribute: true |
| + }, |
| + |
| // Prompt text to display in the search field. |
| label: String, |
| @@ -72,6 +79,16 @@ Polymer({ |
| return this.searchFocused_; |
| }, |
| + showAndFocus: function() { |
| + this.showingSearch = true; |
| + this.focus_(); |
| + }, |
| + |
| + /** @private */ |
| + focus_: function() { |
| + this.getSearchInput().focus(); |
| + }, |
| + |
| /** |
| * @param {boolean} narrow |
| * @return {number} |
| @@ -114,6 +131,12 @@ Polymer({ |
| this.showingSearch = true; |
| }, |
| + /** @private */ |
| + onSearchTermKeydown_: function(e) { |
| + if (e.key == 'Escape') |
| + this.showingSearch = false; |
| + }, |
| + |
| /** |
| * @param {Event} e |
| * @private |
| @@ -130,5 +153,24 @@ Polymer({ |
| clearSearch_: function(e) { |
| this.setValue(''); |
| this.getSearchInput().focus(); |
|
dpapad
2016/12/13 20:02:58
Nit: Re-use
this.focus_();
tsergeant
2016/12/14 00:05:55
Done.
|
| - } |
| + }, |
| + |
| + /** |
| + * @param {boolean} current |
| + * @param {boolean|undefined} previous |
| + * @private |
| + */ |
| + showingSearchChanged_: function(current, previous) { |
| + // Prevent unnecessary 'search-changed' event from firing on startup. |
| + if (previous == undefined) |
| + return; |
| + |
| + if (this.showingSearch) { |
| + this.focus_(); |
| + return; |
| + } |
| + |
| + this.setValue(''); |
| + this.getSearchInput().blur(); |
| + }, |
| }); |