A platform for community discussion. Free, open, simple.
DEV: Add optional clear button to FilterInput component (#35569)
Joffrey JAFFEUXfc437052bb4f
diff --git a/frontend/discourse/app/components/filter-input.gjs b/frontend/discourse/app/components/filter-input.gjsindex f0857cfdb7f..cc7d228fa4d 100644--- a/frontend/discourse/app/components/filter-input.gjs+++ b/frontend/discourse/app/components/filter-input.gjs@@ -1,7 +1,10 @@import Component from "@glimmer/component";import { tracked } from "@glimmer/tracking";import { on } from "@ember/modifier";+import { action } from "@ember/object";import { modifier } from "ember-modifier";+import { and } from "truth-helpers";+import DButton from "discourse/components/d-button";import concatClass from "discourse/helpers/concat-class";import icon from "discourse/helpers/d-icon";import noop from "discourse/helpers/noop";@@ -9,6 +12,10 @@ import noop from "discourse/helpers/noop";export default class FilterInput extends Component {@tracked isFocused = false;+ registerInput = modifier((element) => {+ this.input = element;+ });+focusState = modifier((element) => {const focusInHandler = () => {this.isFocused = true;@@ -26,6 +33,12 @@ export default class FilterInput extends Component {};});+ @action+ onClearInput(event) {+ this.args.onClearInput(event);+ this.input?.focus();+ }+<template><divclass={{concatClass@@ -39,6 +52,7 @@ export default class FilterInput extends Component {{{/if}}<input+ {{this.registerInput}}{{this.focusState}}{{on "input" (if @filterAction @filterAction (noop))}}type="text"@@ -49,6 +63,14 @@ export default class FilterInput extends Component {{{yield}}+ {{#if (and @onClearInput @value.length)}}+ <DButton+ @icon="xmark"+ @action={{this.onClearInput}}+ class="btn-small btn-transparent filter-input-clear-btn"+ />+ {{/if}}+{{#if @icons.right}}{{icon @icons.right class="-right"}}{{/if}}diff --git a/frontend/discourse/tests/integration/components/filter-input-test.gjs b/frontend/discourse/tests/integration/components/filter-input-test.gjsindex eccfbb27ca2..d0b306c1140 100644--- a/frontend/discourse/tests/integration/components/filter-input-test.gjs+++ b/frontend/discourse/tests/integration/components/filter-input-test.gjs@@ -1,5 +1,5 @@import { hash } from "@ember/helper";-import { fillIn, render, triggerEvent } from "@ember/test-helpers";+import { click, fillIn, render, triggerEvent } from "@ember/test-helpers";import { module, test } from "qunit";import FilterInput from "discourse/components/filter-input";import { setupRenderingTest } from "discourse/tests/helpers/component-test";@@ -67,4 +67,53 @@ module("Integration | Component | FilterInput", function (hooks) {assert.dom(".filter-input-container.is-focused").doesNotExist();});++ test("Clear button visibility", async function (assert) {+ const self = this;++ this.set("clearAction", () => {});+ this.set("value", "test");+ await render(+ <template>+ <FilterInput @onClearInput={{self.clearAction}} @value={{self.value}} />+ </template>+ );++ assert.dom(".filter-input-clear-btn").exists();++ this.set("value", "");++ assert.dom(".filter-input-clear-btn").doesNotExist();+ });++ test("onClearInput callback", async function (assert) {+ const self = this;++ this.set("called", false);+ this.set("clearAction", () => {+ this.set("called", true);+ });+ await render(+ <template>+ <FilterInput @onClearInput={{self.clearAction}} @value="test" />+ </template>+ );+ await click(".filter-input-clear-btn");++ assert.true(this.called);+ });++ test("Input focus after clear", async function (assert) {+ const self = this;++ this.set("clearAction", () => {});+ await render(+ <template>+ <FilterInput @onClearInput={{self.clearAction}} @value="test" />+ </template>+ );+ await click(".filter-input-clear-btn");++ assert.dom(".filter-input").isFocused();+ });});diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-channel-filter.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-channel-filter.gjsindex d40947fe7dc..8dca08f69e2 100644--- a/plugins/chat/assets/javascripts/discourse/components/chat-channel-filter.gjs+++ b/plugins/chat/assets/javascripts/discourse/components/chat-channel-filter.gjs@@ -30,6 +30,11 @@ export default class ChatChannelFilter extends Component {return this.currentChannelFilterResultIndex + 1;}+ @action+ clearInput() {+ this.currentChannelFilter = "";+ }+@actionnavigateToPreviousResult() {if (!this.channelFilterResults?.length) {@@ -150,6 +155,7 @@ export default class ChatChannelFilter extends Component {placeholder={{i18n "chat.search.title"}}@filterAction={{this.loadSearchResults}}class="no-blur"+ @onClearInput={{this.clearInput}}/>{{#if this.channelFilterResults.length}}@@ -177,14 +183,6 @@ export default class ChatChannelFilter extends Component {{{/if}}{{/if}}- {{#if this.currentChannelFilter.length}}- <DButton- @icon="xmark"- class="btn-small btn-flat"- @action={{this.clearFilteringState}}- />- {{/if}}-<DButton@action={{fn @onToggleFilter false}}class="btn-small btn-flat"diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-search.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-search.gjsindex 2efc154fac0..3cd6c21d021 100644--- a/plugins/chat/assets/javascripts/discourse/components/chat-search.gjs+++ b/plugins/chat/assets/javascripts/discourse/components/chat-search.gjs@@ -189,12 +189,9 @@ export default class ChatSearch extends Component {@icons={{hash left="magnifying-glass"}}placeholder={{i18n "chat.search_view.filter_placeholder"}}class="no-blur"+ @onClearInput={{fn this.debounceFilterChange ""}}/>- {{#if @query}}- <DButton @icon="xmark" @action={{fn this.debounceFilterChange ""}} />- {{/if}}-<DMenu@identifier="search-sort-options"@label={{this.sortLabel this.currentSort}}diff --git a/plugins/chat/spec/system/chat_channel_search/drawer_spec.rb b/plugins/chat/spec/system/chat_channel_search/drawer_spec.rbindex 2c0cb6e36e2..9770f6ce953 100644--- a/plugins/chat/spec/system/chat_channel_search/drawer_spec.rb+++ b/plugins/chat/spec/system/chat_channel_search/drawer_spec.rb@@ -49,6 +49,21 @@ RSpec.describe "Chat channel search - drawer", type: :system doexpect(channel_page.filter).to have_no_resultsend++ it "can clear the filter input" do+ message =+ Fabricate(:chat_message, chat_channel: channel_1, message: "test", use_service: true)++ drawer_page.visit_channel(channel_1)+ channel_page.filter.toggle.fill_in("test")++ expect(channel_page.filter).to have_state(results: 1, position: 1)++ channel_page.filter.clear++ expect(channel_page.filter).to be_visible+ expect(channel_page.filter).to have_no_state+ endendcontext "when filter is not toggled" dodiff --git a/plugins/chat/spec/system/chat_channel_search/full_page_spec.rb b/plugins/chat/spec/system/chat_channel_search/full_page_spec.rbindex 98716760a05..451abc51874 100644--- a/plugins/chat/spec/system/chat_channel_search/full_page_spec.rb+++ b/plugins/chat/spec/system/chat_channel_search/full_page_spec.rb@@ -48,6 +48,21 @@ RSpec.describe "Chat channel search - full page", type: :system doexpect(channel_page.filter).to have_no_resultsend++ it "can clear the filter input" do+ message =+ Fabricate(:chat_message, chat_channel: channel_1, message: "test", use_service: true)++ chat_page.visit_channel(channel_1)+ channel_page.filter.toggle.fill_in("test")++ expect(channel_page.filter).to have_state(results: 1, position: 1)++ channel_page.filter.clear++ expect(channel_page.filter).to be_visible+ expect(channel_page.filter).to have_no_state+ endendcontext "when filter is not toggled" dodiff --git a/plugins/chat/spec/system/chat_search_spec.rb b/plugins/chat/spec/system/chat_search_spec.rbindex 55c6782481a..2f95270db7e 100644--- a/plugins/chat/spec/system/chat_search_spec.rb+++ b/plugins/chat/spec/system/chat_search_spec.rb@@ -81,6 +81,18 @@ RSpec.describe "Chat search", type: :system doexpect(chat_search_page).to have_x_results(21)end++ it "can clear the search input" do+ Fabricate(:chat_message, chat_channel: channel_1, message: "test message", use_service: true)++ chat_search_page.visit.fill_in("test")++ expect(chat_search_page).to have_x_results(1)++ chat_search_page.clear++ expect(chat_search_page).to have_zero_results+ endendcontext "with search disabled" dodiff --git a/plugins/chat/spec/system/page_objects/chat/components/filter.rb b/plugins/chat/spec/system/page_objects/chat/components/filter.rbindex 34a45109a36..45500bcff70 100644--- a/plugins/chat/spec/system/page_objects/chat/components/filter.rb+++ b/plugins/chat/spec/system/page_objects/chat/components/filter.rb@@ -17,7 +17,7 @@ module PageObjectsenddef toggle- locator(".c-navbar__filter").click+ locator(".c-navbar__filter").click(position: { x: 0, y: 25 }) # avoid mini profilerselfend@@ -34,6 +34,15 @@ module PageObjectsselfend+ def clear+ filter_bar.locator(".filter-input-clear-btn").click+ self+ end++ def has_no_state?+ has_no_css?(".chat-channel__filter-position")+ end+def has_no_results?has_css?(".alert.alert-info", text: I18n.t("js.chat.search.no_results"))enddiff --git a/plugins/chat/spec/system/page_objects/pages/chat_search.rb b/plugins/chat/spec/system/page_objects/pages/chat_search.rbindex 60a9553368b..401ea9777f1 100644--- a/plugins/chat/spec/system/page_objects/pages/chat_search.rb+++ b/plugins/chat/spec/system/page_objects/pages/chat_search.rb@@ -37,6 +37,15 @@ module PageObjectsexpect(page_locator.locator(".alert.alert-info")).to be_visibleend+ def clear+ page_locator.locator(".filter-input-clear-btn").click+ self+ end++ def has_zero_results?+ has_no_selector?(".chat-message-container")+ end+def click_result(message)page_locator.locator(".chat-message-search-entry:has(.chat-message-container[data-id=\"#{message.id}\"])",