felt 0.1.0 → 0.1.1

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: a4d4be91443885d47f5e5aea19104465367551d4e0ef2dc35b110519e84274da
4
- data.tar.gz: 2ba284067b6227021c04bb7f40593acb8a57ec2b762e72fad3f5c077a16a2a29
3
+ metadata.gz: e4cee8afd69693c7c4ab5b113587fa54c63994ceb7e31b662d5c42d2205db328
4
+ data.tar.gz: 8dbefac39a252bc8ff50c24c3175309de8157d0184b82b4ef880f8263486c2a2
5
5
  SHA512:
6
- metadata.gz: 5a3059fe1adc3287eebdd6812a91b80ce82b5f63fbfc9550cf36cd50bca630b0ca8724d1b4004b5e88f6ccef8c3e5d7098af682309c424c6f3d2f4e537c2bfbe
7
- data.tar.gz: 8627ba5a61f4fd64d27c4982d95d0d4860dd6e2d9e74a940110f64fcba804e1c0cd0af426bb7d9daf34a1c8dfaf891f4b0d6ec140a143b544ecfcd6fea2509a2
6
+ metadata.gz: b7ebb4ad07dc4df0d0b1091af1066cb15e83fbf26dc3ced14a7331c59c1ad5198b679f912b6bf48dfb42782a8b23fb41fbe50e2065f3a9833c573734832b2491
7
+ data.tar.gz: c1956e71f6bbdda2716cf712fc1e80661b6a66cfb7e1586532febb767e9bc17db25a7724240c8a47ceaf6f52cdbae737d4b262c7212d61cb0b2552687ec7990f
data/lib/felt/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Felt
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/lib/felt.rb CHANGED
@@ -7,6 +7,9 @@ require_relative "input_group/checkbox_field"
7
7
  require_relative "input_group/email_field"
8
8
  require_relative "input_group/password_field"
9
9
  require_relative "input_group/text_field"
10
+ require_relative "input_group/wrapper"
11
+ require_relative "help"
12
+ require_relative "hint"
10
13
  require_relative "label"
11
14
 
12
15
  module Felt
