2017-12-15 21:39:18 +00:00
|
|
|
import { reads } from '@ember/object/computed';
|
|
|
|
import Component from '@ember/component';
|
|
|
|
import { run } from '@ember/runloop';
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
// Passed to the component (mutable)
|
|
|
|
searchTerm: null,
|
|
|
|
|
|
|
|
// Used as a debounce buffer
|
2017-12-15 21:39:18 +00:00
|
|
|
_searchTerm: reads('searchTerm'),
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
// Used to throttle sets to searchTerm
|
|
|
|
debounce: 150,
|
|
|
|
|
2018-10-30 17:50:24 +00:00
|
|
|
// A hook that's called when the search value changes
|
|
|
|
onChange() {},
|
|
|
|
|
2017-09-30 00:41:12 +00:00
|
|
|
classNames: ['search-box', 'field', 'has-addons'],
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
actions: {
|
|
|
|
setSearchTerm(e) {
|
|
|
|
this.set('_searchTerm', e.target.value);
|
2019-03-26 07:46:44 +00:00
|
|
|
run.debounce(this, updateSearch, this.debounce);
|
2017-09-19 14:47:10 +00:00
|
|
|
},
|
2019-02-13 16:44:29 +00:00
|
|
|
|
|
|
|
clear() {
|
|
|
|
this.set('_searchTerm', '');
|
2019-03-26 07:46:44 +00:00
|
|
|
run.debounce(this, updateSearch, this.debounce);
|
2019-02-13 16:44:29 +00:00
|
|
|
},
|
2017-09-19 14:47:10 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
function updateSearch() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const newTerm = this._searchTerm;
|
2018-10-30 17:50:24 +00:00
|
|
|
this.onChange(newTerm);
|
|
|
|
this.set('searchTerm', newTerm);
|
2017-09-19 14:47:10 +00:00
|
|
|
}
|