Module: Bootstrap5RailsExtensions::ModalHelper
- Defined in:
- app/helpers/bootstrap5_rails_extensions/modal_helper.rb
Defined Under Namespace
Classes: ModalBuilder
Instance Method Summary collapse
-
#render_modal(title_or_nil = nil, id: nil, title: nil, dialog: {}, data: {}, form: nil, &block) ⇒ Object
Stripeダッシュボードを意識したBootstrap 5.3モーダルのDSLを提供するヘルパー。.
Instance Method Details
#render_modal(title_or_nil = nil, id: nil, title: nil, dialog: {}, data: {}, form: nil, &block) ⇒ Object
Stripeダッシュボードを意識したBootstrap 5.3モーダルのDSLを提供するヘルパー。
使い方:
<%= render_modal id: "exampleModal", title: "タイトル" do |modal| %>
<% modal.body do %>
本文...
<% end %>
<% end %>
フォームを1枚で扱う場合:
<%= render_modal id: "userModal", title: "ユーザー追加",
form: { model: User.new, url: users_path } do |modal| %>
<% modal.body do |f| %>
<%= f.text_field :name %>
<% end %>
<% modal.footer do |f| %>
<%= f.submit "保存" %>
<% end %>
<% end %>
オプション:
dialog: { size: :sm|:lg|:xl|:fullscreen, centered: true/false, scrollable: true/false }
data: data-*
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/helpers/bootstrap5_rails_extensions/modal_helper.rb', line 30 def render_modal(title_or_nil = nil, id: nil, title: nil, dialog: {}, data: {}, form: nil, &block) raise ArgumentError, _("モーダルにはブロックが必要です") unless block_given? title ||= title_or_nil if title.nil? && title_or_nil.is_a?(String) raise ArgumentError, _("モーダルIDを指定してください") if id.nil? || id.to_s.empty? raise ArgumentError, _("モーダルタイトルを指定してください") if title.nil? || title.to_s.empty? dialog_classes = ["modal-dialog"] case dialog[:size]&.to_sym when :sm then dialog_classes << "modal-sm" when :lg then dialog_classes << "modal-lg" when :xl then dialog_classes << "modal-xl" when :fullscreen then dialog_classes << "modal-fullscreen" end dialog_classes << "modal-dialog-centered" if dialog[:centered] dialog_classes << "modal-dialog-scrollable" if dialog[:scrollable] modal_data = { controller: "modal" }.merge(data || {}) builder = ModalBuilder.new(self, modal_id: id, default_title: title) content_markup = if form.present? = form.to_h.symbolize_keys capture do form_with(**) do |form_builder| builder.with_form(form_builder) { yield(builder) } concat(builder.render_modal_content) end end else builder.with_form(nil) { yield(builder) } builder.render_modal_content end render( partial: "bootstrap5_rails_extensions/modal", locals: { id: id, dialog_class: dialog_classes.join(" "), data: modal_data, content: content_markup, }, ) end |