Ghost/ghost/admin/app/components/gh-search-input-trigger.js
Kevin Ansfield 1543ffe6ed 🐛 Fixed search losing focus when removing search term (#883)
closes https://github.com/TryGhost/Ghost/issues/9075

- ensure we re-focus the input element after closing the dropdown so that it's not necessary to click on the search input again after using backspace
- removes unnecessary use of `run.scheduleOnce`
  - this was causing the error in TryGhost/Ghost#9075 because search was happening out of sync with the open/close of the dropdown in turn causing `ember-basic-dropdown` to try measuring non-existent elements
  - alternative fix would be to wrap each open/close, focus, and search call in separate `run.scheduleOnce('afterRender', ...)` calls
- remove use of `invokeAction`, it's not been necessary in Ember for a while now and we should be calling methods directly where possible for easier debugging
2017-10-03 12:00:25 +07:00

55 lines
1.3 KiB
JavaScript

import Component from '@ember/component';
import {isBlank} from '@ember/utils';
export default Component.extend({
open() {
this.get('select.actions').open();
},
close() {
this.get('select.actions').close();
},
_focusInput() {
this.$('input')[0].focus();
},
actions: {
captureMouseDown(e) {
e.stopPropagation();
},
search(term) {
// open dropdown if not open and term is present
// close dropdown if open and term is blank
if (isBlank(term) === this.get('select.isOpen')) {
isBlank(term) ? this.close() : this.open();
// ensure focus isn't lost when dropdown is closed
if (isBlank(term)) {
this._focusInput();
}
}
this.get('select').actions.search(term);
},
focusInput() {
this._focusInput();
},
resetInput() {
this.$('input').val('');
},
handleKeydown(e) {
let select = this.get('select');
// TODO: remove keycode check once EPS is updated to 1.0
if (!select.isOpen || e.keyCode === 32) {
e.stopPropagation();
}
}
}
});