Skip to content

Commit

Permalink
Make attribute setting explicit. (#4)
Browse files Browse the repository at this point in the history
With prop_template removing implicit conversations using camelize(:lower), this
commit makes the key formatting the responsibility of form_props. To do this,
we keep a mapping of html attributes => React attributes as provided by React,
and introduce our own with certain options that are needed for frontend components
like `include_hidden`.

The mapping is used right before we pass the keys to prop_template.
  • Loading branch information
jho406 committed Nov 26, 2023
1 parent 5e3bc08 commit 2c5ebd0
Show file tree
Hide file tree
Showing 13 changed files with 562 additions and 22 deletions.
2 changes: 1 addition & 1 deletion form_props.gemspec
Expand Up @@ -16,5 +16,5 @@ Gem::Specification.new do |s|

s.add_dependency "activesupport", ">= 7.0.0"
s.add_dependency "actionview", ">= 7.0.0"
s.add_dependency "props_template", ">= 0.23.0"
s.add_dependency "props_template", ">= 0.30.0"
end
1 change: 1 addition & 0 deletions lib/form_props.rb
Expand Up @@ -2,6 +2,7 @@

require "action_view"
require "action_pack"
require "form_props/helper"
require "form_props/action_view_extensions/form_helper"
require "form_props/form_options_helper"
require "form_props/inputs/base"
Expand Down
16 changes: 8 additions & 8 deletions lib/form_props/action_view_extensions/form_helper.rb
Expand Up @@ -40,12 +40,12 @@ def form_props(model: nil, scope: nil, url: nil, format: nil, **options, &block)
end

html_options = html_options_for_form_with(url, model, **options)
html_options["acceptCharset"] ||= html_options.delete("accept-charset")

json.extras do
extra_props_for_form(json, html_options)
end
json.props(html_options)

json.props(FormProps::Helper.format_keys(html_options))
end

private
Expand All @@ -62,8 +62,8 @@ def token_props(json, token = nil, form_options: {})
json.set!("csrf") do
json.name request_forgery_protection_token.to_s
json.type "hidden"
json.default_value token
json.auto_complete "off"
json.defaultValue token
json.autoComplete "off"
end
end
end
Expand All @@ -72,17 +72,17 @@ def method_props(json, method)
json.set!("method") do
json.name "_method"
json.type "hidden"
json.default_value method.to_s
json.auto_complete "off"
json.defaultValue method.to_s
json.autoComplete "off"
end
end

def utf8_enforcer_props(json)
json.set!("utf8") do
json.name "utf8"
json.type "hidden"
json.default_value "✓"
json.auto_complete "off"
json.defaultValue "✓"
json.autoComplete "off"
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/form_props/form_builder.rb
Expand Up @@ -151,7 +151,7 @@ def fields_for_with_nested_attributes(association_name, association, options, bl
if association.respond_to?(:to_ary)
explicit_child_index = options[:child_index]

json.set!("#{association_name}_attributes") do
json.set!("#{association_name}Attributes") do
json.array! association do |child|
if explicit_child_index
options[:child_index] = explicit_child_index.call if explicit_child_index.respond_to?(:call)
Expand All @@ -163,7 +163,7 @@ def fields_for_with_nested_attributes(association_name, association, options, bl
end
end
elsif association
json.set!("#{association_name}_attributes") do
json.set!("#{association_name}Attributes") do
fields_for_nested_model(name, association, options, block)
end
end
Expand Down

0 comments on commit 2c5ebd0

Please sign in to comment.