form_props 0.0.5 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6c4781f03e94d7cdc4b2c3868e6283c8e3c6241e97eb869d957121b665b2deeb
4
- data.tar.gz: c5696aaf2cd138ca74869c228dee0d1f368ed3822c72df389dbfa5d3aaa36103
3
+ metadata.gz: 90a1bcba67478294801b8ede3004d15a41d78eafa3cd7b55a2d533749979c887
4
+ data.tar.gz: 1cceaf16df12dad4a4570809a745fca0046c2d616e44a2fb1bce374a83e71c03
5
5
  SHA512:
6
- metadata.gz: e51e3147f39f549f35d940d5b1f900213e363c6108e06b190d7d1d6dfa318795092986e87edf3cf26bbffcfa4f89b2be4fe623213acc913530902928614b7691
7
- data.tar.gz: 63047633479d69d86f7bbd219e760e4d28825a2d2a68863266331365ccab6140cf480221ad2192ff96996a7684d5456e62423b60ae88a20f3bc30965e6368783
6
+ metadata.gz: b92a397b40ac479f7c54a8488b0134b7cdba4ce82cf0c581362134b06aea46ed24e8bfefda33f38b188e79c17c9214e39f6fd5c525c7892e11588e6cd149f9d3
7
+ data.tar.gz: ac20f5814f8c0298758164dc6f26fa7b4a0ab793a4210d5f8bfda27ba10cf6f0c8aefe438b65539863342f1adc35fb67fc3159171141fcb26dfc4f0a55066020
data/README.md CHANGED
@@ -1,11 +1,11 @@
1
- # Form Props
1
+ # form_props
2
2
 
3
3
  ![Build Status](https://github.com/thoughtbot/form_props/actions/workflows/build.yml/badge.svg?branch=main)
4
4
 
5
- FormProps is a Rails form builder that outputs HTML props instead of tags. Now
5
+ form_props is a Rails form builder that outputs HTML props instead of tags. Now
6
6
  you can enjoy the power and convenience of Rails helpers in React!
7
7
 
8
- By separting attributes from tags, FormProps can offer greater flexbility than normal
8
+ By separting attributes from tags, form_props can offer greater flexbility than normal
9
9
  Rails form builders; allowing designers to stay longer in HTML land and more easily
10
10
  customize their form structure without needing to know Rails.
11
11
 
@@ -98,7 +98,7 @@ documentation here reflects that default. You can change that [behavior](https:/
98
98
  if you wish.
99
99
 
100
100
  ## Flexibility
101
- FormProps is only concerned about attributes, the designer can focus on tag
101
+ form_props is only concerned about attributes, the designer can focus on tag
102
102
  structure and stay longer in HTML land. For example, you can decide to nest an
103
103
  input inside a label.
104
104
 
@@ -153,7 +153,7 @@ export default (({props, inputs, extras})) => {
153
153
 
154
154
  ## Error handling
155
155
 
156
- FormProps doesn't handle form errors, but you can easily add this functionality:
156
+ form_props doesn't handle form errors, but you can easily add this functionality:
157
157
 
158
158
  ```ruby
159
159
  json.someForm do
@@ -139,6 +139,7 @@ module FormProps
139
139
 
140
140
  def fields_for_with_nested_attributes(association_name, association, options, block)
141
141
  name = "#{object_name}[#{association_name}_attributes]"
142
+ association_key = "#{association_name.to_s.camelize(:lower)}Attributes"
142
143
  association = convert_to_model(association)
143
144
  json = @template.instance_variable_get(:@__json)
144
145
 
@@ -151,7 +152,7 @@ module FormProps
151
152
  if association.respond_to?(:to_ary)
152
153
  explicit_child_index = options[:child_index]
153
154
 
154
- json.set!("#{association_name}Attributes") do
155
+ json.set!(association_key) do
155
156
  json.array! association do |child|
156
157
  if explicit_child_index
157
158
  options[:child_index] = explicit_child_index.call if explicit_child_index.respond_to?(:call)
@@ -163,7 +164,7 @@ module FormProps
163
164
  end
164
165
  end
165
166
  elsif association
166
- json.set!("#{association_name}Attributes") do
167
+ json.set!(association_key) do
167
168
  fields_for_nested_model(name, association, options, block)
168
169
  end
169
170
  end
@@ -11,34 +11,15 @@ module FormProps
11
11
  options = options.with_indifferent_access
12
12
 
13
13
  @controlled = options.delete(:controlled)
14
+ @key = options.delete(:key)
15
+
14
16
  super(object_name, method_name, template_object, options)
15
17
  end
16
18
 
17
19
  private
18
20
 
19
21
  def sanitized_key
20
- sanitized_method_name.camelize(:lower)
21
- end
22
-
23
- def add_options(option_tags, options, value = nil)
24
- if options[:include_blank]
25
- content = (options[:include_blank] if options[:include_blank].is_a?(String))
26
- label = (" " unless content)
27
- option_tags = [{label: content || label, value: ""}] + option_tags
28
- end
29
-
30
- if value.blank? && options[:prompt]
31
- tag_options = {value: ""}.tap do |prompt_opts|
32
- prompt_opts[:disabled] = true if options[:disabled] == ""
33
- if options[:selected] == ""
34
- selected_values.push("")
35
- end
36
- prompt_opts[:label] = prompt_text(options[:prompt])
37
- end
38
- option_tags = [tag_options] + option_tags
39
- end
40
-
41
- option_tags
22
+ @key || sanitized_method_name.camelize(:lower)
42
23
  end
43
24
 
44
25
  def input_props(options)
@@ -129,41 +110,6 @@ module FormProps
129
110
 
130
111
  tag_values
131
112
  end
132
-
133
- def select_content_props(option_tags, options, html_options)
134
- html_options = html_options.stringify_keys
135
- add_default_name_and_id(html_options)
136
-
137
- if placeholder_required?(html_options)
138
- raise ArgumentError, "include_blank cannot be false for a required field." if options[:include_blank] == false
139
- options[:include_blank] ||= true unless options[:prompt]
140
- end
141
-
142
- html_options["type"] = "select"
143
- value_for_blank = options.fetch(:selected) { value }
144
- option_tags = add_options(option_tags, options, value_for_blank)
145
-
146
- if options[:multiple]
147
- html_options["multiple"] = options[:multiple]
148
- end
149
-
150
- if selected_values.any?
151
- html_options["value"] ||= if html_options["multiple"]
152
- Array(selected_values)
153
- else
154
- selected_values.first
155
- end
156
- end
157
-
158
- json.set!(sanitized_key) do
159
- input_props(html_options)
160
-
161
- if options.key?(:include_hidden)
162
- json.includeHidden options[:include_hidden]
163
- end
164
- json.options(option_tags)
165
- end
166
- end
167
113
  end
168
114
  end
169
115
  end
@@ -6,6 +6,8 @@ module FormProps
6
6
  module Inputs
7
7
  class CollectionCheckBoxes < Base
8
8
  include ActionView::Helpers::Tags::CollectionHelpers
9
+ include ActionView::Helpers::FormOptionsHelper
10
+
9
11
  include CollectionHelpers
10
12
 
11
13
  class CheckBoxBuilder < Builder
@@ -6,6 +6,7 @@ module FormProps
6
6
  module Inputs
7
7
  class CollectionRadioButtons < Base
8
8
  include ActionView::Helpers::Tags::CollectionHelpers
9
+ include ActionView::Helpers::FormOptionsHelper
9
10
  include CollectionHelpers
10
11
 
11
12
  class RadioButtonBuilder < Builder
@@ -2,8 +2,13 @@
2
2
 
3
3
  module FormProps
4
4
  module Inputs
5
- class CollectionSelect < Base # :nodoc:
5
+ class CollectionSelect < Base
6
+ if ActionView::VERSION::STRING >= "7.1"
7
+ include ActionView::Helpers::Tags::SelectRenderer
8
+ end
9
+ include ActionView::Helpers::FormOptionsHelper
6
10
  include FormOptionsHelper
11
+ include SelectRenderer
7
12
 
8
13
  def initialize(object_name, method_name, template_object, collection, value_method, text_method, options, html_options)
9
14
  @collection = collection
@@ -6,7 +6,7 @@ module FormProps
6
6
  private
7
7
 
8
8
  def format_date(value)
9
- value&.strftime("%Y-%m-%d")
9
+ value.presence&.strftime("%Y-%m-%d")
10
10
  end
11
11
 
12
12
  def field_type
@@ -3,7 +3,12 @@
3
3
  module FormProps
4
4
  module Inputs
5
5
  class GroupedCollectionSelect < Base
6
+ if ActionView::VERSION::STRING >= "7.1"
7
+ include ActionView::Helpers::Tags::SelectRenderer
8
+ end
9
+ include ActionView::Helpers::FormOptionsHelper
6
10
  include FormOptionsHelper
11
+ include SelectRenderer
7
12
 
8
13
  def initialize(object_name, method_name, template_object, collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options)
9
14
  @collection = collection
@@ -22,8 +22,6 @@ module FormProps
22
22
  @options[:value] = @tag_value
23
23
  @options[:checked] = true if input_checked?(@options)
24
24
 
25
- name_for_key = (sanitized_method_name + "_#{sanitized_value(@tag_value)}").camelize(:lower)
26
-
27
25
  body_block = -> {
28
26
  add_default_name_and_id_for_value(@tag_value, @options)
29
27
  input_props(@options)
@@ -32,7 +30,7 @@ module FormProps
32
30
  if flatten
33
31
  body_block.call
34
32
  else
35
- json.set!(name_for_key) do
33
+ json.set!(sanitized_key) do
36
34
  body_block.call
37
35
  end
38
36
  end
@@ -40,6 +38,10 @@ module FormProps
40
38
 
41
39
  private
42
40
 
41
+ def sanitized_key
42
+ @key || (sanitized_method_name + "_#{sanitized_value(@tag_value)}").camelize(:lower)
43
+ end
44
+
43
45
  def checked?(value)
44
46
  value.to_s == @tag_value.to_s
45
47
  end
@@ -3,7 +3,12 @@
3
3
  module FormProps
4
4
  module Inputs
5
5
  class Select < Base
6
+ if ActionView::VERSION::STRING >= "7.1"
7
+ include ActionView::Helpers::Tags::SelectRenderer
8
+ end
9
+ include ActionView::Helpers::FormOptionsHelper
6
10
  include FormOptionsHelper
11
+ include SelectRenderer
7
12
 
8
13
  def initialize(object_name, method_name, template_object, choices, options, html_options)
9
14
  @choices = choices
@@ -3,7 +3,12 @@
3
3
  module FormProps
4
4
  module Inputs
5
5
  class TimeZoneSelect < Base
6
+ if ActionView::VERSION::STRING >= "7.1"
7
+ include ActionView::Helpers::Tags::SelectRenderer
8
+ end
9
+ include ActionView::Helpers::FormOptionsHelper
6
10
  include FormOptionsHelper
11
+ include SelectRenderer
7
12
 
8
13
  def initialize(object_name, method_name, template_object, priority_zones, options, html_options)
9
14
  @priority_zones = priority_zones
@@ -3,7 +3,12 @@
3
3
  module FormProps
4
4
  module Inputs
5
5
  class WeekdaySelect < Base
6
+ if ActionView::VERSION::STRING >= "7.1"
7
+ include ActionView::Helpers::Tags::SelectRenderer
8
+ end
9
+ include ActionView::Helpers::FormOptionsHelper
6
10
  include FormOptionsHelper
11
+ include SelectRenderer
7
12
 
8
13
  def initialize(object_name, method_name, template_object, options, html_options)
9
14
  @html_options = html_options
@@ -0,0 +1,59 @@
1
+ module FormProps
2
+ module SelectRenderer
3
+ def add_options(option_tags, options, value = nil)
4
+ if options[:include_blank]
5
+ content = (options[:include_blank] if options[:include_blank].is_a?(String))
6
+ label = (" " unless content)
7
+ option_tags = [{label: content || label, value: ""}] + option_tags
8
+ end
9
+
10
+ if value.blank? && options[:prompt]
11
+ tag_options = {value: ""}.tap do |prompt_opts|
12
+ prompt_opts[:disabled] = true if options[:disabled] == ""
13
+ if options[:selected] == ""
14
+ selected_values.push("")
15
+ end
16
+ prompt_opts[:label] = prompt_text(options[:prompt])
17
+ end
18
+ option_tags = [tag_options] + option_tags
19
+ end
20
+
21
+ option_tags
22
+ end
23
+
24
+ def select_content_props(option_tags, options, html_options)
25
+ html_options = html_options.stringify_keys
26
+ add_default_name_and_id(html_options)
27
+
28
+ if placeholder_required?(html_options)
29
+ raise ArgumentError, "include_blank cannot be false for a required field." if options[:include_blank] == false
30
+ options[:include_blank] ||= true unless options[:prompt]
31
+ end
32
+
33
+ html_options["type"] = "select"
34
+ value_for_blank = options.fetch(:selected) { value }
35
+ option_tags = add_options(option_tags, options, value_for_blank)
36
+
37
+ if options[:multiple]
38
+ html_options["multiple"] = options[:multiple]
39
+ end
40
+
41
+ if selected_values.any?
42
+ html_options["value"] ||= if html_options["multiple"]
43
+ Array(selected_values)
44
+ else
45
+ selected_values.first
46
+ end
47
+ end
48
+
49
+ json.set!(sanitized_key) do
50
+ input_props(html_options)
51
+
52
+ if options.key?(:include_hidden)
53
+ json.includeHidden options[:include_hidden]
54
+ end
55
+ json.options(option_tags)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FormProps
4
- VERSION = "0.0.5"
4
+ VERSION = "0.0.7"
5
5
  end
data/lib/form_props.rb CHANGED
@@ -5,6 +5,7 @@ require "action_pack"
5
5
  require "form_props/helper"
6
6
  require "form_props/action_view_extensions/form_helper"
7
7
  require "form_props/form_options_helper"
8
+ require "form_props/select_renderer"
8
9
  require "form_props/inputs/base"
9
10
  require "form_props/inputs/text_field"
10
11
  require "form_props/inputs/text_area"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: form_props
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johny Ho
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-26 00:00:00.000000000 Z
11
+ date: 2024-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -94,6 +94,7 @@ files:
94
94
  - lib/form_props/inputs/url_field.rb
95
95
  - lib/form_props/inputs/week_field.rb
96
96
  - lib/form_props/inputs/weekday_select.rb
97
+ - lib/form_props/select_renderer.rb
97
98
  - lib/form_props/version.rb
98
99
  homepage: https://github.com/thoughtbot/form_props/
99
100
  licenses: