forked from primer/view_components
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve interop with Rails FormBuilder/
form_with
* Teaches `form_arguments` `:builder` and `:name` options. `:builder` should be an instance of `ActionView::Helpers::FormBuilder`, which is created by the standard Rails `#form_with` and `#form_for` helpers. * Uses Rails Form Helpers under-the-hood to render form tags, ensuring automatic generation of CSRF token, `_method` hidden inputs.
- Loading branch information
Showing
11 changed files
with
139 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
app/components/primer/open_project/danger_confirmation_dialog/form_wrapper.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<% if renders_form? %> | ||
<%= form_with(url: @action, method: @http_method, **@form_arguments) do %> | ||
<%= content %> | ||
<% end %> | ||
<% else %> | ||
<%= content %> | ||
<% end %> |
26 changes: 26 additions & 0 deletions
26
app/components/primer/open_project/danger_confirmation_dialog/form_wrapper.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module Primer | ||
module OpenProject | ||
class DangerConfirmationDialog | ||
# Utility component for wrapping DangerConfirmationDialog in a form | ||
class FormWrapper < Primer::Component | ||
def initialize(builder: nil, action: nil, **form_arguments) | ||
raise ArgumentError, "Pass in either a :builder or :action argument, not both." if builder && action | ||
|
||
@builder = builder | ||
@action = action | ||
@form_arguments = deny_tag_argument(**form_arguments) | ||
end | ||
|
||
def renders_form? | ||
!@builder && @action | ||
end | ||
|
||
def shows_form? | ||
@builder || @action | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
previews/primer/open_project/danger_confirmation_dialog_preview/with_form.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<%= render(Primer::OpenProject::DangerConfirmationDialog.new( | ||
title: "Delete dialog", | ||
form_arguments: { action: generic_form_submission_path(format: route_format) } | ||
)) do |dialog| %> | ||
<% dialog.with_show_button { "Click me" } %> | ||
<% dialog.with_confirmation_message do |message| | ||
message.with_heading(tag: :h2).with_content("Permanently delete?") | ||
message.with_description_content("This action is not reversible. Please proceed with caution.") | ||
end %> | ||
<% dialog.with_confirmation_check_box_content("I understand that this deletion cannot be reversed") %> | ||
<% end %> |
13 changes: 13 additions & 0 deletions
13
...ws/primer/open_project/danger_confirmation_dialog_preview/with_form_builder_form.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<%= form_with(url: generic_form_submission_path(format: route_format)) do |f| %> | ||
<%= render(Primer::OpenProject::DangerConfirmationDialog.new( | ||
title: "Delete dialog", | ||
form_arguments: { builder: f, name: "confirm_very_dangerous_action" } | ||
)) do |dialog| %> | ||
<% dialog.with_show_button { "Click me" } %> | ||
<% dialog.with_confirmation_message do |message| | ||
message.with_heading(tag: :h2).with_content("Permanently delete?") | ||
message.with_description_content("This action is not reversible. Please proceed with caution.") | ||
end %> | ||
<% dialog.with_confirmation_check_box_content("I understand that this deletion cannot be reversed") %> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters