Skip to content

Commit

Permalink
feature: heading target
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob committed May 8, 2024
1 parent c8ed6e8 commit dde19c1
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
10 changes: 2 additions & 8 deletions app/components/avo/field_wrapper_component.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class Avo::FieldWrapperComponent < ViewComponent::Base
include Avo::Concerns::HasResourceStimulusControllers

attr_reader :dash_if_blank
attr_reader :compact
attr_reader :field
Expand Down Expand Up @@ -112,12 +114,4 @@ def short?
def full_width?
@full_width
end

private

def add_stimulus_attributes_for(entity, attributes)
entity.get_stimulus_controllers.split(" ").each do |controller|
attributes["#{controller}-target"] = "#{@field.id.to_s.underscore}_#{@field.type.to_s.underscore}_wrapper".camelize(:lower)
end
end
end
10 changes: 5 additions & 5 deletions app/components/avo/fields/common/heading_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<div class="flex items-start py-1 leading-tight bg-gray-100 text-gray-500 text-xs">
<%= content_tag :div, class: "flex items-start py-1 leading-tight bg-gray-100 text-gray-500 text-xs", data: @data do %>
<div class="py-2 px-6 h-full w-full">
<% if as_html %>
<%= sanitize value %>
<% if @field.as_html %>
<%= sanitize @field.value %>
<% else %>
<div class="font-semibold uppercase"><%= value %></div>
<div class="font-semibold uppercase"><%= @field.value %></div>
<% end %>
</div>
</div>
<% end %>
12 changes: 7 additions & 5 deletions app/components/avo/fields/common/heading_component.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# frozen_string_literal: true

class Avo::Fields::Common::HeadingComponent < ViewComponent::Base
attr_reader :value
attr_reader :as_html
include Avo::Concerns::HasResourceStimulusControllers

def initialize(value:, as_html:)
@value = value
@as_html = as_html
def initialize(field:)
@field = field
@view = field.resource.view

@data = stimulus_data_attributes
add_stimulus_attributes_for(field.resource, @data)
end
end
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div <% if @index == 0 %> class="overflow-hidden rounded-t" <% end %> data-field-id="<%= @field.id %>">
<%= render Avo::Fields::Common::HeadingComponent.new value: @field.value, as_html: @field.as_html %>
<%= render Avo::Fields::Common::HeadingComponent.new field: @field %>
</div>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div <% if @index == 0 %> class="overflow-hidden rounded-t" <% end %> data-field-id="<%= @field.id %>">
<%= render Avo::Fields::Common::HeadingComponent.new value: @field.value, as_html: @field.as_html %>
<%= render Avo::Fields::Common::HeadingComponent.new field: @field %>
</div>
12 changes: 9 additions & 3 deletions lib/avo/concerns/has_resource_stimulus_controllers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ module HasResourceStimulusControllers
end

def get_stimulus_controllers
return "" if view.nil?
return "" if @view.nil?

controllers = []

case view.to_sym
case @view.to_sym
when :show
controllers << "resource-show"
when :new, :edit
Expand All @@ -32,11 +32,17 @@ def stimulus_data_attributes
}

get_stimulus_controllers.split(" ").each do |controller|
attributes["#{controller}-view-value"] = view
attributes["#{controller}-view-value"] = @view
end

attributes
end

def add_stimulus_attributes_for(entity, attributes)
entity.get_stimulus_controllers.split(" ").each do |controller|
attributes["#{controller}-target"] = "#{@field.id.to_s.underscore}_#{@field.type.to_s.underscore}_wrapper".camelize(:lower)
end
end
end
end
end

0 comments on commit dde19c1

Please sign in to comment.