@@ -0,0 +1 @@
1
+ <div class="<%= classes %>"><%= text %></div>
data/lib/help.rb ADDED
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "view_component"
4
+
5
+ module Felt
6
+ # Renders a help element for a form input.
7
+ class Help < ViewComponent::Base
8
+ attr_reader :attribute, :form, :options
9
+
10
+ # Returns the classes to use for the help element
11
+ def classes
12
+ @classes ||
13
+ Felt.configuration.classes.dig(:help, :default, :default)
14
+ end
15
+
16
+ # - classes: Classes to add to the help element.
17
+ #
18
+ # - text: The help text to show. If not provided, the text will be looked up
19
+ # in the `forms.<object_name>.<attribute>` translation. See #help for more
20
+ # details. To disable the help, pass an empty string.
21
+ #
22
+ # All remaining keyword arguments are passed to the help element. See
23
+ # ActionView::Helpers::FormBuilder#help for details.
24
+ def initialize(attribute:, form:, classes: nil, text: nil, **options)
25
+ @attribute = attribute
26
+ @classes = classes
27
+ @form = form
28
+ @text = text
29
+ @options = options
30
+ end
31
+
32
+ def render?
33
+ text.present?
34
+ end
35
+
36
+ # Returns the text to render in the help. If no text is configured, returns
37
+ # nil.
38
+ #
39
+ # Help texts are looked up in the following order:
40
+ #
41
+ # 1. The text argument passed to the component.
42
+ # 2. The `help` key in the `forms.<object_name>.<attribute>` translation.
43
+ # 3. The translation value found under
44
+ # `helpers.help.<modelname>.<attribute>` (like with
45
+ # ActionView::Helpers::FormBuilder#help).
46
+ def text
47
+ @text ||= translate("help")
48
+ end
49
+
50
+ # Returns true if the input group has a help text configured
51
+ def text?
52
+ text.present?
53
+ end
54
+
55
+ private
56
+
57
+ def translate(key)
58
+ I18n.translate(key, default: nil, scope: translation_scope)
59
+ end
60
+
61
+ def translation_scope
62
+ [:forms, form.object_name, attribute].join(".")
63
+ end
64
+ end
65
+ end
@@ -0,0 +1 @@
1
+ <div class="<%= classes %>"><%= text %></div>
data/lib/hint.rb ADDED
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "view_component"
4
+
5
+ module Felt
6
+ # Renders a hint element for a form input.
7
+ class Hint < ViewComponent::Base
8
+ attr_reader :attribute, :form, :options
9
+
10
+ # Returns the classes to use for the hint element
11
+ def classes
12
+ @classes ||
13
+ Felt.configuration.classes.dig(:hint, :default, :default)
14
+ end
15
+
16
+ # - classes: Classes to add to the hint element.
17
+ #
18
+ # - text: The hint text to show. If not provided, the text will be
19
+ # looked up in the `forms.<object_name>.<attribute>` translation. See
20
+ # #hint for more details. To disable the hint, pass an empty string.
21
+ #
22
+ # All remaining keyword arguments are passed to the hint element. See
23
+ # ActionView::Helpers::FormBuilder#hint for details.
24
+ def initialize(attribute:, form:, classes: nil, text: nil, **options)
25
+ @attribute = attribute
26
+ @classes = classes
27
+ @form = form
28
+ @text = text
29
+ @options = options
30
+ end
31
+
32
+ def render?
33
+ text.present?
34
+ end
35
+
36
+ # Returns the text to render in the hint. If no text is configured, returns
37
+ # nil.
38
+ #
39
+ # Hint texts are looked up in the following order:
40
+ #
41
+ # 1. The text argument passed to the component.
42
+ # 2. The `hint` key in the `forms.<object_name>.<attribute>` translation.
43
+ # 3. The translation value found under
44
+ # `helpers.hint.<modelname>.<attribute>` (like with
45
+ # ActionView::Helpers::FormBuilder#hint).
46
+ def text
47
+ @text ||= translate("hint")
48
+ end
49
+
50
+ # Returns true if the input group has a hint text configured
51
+ def text?
52
+ text.present?
53
+ end
54
+
55
+ private
56
+
57
+ def translate(key)
58
+ I18n.translate(key, default: nil, scope: translation_scope)
59
+ end
60
+
61
+ def translation_scope
62
+ [:forms, form.object_name, attribute].join(".")
63
+ end
64
+ end
65
+ end
@@ -11,7 +11,12 @@ module Felt
11
11
 
12
12
  # Returns the classes to use for the root element of the input.
13
13
  def classes
14
- classes_from_configuration(:input_group, self.class.config_key)
14
+ @classes ||
15
+ classes_from_configuration(:input_group, config_key)
16
+ end
17
+
18
+ def config_key
19
+ raise "Must be implemented in a subclass"
15
20
  end
16
21
 
17
22
  # Returns the error messages to output in the input group. Returns [] if no
@@ -32,7 +37,7 @@ module Felt
32
37
 
33
38
  # Returns the classes to use for the help text.
34
39
  def error_classes
35
- classes_from_configuration(:error, self.class.config_key, state_key) ||
40
+ classes_from_configuration(:error, config_key, state_key) ||
36
41
  classes_from_configuration(:error, :default, state_key)
37
42
  end
38
43
 
@@ -55,7 +60,7 @@ module Felt
55
60
 
56
61
  # Returns the classes to use for the help text.
57
62
  def help_classes
58
- classes_from_configuration(:help, self.class.config_key, state_key) ||
63
+ classes_from_configuration(:help, config_key, state_key) ||
59
64
  classes_from_configuration(:help, :default, state_key)
60
65
  end
61
66
 
@@ -77,10 +82,12 @@ module Felt
77
82
 
78
83
  # Returns the classes to use for the hint text.
79
84
  def hint_classes
80
- classes_from_configuration(:hint, self.class.config_key, state_key) ||
85
+ classes_from_configuration(:hint, config_key, state_key) ||
81
86
  classes_from_configuration(:hint, :default, state_key)
82
87
  end
83
88
 
89
+ # - classes: CSS classes to add to the wrapping input group element.
90
+ #
84
91
  # - hint: The hint for the input group. If not provided, the hint will be
