Skip to content

Commit 6283639

Browse files
authored
Merge pull request #814 from geneukum/update-search-input-component-to-glimmer
Update search input component to glimmer
2 parents ad22166 + 577eee3 commit 6283639

File tree

5 files changed

+70
-64
lines changed

5 files changed

+70
-64
lines changed

.eslintrc.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@ module.exports = {
2323
'no-console': 'off',
2424
'ember/no-new-mixins': 'off',
2525
'ember/no-mixins': 'off',
26-
'ember/native-classes': 'off',
2726
'ember/require-tagless-components': 'off',
28-
'ember/no-test-this-render': 'off',
2927
'ember/no-classic-classes': 'off',
3028
'ember/no-get': 'off',
31-
'ember/no-actions-hash': 'off',
3229
'ember/no-classic-components': 'off',
3330
'ember/no-private-routing-service': 'off',
3431
},

app/components/search-input.hbs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<div class='search-input'>
2+
<input
3+
id='search-input'
4+
type='search'
5+
value={{this.value}}
6+
oninput={{perform this.search value='target.value'}}
7+
onfocus={{action 'onfocus'}}
8+
onblur={{action 'onblur'}}
9+
placeholder='Search'
10+
data-test-search-input
11+
/>
12+
{{! Search results dropdown }}
13+
<EmberTether
14+
@target='#search-input'
15+
@targetAttachment='bottom left'
16+
@attachment='top left'
17+
@constraints={{this._resultTetherConstraints}}
18+
@class='ds-dropdown-results'
19+
>
20+
<SearchInput::Dropdown
21+
@isVisible={{this._focused}}
22+
@results={{this.searchService.results}}
23+
@noResults={{if
24+
(and
25+
(and (not this.searchService.search.isRunning) this.queryIsPresent)
26+
(eq this.searchService.results.length 0)
27+
)
28+
true
29+
false
30+
}}
31+
/>
32+
</EmberTether>
33+
</div>

app/components/search-input.js

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,65 @@
1-
import { A } from '@ember/array';
21
import { inject as service } from '@ember/service';
3-
import Component from '@ember/component';
4-
import { get, set, computed } from '@ember/object';
5-
import { isPresent, isEmpty } from '@ember/utils';
2+
import Component from '@glimmer/component';
3+
import { get } from '@ember/object';
4+
import { isPresent } from '@ember/utils';
65
import { task, timeout } from 'ember-concurrency';
6+
import { action } from '@ember/object';
7+
import { tracked } from '@glimmer/tracking';
78

89
const SEARCH_DEBOUNCE_PERIOD = 300;
910
const SEARCH_CLOSE_PERIOD = 200;
1011

11-
export default Component.extend({
12-
query: '',
12+
export default class SearchInput extends Component {
13+
@tracked query = '';
14+
@tracked _focused = false;
1315

14-
classNames: ['search-input'],
15-
searchService: service('search'),
16+
@service('search') searchService;
1617

17-
_results: A(),
18-
_focused: false,
19-
_resultTetherConstraints: null,
18+
_resultTetherConstraints = null;
19+
20+
constructor() {
21+
super(...arguments);
2022

21-
init() {
2223
this._resultTetherConstraints = [
2324
{
2425
to: 'window',
2526
pin: ['left', 'right'],
2627
},
2728
];
28-
this._super(...arguments);
29-
},
30-
31-
noResults: computed(
32-
'query',
33-
'searchService.{results.[],search.isRunning}',
34-
function () {
35-
if (get(this, 'searchService.search.isRunning')) {
36-
return false;
37-
}
38-
return (
39-
isPresent(this.query) && isEmpty(get(this, 'searchService.results'))
40-
);
41-
}
42-
),
29+
}
30+
31+
get queryIsPresent() {
32+
return isPresent(this.query);
33+
}
4334

44-
search: task(function* (query) {
35+
@task({ restartable: true }) *search(query) {
4536
yield timeout(SEARCH_DEBOUNCE_PERIOD);
4637

47-
set(this, 'query', query);
38+
this.query = query;
4839

4940
// Hide and don't run query if there's no search query
5041
if (!query) {
51-
return set(this, '_focused', false);
42+
this._focused = false;
43+
return;
5244
}
5345

5446
// ensure search results are visible if the menu was previously closed above
55-
set(this, '_focused', true);
47+
this._focused = true;
5648

5749
yield get(this, 'searchService.search').perform(query);
58-
}).restartable(),
50+
}
5951

60-
closeMenu: task(function* () {
52+
@task *closeMenu() {
6153
yield timeout(SEARCH_CLOSE_PERIOD);
6254

63-
set(this, '_focused', false);
64-
}),
55+
this._focused = false;
56+
}
6557

66-
actions: {
67-
onfocus() {
68-
set(this, '_focused', true);
69-
},
58+
@action onfocus() {
59+
this._focused = true;
60+
}
7061

71-
onblur() {
72-
this.closeMenu.perform();
73-
},
74-
},
75-
});
62+
@action onblur() {
63+
this.closeMenu.perform();
64+
}
65+
}

app/templates/components/search-input.hbs

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/integration/components/search-input-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { module, test } from 'qunit';
22
import { setupRenderingTest } from 'ember-qunit';
3-
import { fillIn, waitFor } from '@ember/test-helpers';
3+
import { fillIn, render, waitFor } from '@ember/test-helpers';
44
import hbs from 'htmlbars-inline-precompile';
55
import { set } from '@ember/object';
66

@@ -155,7 +155,7 @@ module('Integration | Component | search input', function (hooks) {
155155
];
156156
});
157157

158-
await this.render(hbs`{{search-input}}`);
158+
await render(hbs`<SearchInput/>`);
159159

160160
await fillIn('#search-input', 'model');
161161

@@ -171,7 +171,7 @@ module('Integration | Component | search input', function (hooks) {
171171
return [];
172172
});
173173

174-
await this.render(hbs`{{search-input}}`);
174+
await render(hbs`<SearchInput/>`);
175175

176176
await fillIn('#search-input', 'model');
177177

0 commit comments

Comments
 (0)