OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Fix clear button not triggering a page filter reload.
Dombi Attila5b857bbfba8f
diff --git a/app/components/projects/index_sub_header_component.html.erb b/app/components/projects/index_sub_header_component.html.erbindex 8c1f9238554..7f3480e8867 100644--- a/app/components/projects/index_sub_header_component.html.erb+++ b/app/components/projects/index_sub_header_component.html.erb@@ -1,9 +1,5 @@<%= component_wrapper(tag: 'turbo-frame') do %>- <%= render(Primer::OpenProject::SubHeader.new(data: {- controller: "filter--filters-form",- 'filter--filters-form-perform-turbo-requests-value': true,- "application-target": "dynamic",- })) do |subheader|+ <%= render(Primer::OpenProject::SubHeader.new(data: sub_header_data_attributes)) do |subheader|subheader.with_filter_input(name: "name_and_identifier",label: t('projects.index.search.label'),value: filter_input_value,@@ -13,6 +9,7 @@size: :small},show_clear_button: true,+ clear_button_id: clear_button_id,data: filter_input_data_attributes)subheader.with_filter_component dodiff --git a/app/components/projects/index_sub_header_component.rb b/app/components/projects/index_sub_header_component.rbindex 2c4c668c900..1daa917a087 100644--- a/app/components/projects/index_sub_header_component.rb+++ b/app/components/projects/index_sub_header_component.rb@@ -50,14 +50,26 @@ module Projects@query.find_active_filter(:name_and_identifier)&.values&.firstend+ def sub_header_data_attributes+ {+ controller: "filter--filters-form",+ "application-target": "dynamic",+ "filter--filters-form-perform-turbo-requests-value": true,+ "filter--filters-form-clear-button-id-value": clear_button_id+ }+ end+def filter_input_data_attributes{"filter-name": "name_and_identifier","filter-type": "string","filter-operator": "~",- "filter--filters-form-target": "simpleFilter filterValueContainer simpleValue",- action: "input->filter--filters-form#sendForm:prevent"+ "filter--filters-form-target": "simpleFilter filterValueContainer simpleValue"}end++ def clear_button_id+ "project-filters-form-clear-button"+ endendenddiff --git a/frontend/src/stimulus/controllers/dynamic/filter/filters-form.controller.ts b/frontend/src/stimulus/controllers/dynamic/filter/filters-form.controller.tsindex 40804fc7aff..77305dbef2d 100644--- a/frontend/src/stimulus/controllers/dynamic/filter/filters-form.controller.ts+++ b/frontend/src/stimulus/controllers/dynamic/filter/filters-form.controller.ts@@ -33,6 +33,10 @@ import { Controller } from '@hotwired/stimulus';import { renderStreamMessage } from '@hotwired/turbo';import { debounce } from 'lodash';+interface PrimerTextFieldElement extends HTMLElement {+ inputElement:HTMLInputElement;+}+interface InternalFilterValue {name:string;operator:string;@@ -74,11 +78,13 @@ export default class FiltersFormController extends Controller {displayFilters: { type: Boolean, default: false },outputFormat: { type: String, default: 'params' },performTurboRequests: { type: Boolean, default: false },+ clearButtonId: String,};declare displayFiltersValue:boolean;declare outputFormatValue:string;declare performTurboRequestsValue:boolean;+ declare readonly clearButtonIdValue:string;initialize() {// Initialize runs anytime an element with a controller connected to the DOM for the first time@@ -89,11 +95,14 @@ export default class FiltersFormController extends Controller {const urlParams = new URLSearchParams(window.location.search);this.displayFiltersValue = urlParams.has('filters');+ const clearButton = document.getElementById(this.clearButtonIdValue);+ clearButton?.addEventListener('click', (event:MouseEvent) => this.clearInputWithButton(event));+// Auto-register change event listeners for all fields// to keep DOM cleaner.if (this.performTurboRequestsValue) {this.simpleValueTargets.forEach((simpleValue) => {- simpleValue.addEventListener('change', this.sendForm.bind(this));+ simpleValue.addEventListener('input', this.sendForm.bind(this));});this.operatorTargets.forEach((operator) => {@@ -119,6 +128,9 @@ export default class FiltersFormController extends Controller {}disconnect() {+ const clearButton = document.getElementById(this.clearButtonIdValue);+ clearButton?.removeEventListener('click', (event:MouseEvent) => this.clearInputWithButton(event));+// Auto-deregister change event listeners for all fields// to keep DOM cleaner.if (this.performTurboRequestsValue) {@@ -232,6 +244,21 @@ export default class FiltersFormController extends Controller {}}+ clearInputWithButton(event:MouseEvent) {+ // Primer does not trigger an input event when clearing the value of the input field unless+ // it is focused. This handler will find the sibling input of the clear button inside the+ // PrimerTextField and triggers the input in order to notify the auto-reloading filter mechanism.+ const element = event.currentTarget as HTMLElement;+ const primerTextField = element.closest('primer-text-field') as PrimerTextFieldElement;+ const inputElement = primerTextField.inputElement;++ const inputEvent = new Event('input', {+ bubbles: true,+ cancelable: true,+ });+ inputElement.dispatchEvent(inputEvent);+ }+private setSpacerVisibility() {if (this.anyFiltersStillVisible()) {this.spacerTarget.classList.remove('hidden');@@ -328,7 +355,6 @@ export default class FiltersFormController extends Controller {private parseSimpleFilters():InternalFilterValue[] {const simpleFilters = this.simpleFilterTargets;const filters:InternalFilterValue[] = [];- // debugger;simpleFilters.forEach((filter) => {const name = filter.getAttribute('data-filter-name');