Skip to content

Commit

Permalink
Update dropdown menu markup for Bootstrap 5
Browse files Browse the repository at this point in the history
  • Loading branch information
spohlenz committed Jul 22, 2024
1 parent ff8646b commit 932152f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/helpers/trestle/url_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def admin_link_to(content, instance_or_url=nil, options={}, &block)
# Determine link data options
options[:data] ||= {}

if MODAL_ACTIONS.include?(action) && admin.form.modal?
if MODAL_ACTIONS.include?(action) && admin.respond_to?(:form) && admin.form.modal?
options[:data][:controller] ||= "modal-trigger"
else
options[:data][:turbo_frame] = "_top"
Expand Down
13 changes: 8 additions & 5 deletions lib/trestle/toolbar/menu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,33 @@ def render_items
end

class Builder
delegate :admin_link_to, :content_tag, to: :@template
delegate :admin_link_to, :content_tag, :tag, to: :@template

def initialize(menu, template)
@menu, @template = menu, template
end

def link(content, instance_or_url=nil, options={}, &block)
if instance_or_url.is_a?(Hash)
instance_or_url, options = nil, instance_or_url
end

options[:class] = Array(options[:class])
options[:class] << "dropdown-item"

item { admin_link_to(content, instance_or_url, options, &block) }
end

def header(text)
item(class: "dropdown-header") { text }
item { content_tag(:h6, text, class: "dropdown-header") }
end

def divider
item(class: "divider")
item { tag(:hr, class: "dropdown-divider") }
end

def item(options={}, &block)
opts = { role: "presentation" }.merge(options)
item = block_given? ? content_tag(:li, opts, &block) : content_tag(:li, "", opts)
item = block_given? ? content_tag(:li, options, &block) : content_tag(:li, "", options)

@menu.items << item

Expand Down
6 changes: 4 additions & 2 deletions sandbox/app/admin/articles_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
end

hook "index.toolbar.secondary" do |t|
t.link "Batch Action (GET)", action: :batch_get, style: :info, data: { controller: "batch-action" }
t.link "Batch Action (POST)", action: :batch_post, style: :warning, data: { controller: "confirm batch-action", turbo_method: :post }
t.dropdown "Batch Actions", style: :info do |l|
l.link "Batch Action (GET)", action: :batch_get, data: { controller: "batch-action" }
l.link "Batch Action (POST)", action: :batch_post, data: { controller: "confirm batch-action", turbo_method: :post }
end
end

hook "new.toolbar.secondary" do |t|
Expand Down
21 changes: 18 additions & 3 deletions spec/trestle/toolbar/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,24 @@
shared_examples "a toolbar item with a dropdown" do |tag, attrs|
include_context "template"

let(:admin) { double }

let(:options) { {} }

let(:block) do
->(d) {
d.header "Header"
d.link "Link", "#"
d.link "Disabled Link", class: "disabled", admin: :test
d.divider
}
end

before(:each) {
allow(Trestle).to receive(:lookup).with(:test).and_return(admin)
allow(admin).to receive(:path).and_return("/admin/test")
}

it "renders the button within a button group" do
expect(subject.to_s).to have_tag(".btn-group", with: { role: "group" }) do
with_tag "#{tag}.btn.btn-default"
Expand All @@ -62,11 +70,18 @@

it "renders the block items within a dropdown menu" do
expect(subject.to_s).to have_tag(".btn-group", with: { role: "group" }) do
with_tag "li.dropdown-header", text: "Header", with: { role: "presentation" }
with_tag "li", with: { role: "presentation" } do
with_tag "li" do
with_tag "h6", text: "Header", with: { class: "dropdown-header" }
end
with_tag "li" do
with_tag "a", text: "Link", with: { href: "#", class: "dropdown-item" }
end
with_tag "li.divider", with: { role: "presentation" }
with_tag "li" do
with_tag "a", text: "Disabled Link", with: { href: "/admin/test", class: "disabled dropdown-item" }
end
with_tag "li" do
with_tag "hr", class: "dropdown-divider"
end
end
end
end
Expand Down

0 comments on commit 932152f

Please sign in to comment.