85
92
  # looked up in the `forms.<object_name>.<attribute>` translation. See
86
93
  # #hint for more details. To disable the hint, pass an empty string.
@@ -100,8 +107,9 @@ module Felt
100
107
  # All remaining keyword arguments are passed to the wrapping div element
101
108
  # of the input group. See ActionView::Helpers::TagHelper#content_tag for
102
109
  # details.
103
- def initialize(attribute:, form:, help: nil, hint: nil, input_options: {}, label: nil, placeholder: nil, **options)
110
+ def initialize(attribute:, form:, classes: nil, help: nil, hint: nil, input_options: {}, label: nil, placeholder: nil, **options)
104
111
  @attribute = attribute
112
+ @classes = classes
105
113
  @form = form
106
114
  @help = help
107
115
  @hint = hint
@@ -113,13 +121,13 @@ module Felt
113
121
 
114
122
  # Returns the classes to use for the input field.
115
123
  def input_classes
116
- classes_from_configuration(:input, self.class.config_key, state_key) ||
124
+ classes_from_configuration(:input, config_key, state_key) ||
117
125
  classes_from_configuration(:input, :default, state_key)
118
126
  end
119
127
 
120
128
  # Returns the classes to use for the label field.
121
129
  def label_classes
122
- classes_from_configuration(:label, self.class.config_key, state_key) ||
130
+ classes_from_configuration(:label, config_key, state_key) ||
123
131
  classes_from_configuration(:label, :default, state_key)
124
132
  end
125
133
 
@@ -1,5 +1,5 @@
1
1
  <%= content_tag(:div, :class => classes, **options) do %>
2
- <% if hint? %><div class="<%= hint_classes %>"><%= hint %></div><% end %>
2
+ <%= render(Felt::Hint.new(:form => form, :attribute => attribute, :classes => hint_classes, :text => hint)) %>
3
3
  <div class="flex">
4
4
  <div class="flex items-center h-5">
5
5
  <%= form.check_box(attribute, :class => input_classes, :placeholder => placeholder, **input_options) %>
@@ -9,10 +9,8 @@ module Felt
9
9
  #
10
10
  # The hint text is optional and is rendered before the checkbox.
11
11
  class CheckboxField < InputGroup::Base
12
- class << self
13
- def config_key
14
- :checkbox_field
15
- end
12
+ def config_key
13
+ :checkbox_field
16
14
  end
17
15
  end
18
16
  end
@@ -1,9 +1,3 @@
1
- <%= content_tag(:div, :class => classes, **options) do %>
2
- <%= render(Felt::Label.new(:form => form, :attribute => attribute, :classes => label_classes, :text => label)) %>
3
- <% if hint? %><div class="<%= hint_classes %>"><%= hint %></div><% end %>
1
+ <%= render(Felt::InputGroup::Wrapper.new(attribute: attribute, classes: classes, config_key: config_key, form: form, help: help, hint: hint, input_options: input_options, label: label, placeholder: placeholder, **options)) do %>
4
2
  <%= form.email_field(attribute, :class => input_classes, :placeholder => placeholder, **input_options) %>
5
- <% if help? %><div class="<%= help_classes %>"><%= help %></div><% end %>
6
- <% errors.each do |error| %>
7
- <p class="<%= error_classes %>"><%= error %></p>
8
- <% end %>
9
3
  <% end %>
@@ -5,10 +5,8 @@ require "input_group/base"
5
5
  module Felt
6
6
  module InputGroup
7
7
  class EmailField < InputGroup::Base
8
- class << self
9
- def config_key
10
- :email_field
11
- end
8
+ def config_key
9
+ :email_field
12
10
  end
13
11
  end
14
12
  end
@@ -1,6 +1,6 @@
1
1
  <%= content_tag(:div, :class => classes, **options) do %>
2
2
  <%= render(Felt::Label.new(:form => form, :attribute => attribute, :classes => label_classes, :text => label)) %>
3
- <% if hint? %><div class="<%= hint_classes %>"><%= hint %></div><% end %>
3
+ <%= render(Felt::Hint.new(:form => form, :attribute => attribute, :classes => hint_classes, :text => hint)) %>
4
4
  <%= form.password_field(attribute, :class => input_classes, :placeholder => placeholder, **input_options) %>
5
5
  <% if help? %><div class="<%= help_classes %>"><%= help %></div><% end %>
6
6
  <% errors.each do |error| %>
@@ -8,10 +8,8 @@ module Felt
8
8
  # field is blank by default; pass in a value via options if this is not
9
9
  # desired.
10
10
  class PasswordField < InputGroup::Base
11
- class << self
12
- def config_key
13
- :password_field
14
- end
11
+ def config_key
12
+ :password_field
15
13
  end
16
14
  end
17
15
  end
@@ -1,9 +1,3 @@
1
- <%= content_tag(:div, :class => classes, **options) do %>
2
- <%= render(Felt::Label.new(:form => form, :attribute => attribute, :classes => label_classes, :text => label)) %>
3
- <% if hint? %><div class="<%= hint_classes %>"><%= hint %></div><% end %>
1
+ <%= render(Felt::InputGroup::Wrapper.new(attribute: attribute, classes: classes, config_key: config_key, form: form, help: help, hint: hint, input_options: input_options, label: label, placeholder: placeholder, **options)) do %>
4
2
  <%= form.text_field(attribute, :class => input_classes, :placeholder => placeholder, **input_options) %>
5
- <% if help? %><div class="<%= help_classes %>"><%= help %></div><% end %>
6
- <% errors.each do |error| %>
7
- <p class="<%= error_classes %>"><%= error %></p>
8
- <% end %>
9
3
  <% end %>
@@ -5,10 +5,8 @@ require "input_group/base"
5
5
  module Felt
6
6
  module InputGroup
7
7
  class TextField < InputGroup::Base
8
- class << self
9
- def config_key
10
- :text_field
11
- end
8
+ def config_key
9
+ :text_field
12
10
  end
13
11
  end
14
12
  end
@@ -0,0 +1,9 @@
1
+ <%= content_tag(:div, :class => classes, **options) do %>
2
+ <%= render(Felt::Label.new(:form => form, :attribute => attribute, :classes => label_classes, :text => label)) %>
3
+ <%= render(Felt::Hint.new(:form => form, :attribute => attribute, :classes => hint_classes, :text => hint)) %>
4
+ <%= content %>
5
+ <%= render(Felt::Help.new(:form => form, :attribute => attribute, :classes => help_classes, :text => help)) %>
6
+ <% errors.each do |error| %>
7
+ <p class="<%= error_classes %>"><%= error %></p>
8
+ <% end %>
9
+ <% end %>
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "input_group/base"
4
+
5
+ module Felt
6
+ module InputGroup
7
+ # Encapsulates the common functionality of all input groups.
8
+ #
9
+ # Renders a wrapping div containing a label, a hint, an input field, a help
10
+ # element, and a list of errors.
11
+ #
12
+ # Pass the input field as a block to the constructor.
13
+ class Wrapper < Base
14
+ attr_reader :config_key
15
+
16
+ def initialize(config_key: nil, **args)
17
+ @config_key = config_key
18
+ super(**args)
19
+ end
20
+ end
21
+ end
22
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: felt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakob Skjerning
@@ -51,6 +51,10 @@ files:
51
51
  - lib/felt.rb
52
52
  - lib/felt/configuration.rb
53
53
  - lib/felt/version.rb
54
+ - lib/help.rb
55
+ - lib/help/help.html.erb
56
+ - lib/hint.rb
57
+ - lib/hint/hint.html.erb
54
58
  - lib/input_group/base.rb
55
59
  - lib/input_group/checkbox_field.rb
56
60
  - lib/input_group/checkbox_field/checkbox_field.html.erb
@@ -60,6 +64,8 @@ files:
60
64
  - lib/input_group/password_field/password_field.html.erb
61
65
  - lib/input_group/text_field.rb
62
66
  - lib/input_group/text_field/text_field.html.erb
67
+ - lib/input_group/wrapper.rb
68
+ - lib/input_group/wrapper/wrapper.html.erb
63
69
  - lib/label.rb
64
70
  - lib/label/label.html.erb
65
71
  homepage: https://github.com/substancelab/felt