-
-
Notifications
You must be signed in to change notification settings - Fork 22
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
refactor: use glimmer component for search-input #205
Open
kennstenicht
wants to merge
6
commits into
ember-learn:main
Choose a base branch
from
kennstenicht:refactor-search-input
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0da9f43
refactor: format search-input template
kennstenicht 126835b
refactor: use native class for search-input
kennstenicht 887a408
fix: replace target _deprecations with _blank
kennstenicht 9e7d3b7
fix: set restartable on correct search task
kennstenicht e9d28be
fix: remove ember-concurrency from test-app
kennstenicht 4443b0b
Kick CI
kategengler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 64 additions & 41 deletions
105
guidemaker-ember-template/src/components/search-input.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,68 @@ | ||
{{!-- template-lint-disable link-rel-noopener no-action no-curly-component-invocation --}} | ||
{{#if this.searchService.index}} | ||
<input | ||
id="search-input" | ||
type="search" | ||
value={{this.value}} | ||
oninput={{perform this.search value="target.value"}} | ||
placeholder="Search the guides" | ||
autocomplete="off" | ||
onfocus={{action "onfocus"}} | ||
onblur={{action "onblur"}} | ||
data-test-search-input | ||
> | ||
<div class="search-input" ...attributes> | ||
{{#if this.searchService.index}} | ||
<input | ||
id="search-input" | ||
type="search" | ||
value={{this.value}} | ||
oninput={{perform this.search value="target.value"}} | ||
placeholder="Search the guides" | ||
autocomplete="off" | ||
data-test-search-input | ||
{{on "focus" this.onfocus}} | ||
{{on "blur" (perform this.closeMenu)}} | ||
/> | ||
|
||
{{!-- Search results dropdown --}} | ||
<EmberTether @target="#search-input" @targetAttachment="bottom right" @attachment="top right" @constraints={{this._resultTetherConstraints}} @class="ds-dropdown-results"> | ||
{{#if this.showDropdown}} | ||
<div class="ds-suggestions ds-dropdown-menu"> | ||
<DropdownHeader> | ||
Search Results | ||
</DropdownHeader> | ||
|
||
{{#each this.searchService.results as |result|}} | ||
<SearchResult @result={{result}} /> | ||
{{else}} | ||
<div class="algolia-docsearch-suggestion"> | ||
<div class="algolia-docsearch-suggestion--noresults"> | ||
<p> | ||
No results found. | ||
{{#if this.deprecationsGuideURL}} | ||
Try searching the <a href={{this.deprecationsGuideURL}} target="_deprecations">deprecations guide</a>. | ||
{{/if}} | ||
</p> | ||
{{! Search results dropdown }} | ||
<EmberTether | ||
@target="#search-input" | ||
@targetAttachment="bottom right" | ||
@attachment="top right" | ||
@constraints={{this._resultTetherConstraints}} | ||
@class="ds-dropdown-results" | ||
> | ||
{{#if this.showDropdown}} | ||
<div class="ds-suggestions ds-dropdown-menu" data-test-search-dropdown> | ||
<DropdownHeader> | ||
Search Results | ||
</DropdownHeader> | ||
{{#each this.searchService.results as |result|}} | ||
<SearchResult @result={{result}} /> | ||
{{else}} | ||
<div class="algolia-docsearch-suggestion"> | ||
<div | ||
class="algolia-docsearch-suggestion--noresults" | ||
data-test-search-noresults | ||
> | ||
<p> | ||
No results found. | ||
{{#if this.deprecationsGuideURL}} | ||
Try searching the | ||
<a | ||
href={{this.deprecationsGuideURL}} | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
deprecations guide | ||
</a>. | ||
{{/if}} | ||
</p> | ||
</div> | ||
</div> | ||
{{/each}} | ||
<div class="powered-by-algolia"> | ||
<a | ||
href="https://www.algolia.com/" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
<img | ||
src="/images/logos/search-by-algolia.svg" | ||
alt="Search Powered by Algolia" | ||
/> | ||
</a> | ||
</div> | ||
{{/each}} | ||
<div class="powered-by-algolia"> | ||
<a href="https://www.algolia.com/" target="_blank" rel="noopener"> | ||
<img src="/images/logos/search-by-algolia.svg" alt="Search Powered by Algolia"> | ||
</a> | ||
</div> | ||
</div> | ||
{{/if}} | ||
</EmberTether> | ||
{{/if}} | ||
{{/if}} | ||
</EmberTether> | ||
{{/if}} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,59 @@ | ||
/* eslint-disable ember/no-classic-components, ember/no-classic-classes, ember/require-tagless-components, prettier/prettier, ember/no-get, ember/no-actions-hash */ | ||
import { getOwner } from '@ember/application'; | ||
import Component from '@ember/component'; | ||
import { get, set } from '@ember/object'; | ||
import { and } from '@ember/object/computed'; | ||
import Component from '@glimmer/component'; | ||
import { tracked } from '@glimmer/tracking'; | ||
import { inject as service } from '@ember/service'; | ||
import { action } from '@ember/object'; | ||
import { task, timeout } from 'ember-concurrency'; | ||
import config from 'ember-get-config'; | ||
|
||
const SEARCH_DEBOUNCE_PERIOD = 300; | ||
const SEARCH_CLOSE_PERIOD = 200; | ||
|
||
export default Component.extend({ | ||
export default class SearchInputComponent extends Component { | ||
@service('search') searchService; | ||
|
||
classNames: ['search-input'], | ||
|
||
searchService: service('search'), | ||
|
||
_resultTetherConstraints: Object.freeze([ | ||
_resultTetherConstraints = [ | ||
{ | ||
to: 'window', | ||
pin: ['left','right'] | ||
} | ||
]), | ||
|
||
_focused: false, | ||
pin: ['left', 'right'], | ||
}, | ||
]; | ||
|
||
init() { | ||
this._super(...arguments); | ||
const config = getOwner(this).resolveRegistration('config:environment'); | ||
this.deprecationsGuideURL = config['deprecationsGuideURL']; | ||
}, | ||
@tracked _focused = false; | ||
@tracked query; | ||
@tracked value = ''; | ||
|
||
showDropdown: and('query', '_focused'), | ||
get deprecationsGuideURL() { | ||
return config['deprecationsGuideURL']; | ||
} | ||
|
||
search: task(function * (query) { | ||
get showDropdown() { | ||
return this.query && this._focused; | ||
} | ||
|
||
yield timeout(SEARCH_DEBOUNCE_PERIOD); | ||
search = task({ restartable: true }, async (query) => { | ||
await 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); | ||
return (this._focused = false); | ||
} | ||
|
||
// ensure search results are visible if the menu was previously closed above | ||
set(this, '_focused', true); | ||
|
||
yield get(this, 'searchService.search').perform(query, this.projectVersion); | ||
|
||
}).restartable(), | ||
this._focused = true; | ||
|
||
closeMenu: task(function * () { | ||
yield timeout(SEARCH_CLOSE_PERIOD); | ||
await this.searchService.search.perform(query, this.args.projectVersion); | ||
}); | ||
|
||
set(this, '_focused', false); | ||
}), | ||
closeMenu = task(async () => { | ||
await timeout(SEARCH_CLOSE_PERIOD); | ||
|
||
actions: { | ||
onfocus() { | ||
set(this, '_focused', true); | ||
}, | ||
|
||
onblur() { | ||
this.get('closeMenu').perform(); | ||
} | ||
this._focused = false; | ||
}); | ||
|
||
@action | ||
onfocus() { | ||
this._focused = true; | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,42 @@ | ||
/* eslint-disable ember/no-classic-classes, prettier/prettier, ember/no-get */ | ||
import Service from '@ember/service'; | ||
import { tracked } from '@glimmer/tracking'; | ||
import { task } from 'ember-concurrency'; | ||
import { get, set } from '@ember/object'; | ||
import { A as emberArray } from '@ember/array'; | ||
import algoliasearch from 'algoliasearch'; | ||
import { getOwner } from '@ember/application'; | ||
|
||
export default Service.extend({ | ||
export default class SearchServices extends Service { | ||
@tracked results = []; | ||
client = null; | ||
index = null; | ||
|
||
results: emberArray(), | ||
constructor() { | ||
super(...arguments); | ||
|
||
init() { | ||
this._super(...arguments); | ||
const config = getOwner(this).resolveRegistration('config:environment'); | ||
|
||
const { algoliaId, algoliaKey, indexName } = config['algolia'] || {}; | ||
|
||
if (algoliaId && algoliaKey && indexName) { | ||
this.client = algoliasearch(algoliaId, algoliaKey); | ||
this.index = this.client.initIndex(indexName); | ||
} | ||
}, | ||
} | ||
|
||
search: task(function * (query, projectVersion) { | ||
search = task({ restartable: true }, async (query, projectVersion) => { | ||
const searchObj = { | ||
hitsPerPage: 15, | ||
restrictSearchableAttributes: ['content'], | ||
}; | ||
|
||
if(projectVersion && projectVersion.match(/\d+\.\d+\.\d+/)) { | ||
if (projectVersion && projectVersion.match(/\d+\.\d+\.\d+/)) { | ||
searchObj.facetFilters = [[`version:${projectVersion}`]]; | ||
} | ||
|
||
return set(this, 'results', yield this.doSearch(query, searchObj)); | ||
|
||
}).restartable(), | ||
return (this.results = await this.doSearch(query, searchObj)); | ||
}); | ||
|
||
doSearch(query, searchObj) { | ||
return this.index.search(query, searchObj).then((results) => { | ||
return get(results, 'hits'); | ||
return results.hits; | ||
}); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"compilerOptions":{"target":"es6","experimentalDecorators":true},"exclude":["node_modules","bower_components","tmp","vendor",".git","dist"]} |
76 changes: 76 additions & 0 deletions
76
test-app/tests/integration/components/search-input-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
import { render, fillIn, blur, waitFor } from '@ember/test-helpers'; | ||
import { hbs } from 'ember-cli-htmlbars'; | ||
import Service from '@ember/service'; | ||
import mirageConfig from 'test-app/mirage/config'; | ||
import { setupMirage } from 'ember-mirage/test-support'; | ||
|
||
// Stub page service | ||
class PageStub extends Service { | ||
pages = [ | ||
{ | ||
id: 'examples', | ||
title: 'Examples', | ||
}, | ||
{ | ||
id: 'index', | ||
title: 'Home', | ||
}, | ||
]; | ||
} | ||
|
||
module('Integration | Component | search-input', function (hooks) { | ||
setupRenderingTest(hooks); | ||
setupMirage(hooks, { makeServer: mirageConfig }); | ||
|
||
hooks.beforeEach(async function () { | ||
this.owner.register('service:page', PageStub); | ||
}); | ||
|
||
test('it renders the inital search input without dropdown', async function (assert) { | ||
await render(hbs`<SearchInput @projectVersion="v1.0.0" />`); | ||
|
||
assert.dom('[data-test-search-input]').exists(); | ||
assert.dom('[data-test-search-dropdown]').doesNotExist(); | ||
}); | ||
|
||
test('it renders the search dropdown while searching', async function (assert) { | ||
await render(hbs`<SearchInput @projectVersion="v1.0.0" />`); | ||
|
||
await fillIn('[data-test-search-input]', 'query string'); | ||
|
||
assert.dom('[data-test-search-input]').hasValue('query string'); | ||
assert.dom('[data-test-search-dropdown]').exists(); | ||
}); | ||
|
||
test('it renders the search input with a closed dropdown after blur', async function (assert) { | ||
await render(hbs`<SearchInput @projectVersion="v1.0.0" />`); | ||
|
||
await fillIn('[data-test-search-input]', 'query string'); | ||
await waitFor('[data-test-search-result-item]'); | ||
await blur('[data-test-search-input]'); | ||
|
||
assert.dom('[data-test-search-input]').hasValue('query string'); | ||
assert.dom('[data-test-search-dropdown]').doesNotExist(); | ||
}); | ||
|
||
test('it renders the results', async function (assert) { | ||
await render(hbs`<SearchInput @projectVersion="v1.0.0" />`); | ||
await fillIn('[data-test-search-input]', 'query string'); | ||
|
||
await waitFor('[data-test-search-result-item]'); | ||
|
||
assert.dom('[data-test-search-result-item]').exists({ count: 4 }); | ||
}); | ||
|
||
test('it renders the no results message', async function (assert) { | ||
this.searchService = this.owner.lookup('service:search'); | ||
|
||
await render(hbs`<SearchInput @projectVersion="v1.0.0" />`); | ||
await fillIn('[data-test-search-input]', 'query string'); | ||
this.searchService.results = []; | ||
await waitFor('[data-test-search-noresults]'); | ||
assert.dom('[data-test-search-noresults]').hasText('No results found.'); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is using 'perform' the standard in this project? To explicitly indicate the action?
In the line above it's commenly written.