Skip to content

Update search input component to glimmer #814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ module.exports = {
'no-console': 'off',
'ember/no-new-mixins': 'off',
'ember/no-mixins': 'off',
'ember/native-classes': 'off',
'ember/require-tagless-components': 'off',
'ember/no-test-this-render': 'off',
'ember/no-classic-classes': 'off',
'ember/no-get': 'off',
'ember/no-actions-hash': 'off',
'ember/no-classic-components': 'off',
'ember/no-private-routing-service': 'off',
},
Expand Down
33 changes: 33 additions & 0 deletions app/components/search-input.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<div class='search-input'>
<input
id='search-input'
type='search'
value={{this.value}}
oninput={{perform this.search value='target.value'}}
onfocus={{action 'onfocus'}}
onblur={{action 'onblur'}}
placeholder='Search'
data-test-search-input
/>
{{! Search results dropdown }}
<EmberTether
@target='#search-input'
@targetAttachment='bottom left'
@attachment='top left'
@constraints={{this._resultTetherConstraints}}
@class='ds-dropdown-results'
>
<SearchInput::Dropdown
@isVisible={{this._focused}}
@results={{this.searchService.results}}
@noResults={{if
(and
(and (not this.searchService.search.isRunning) this.queryIsPresent)
(eq this.searchService.results.length 0)
)
true
false
}}
/>
</EmberTether>
</div>
78 changes: 34 additions & 44 deletions app/components/search-input.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,65 @@
import { A } from '@ember/array';
import { inject as service } from '@ember/service';
import Component from '@ember/component';
import { get, set, computed } from '@ember/object';
import { isPresent, isEmpty } from '@ember/utils';
import Component from '@glimmer/component';
import { get } from '@ember/object';
import { isPresent } from '@ember/utils';
import { task, timeout } from 'ember-concurrency';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';

const SEARCH_DEBOUNCE_PERIOD = 300;
const SEARCH_CLOSE_PERIOD = 200;

export default Component.extend({
query: '',
export default class SearchInput extends Component {
@tracked query = '';
@tracked _focused = false;

classNames: ['search-input'],
searchService: service('search'),
@service('search') searchService;

_results: A(),
_focused: false,
_resultTetherConstraints: null,
_resultTetherConstraints = null;

constructor() {
super(...arguments);

init() {
this._resultTetherConstraints = [
{
to: 'window',
pin: ['left', 'right'],
},
];
this._super(...arguments);
},

noResults: computed(
'query',
'searchService.{results.[],search.isRunning}',
function () {
if (get(this, 'searchService.search.isRunning')) {
return false;
}
return (
isPresent(this.query) && isEmpty(get(this, 'searchService.results'))
);
}
),
}

get queryIsPresent() {
return isPresent(this.query);
}

search: task(function* (query) {
@task({ restartable: true }) *search(query) {
yield timeout(SEARCH_DEBOUNCE_PERIOD);

set(this, 'query', query);
this.query = query;

// Hide and don't run query if there's no search query
if (!query) {
return set(this, '_focused', false);
this._focused = false;
return;
}

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

yield get(this, 'searchService.search').perform(query);
}).restartable(),
}

closeMenu: task(function* () {
@task *closeMenu() {
yield timeout(SEARCH_CLOSE_PERIOD);

set(this, '_focused', false);
}),
this._focused = false;
}

actions: {
onfocus() {
set(this, '_focused', true);
},
@action onfocus() {
this._focused = true;
}

onblur() {
this.closeMenu.perform();
},
},
});
@action onblur() {
this.closeMenu.perform();
}
}
14 changes: 0 additions & 14 deletions app/templates/components/search-input.hbs

This file was deleted.

6 changes: 3 additions & 3 deletions tests/integration/components/search-input-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { fillIn, waitFor } from '@ember/test-helpers';
import { fillIn, render, waitFor } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import { set } from '@ember/object';

Expand Down Expand Up @@ -155,7 +155,7 @@ module('Integration | Component | search input', function (hooks) {
];
});

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

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

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

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

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

Expand Down