Skip to content

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

Handle focus in one unique controller

Christophe Bliard7595b8d02ab4
diff --git a/app/components/work_packages/progress/status_based/modal_body_component.html.erb b/app/components/work_packages/progress/status_based/modal_body_component.html.erb
index d2379649265..13fd66f0006 100644
--- a/app/components/work_packages/progress/status_based/modal_body_component.html.erb
+++ b/app/components/work_packages/progress/status_based/modal_body_component.html.erb
@@ -7,8 +7,7 @@
html: { autocomplete: "off" },
data: { "application-target": "dynamic",
"work-packages--progress--preview-target": "form",
- controller: "work-packages--progress--focus-field " \
- "work-packages--progress--preview" }
+ controller: "work-packages--progress--preview" }
) do |f| %>
<%= flex_layout do |modal_body| %>
<% modal_body.with_row(classes: "FormControl-horizontalGroup--sm-vertical") do |_fields| %>
diff --git a/app/components/work_packages/progress/work_based/modal_body_component.html.erb b/app/components/work_packages/progress/work_based/modal_body_component.html.erb
index 2726ce5db9e..80c60dfaa45 100644
--- a/app/components/work_packages/progress/work_based/modal_body_component.html.erb
+++ b/app/components/work_packages/progress/work_based/modal_body_component.html.erb
@@ -7,8 +7,7 @@
html: { autocomplete: "off" },
data: { "application-target": "dynamic",
"work-packages--progress--preview-target": "form",
- controller: "work-packages--progress--focus-field " \
- "work-packages--progress--preview" }
+ controller: "work-packages--progress--preview" }
) do |f| %>
<%= flex_layout do |modal_body| %>
<% modal_body.with_row(classes: "FormControl-horizontalGroup--sm-vertical") do |_fields| %>
diff --git a/app/forms/work_packages/pre_14_4_progress_form.rb b/app/forms/work_packages/pre_14_4_progress_form.rb
index f6b530f93dd..0d9ac1a2ad9 100644
--- a/app/forms/work_packages/pre_14_4_progress_form.rb
+++ b/app/forms/work_packages/pre_14_4_progress_form.rb
@@ -199,7 +199,7 @@ class WorkPackages::Pre144ProgressForm < ApplicationForm
action: "input->work-packages--progress--preview#markFieldAsTouched" }
if @focused_field == name
- data[:"work-packages--progress--focus-field-target"] = "fieldToFocus"
+ data[:focus] = "true"
end
{ data: }
end
diff --git a/app/forms/work_packages/progress_form.rb b/app/forms/work_packages/progress_form.rb
index 87542bb29ca..5c9e8267944 100644
--- a/app/forms/work_packages/progress_form.rb
+++ b/app/forms/work_packages/progress_form.rb
@@ -201,7 +201,7 @@ class WorkPackages::ProgressForm < ApplicationForm
action: "work-packages--progress--preview#markFieldAsTouched" }
if @focused_field == name
- data[:"work-packages--progress--focus-field-target"] = "fieldToFocus"
+ data[:focus] = "true"
end
{ data: }
end
diff --git a/frontend/src/stimulus/controllers/dynamic/work-packages/progress/focus-field.controller.ts b/frontend/src/stimulus/controllers/dynamic/work-packages/progress/focus-field.controller.ts
deleted file mode 100644
index ee7ecb62913..00000000000
--- a/frontend/src/stimulus/controllers/dynamic/work-packages/progress/focus-field.controller.ts
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * -- copyright
- * OpenProject is an open source project management software.
- * Copyright (C) the OpenProject GmbH
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License version 3.
- *
- * OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
- * Copyright (C) 2006-2013 Jean-Philippe Lang
- * Copyright (C) 2010-2013 the ChiliProject Team
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- * See COPYRIGHT and LICENSE files for more details.
- * ++
- */
-
-import { Controller } from '@hotwired/stimulus';
-
-export default class FocusFieldController extends Controller {
- static targets = [
- 'fieldToFocus',
- ];
-
- declare readonly fieldToFocusTarget:HTMLInputElement;
-
- connect() {
- if (this.fieldToFocusTarget) {
- this.fieldToFocusTarget.focus();
- this.setCursorPositionToEndOfInput();
- }
- }
-
- private setCursorPositionToEndOfInput() {
- this.fieldToFocusTarget.setSelectionRange(
- this.fieldToFocusTarget.value.length,
- this.fieldToFocusTarget.value.length,
- );
- }
-}
diff --git a/frontend/src/stimulus/controllers/dynamic/work-packages/progress/preview.controller.ts b/frontend/src/stimulus/controllers/dynamic/work-packages/progress/preview.controller.ts
index 261bb72a3a7..10262134efe 100644
--- a/frontend/src/stimulus/controllers/dynamic/work-packages/progress/preview.controller.ts
+++ b/frontend/src/stimulus/controllers/dynamic/work-packages/progress/preview.controller.ts
@@ -82,6 +82,10 @@ export default class PreviewController extends Controller {
target.addEventListener('input', this.debouncedPreview);
}
target.addEventListener('blur', this.debouncedPreview);
+
+ if (target.dataset.focus === 'true') {
+ this.focusAndSetCursorPositionToEndOfInput(target);
+ }
});
const turboFrame = this.formTarget.closest('turbo-frame') as HTMLFrameElement;
@@ -141,6 +145,14 @@ export default class PreviewController extends Controller {
}
}
+ private focusAndSetCursorPositionToEndOfInput(field:HTMLInputElement) {
+ field.focus();
+ field.setSelectionRange(
+ field.value.length,
+ field.value.length,
+ );
+ }
+
// Ensures that on create forms, there is an "id" for the un-persisted
// work package when sending requests to the edit action for previews.
private ensureValidPathname(formAction:string):string {