Skip to content

Commit

Permalink
feat(search): add clear button
Browse files Browse the repository at this point in the history
  • Loading branch information
Yelinz committed Dec 9, 2024
1 parent d68eabd commit 276887e
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 7 deletions.
10 changes: 9 additions & 1 deletion addon/components/search.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class="uk-background-default uk-search uk-search-default uk-width-1"
{{on "submit" this.onSubmit}}
>
<span uk-search-icon />
<span uk-search-icon></span>
<input
type="search"
class="uk-search-input uk-background-default"
Expand All @@ -13,5 +13,13 @@
data-test-search-input
{{on "input" this.updateSearch.perform}}
/>
{{!<button class="uk-form-icon uk-form-icon-flip"><UkIcon @icon="close" /></button>}}
<a
class="uk-form-icon uk-form-icon-flip"
href="#"
uk-tooltip={{t "alexandria.search-clear"}}
{{on "click" this.resetSearch}}
data-test-search-clear
><UkIcon @icon="close" /></a>
</form>
</div>
10 changes: 9 additions & 1 deletion addon/components/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@ export default class SearchComponent extends Component {
}

this.router.transitionTo(this.router.currentRouteName, {
queryParams: { search: search || undefined, category: undefined },
queryParams: { search: search || undefined },
});
},
);

@action
resetSearch(event) {
event.preventDefault();
this.router.transitionTo(this.router.currentRouteName, {
queryParams: { search: undefined },
});
}
}
2 changes: 1 addition & 1 deletion addon/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class IndexController extends Controller {

get documentFilters() {
let filters = {
category: this.category,
categories: this.category,
tags: this.tags.length ? this.tags.join(",") : undefined,
marks: this.marks.length ? this.marks.join(",") : undefined,
query: this.search,
Expand Down
31 changes: 28 additions & 3 deletions tests/integration/components/search-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, fillIn } from "@ember/test-helpers";
import { render, fillIn, click } from "@ember/test-helpers";
import { setupRenderingTest } from "dummy/tests/helpers";
import { hbs } from "ember-cli-htmlbars";
import { module, test } from "qunit";
Expand All @@ -7,7 +7,7 @@ import { fake, stub } from "sinon";
module("Integration | Component | search", function (hooks) {
setupRenderingTest(hooks);

test("it renders", async function (assert) {
test("it searches", async function (assert) {
const router = this.engine.lookup("service:router");

stub(router, "currentRouteName").get(() => null);
Expand All @@ -25,7 +25,32 @@ module("Integration | Component | search", function (hooks) {
[
null,
{
queryParams: { search: "new search", category: undefined },
queryParams: { search: "new search" },
},
],
"transitionTo was called with the correct arguments",
);
});

test("it clears search", async function (assert) {
const router = this.engine.lookup("service:router");

stub(router, "currentRouteName").get(() => null);
router.transitionTo = fake();

await render(hbs`<Search @search="test"/>`, { owner: this.engine });

assert.dom("[data-test-search-input]").hasValue("test");

await click("[data-test-search-clear]");

assert.ok(router.transitionTo.called, "transitionTo was called");
assert.deepEqual(
router.transitionTo.firstCall.args,
[
null,
{
queryParams: { search: undefined },
},
],
"transitionTo was called with the correct arguments",
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/controllers/application-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module("Unit | Controller | index", function (hooks) {

assert.deepEqual(controller.documentFilters, {
activeGroup: "group",
category: 1,
categories: 1,
metainfo: JSON.stringify([{ key: "instance_id", value: "1" }]),
query: "test",
tags: undefined,
Expand Down
1 change: 1 addition & 0 deletions translations/de.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
alexandria:
loading: "Lädt..."
search: "Suchen..."
search-clear: "Suche löschen"
nothing-found: "Wir haben nichts gefunden..."
delete: "Löschen"
upload-file: "Datei hochladen"
Expand Down
1 change: 1 addition & 0 deletions translations/en.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
alexandria:
loading: "Loading..."
search: "Search..."
search-clear: "Clear search"
nothing-found: "We found nothing..."
delete: "Delete"
upload-file: "Upload file"
Expand Down
1 change: 1 addition & 0 deletions translations/it.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
alexandria:
loading: "Sto caricando i risultati..."
search: "Cerca..."
search-clear: "Ricerca chiara"
nothing-found: "Nessun risultato trovato..."
delete: "Elimina"
upload-file: "Carica file"
Expand Down

0 comments on commit 276887e

Please sign in to comment.