Skip to content

Commit

Permalink
Autofocus the clicked field upon the modal opening
Browse files Browse the repository at this point in the history
In order to forward context to the modal, it will autofocus the clicked
on field that triggered it to open.
  • Loading branch information
aaron-contreras committed Mar 27, 2024
1 parent c339404 commit fc310bc
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/components/_index.sass
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@import "work_packages/share/modal_body_component"
@import "work_packages/share/invite_user_form_component"
@import "work_packages/progress/progress_modal"
@import "work_packages/progress/modal_body_component"
@import "open_project/common/attribute_component"
@import "filters_component"
@import "projects/settings/project_custom_field_sections/index_component"
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<%= turbo_frame_tag "work_package_progress_modal" do %>
<%= primer_form_with(
model: work_package,
url: work_package_progress_path(work_package),
id: 'blah') do |f| %>
model: work_package,
url: work_package_progress_path(work_package),
class: 'progress-form',
id: 'progress-form') do |f| %>
<%= flex_layout do |modal_body| %>
<% modal_body.with_row do |_fields| %>
<%= render(WorkPackages::ProgressForm.new(f)) %>
<%= render(WorkPackages::ProgressForm.new(f, focused_field: focused_field)) %>
<% end %>

<% modal_body.with_row(mt: 3) do |_tooltip| %>
Expand Down
28 changes: 26 additions & 2 deletions app/components/work_packages/progress/modal_body_component.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# -- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2010-2024 the OpenProject GmbH
Expand Down Expand Up @@ -33,12 +35,34 @@ class ModalBodyComponent < ApplicationComponent
include Turbo::FramesHelper
include OpPrimer::ComponentHelpers

attr_reader :work_package
attr_reader :work_package, :focused_field

FIELD_MAP = {
"estimatedTime" => :estimated_hours,
"remainingTime" => :remaining_hours
}.freeze

def initialize(work_package)
def initialize(work_package, focused_field:)
super()

@work_package = work_package
@focused_field = map_field(focused_field)
end

private

def map_field(field)
# Scenarios when a field is not provided occur after a
# form submission since the last focused element
# was the submit button. In this case, don't focus on
# an element by default.
return nil if field.nil?

field = FIELD_MAP[field.to_s]

return field if field.present?

raise ArgumentError, "The selected field is not one of #{FIELD_MAP.keys.join(', ')}."
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.progress-form
.input--readonly
background-color: unset !important
border: none !important
box-shadow: none !important
4 changes: 0 additions & 4 deletions app/components/work_packages/progress/progress_modal.sass

This file was deleted.

2 changes: 1 addition & 1 deletion app/controllers/work_packages/progress_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class WorkPackages::ProgressController < ApplicationController
before_action :set_work_package

def edit
render WorkPackages::Progress::ModalBodyComponent.new(@work_package)
render WorkPackages::Progress::ModalBodyComponent.new(@work_package, focused_field: params[:field])
end

def update
Expand Down
14 changes: 10 additions & 4 deletions app/forms/work_packages/progress_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,31 @@
# ++

class WorkPackages::ProgressForm < ApplicationForm
include OpenProject::StaticRouting::UrlHelpers
def initialize(focused_field:)
super()

@focused_field = focused_field
end

form do |query_form|
query_form.group(layout: :horizontal) do |group|
group.text_field(
name: :estimated_hours,
label: "Work"
label: "Work",
autofocus: @focused_field == :estimated_hours
)

group.text_field(
name: :remaining_hours,
label: "Remaining work"
label: "Remaining work",
autofocus: @focused_field == :remaining_hours
)

group.text_field(
name: :done_ratio,
label: "% Complete",
readonly: true,
classes: "boob"
classes: "input--readonly"
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/work_packages/progress/update.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<%= turbo_stream.update "work_package_progress_modal" do %>
<%= render WorkPackages::Progress::ModalBodyComponent.new(@work_package) %>
<%= render WorkPackages::Progress::ModalBodyComponent.new(@work_package, focused_field: params[:field]) %>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,12 @@ export class ProgressPopoverEditFieldComponent extends ProgressEditFieldComponen

ngOnInit() {
super.ngOnInit();
/*
Append clicked field name to URL query props
in order to indicate which field should be focused on load.
*/
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
this.frameSrc = this.pathHelper.workPackageProgressModalPath(this.resource.id as string);
this.frameSrc = `${this.pathHelper.workPackageProgressModalPath(this.resource.id as string)}?field=${this.name}`;
this.frameId = 'work_package_progress_modal';
}

Expand Down

0 comments on commit fc310bc

Please sign in to comment.