sdr_view_components 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4e3803526ec08ea8786418750aded1cefd281a20bbc9d9911fe8b684ffaceb25
4
- data.tar.gz: 1db98361a27b37e781a0bb8a17e4e7e913ee22f455ff6b97aaacd115ea5292bc
3
+ metadata.gz: 961d9463dcd2c8acb582e5e97f18c31fff4aa9ec1520456351f6e7aaef232a7e
4
+ data.tar.gz: 6f75438e29d6c7571cdbd54aef23966323fcc85a31b757e777489f4babd5a59c
5
5
  SHA512:
6
- metadata.gz: 7d2e42055690b2e650f841d791f09d122b879b8ccf040f7c25911e080ad91c9221e4c141f1c68720491c9dad77dc50308d3401f58dbe13796a6a3bfad0c32ba6
7
- data.tar.gz: 35e9aee8a9ce3642e04db16e953307c54fcb41f8079fdb4872424f461a43289f1a6cd465be00739a6646213874932c778de351d1db58dd61b55ef473eb65d20d
6
+ metadata.gz: 8de70284a279feae9500bdeb0dabef38e67a8f0433b67d2dfeccbc34f72b78c7703061a3e3fb7ad234148d6d2c0f9738953e01554bed19bfc5f69ce6b13264ce
7
+ data.tar.gz: 8be397da249a2e0871f80f78e9ae3ee65d237c5437ee3e79dc20b4db9548403bd7802f0f1b397a4d350793f8c688c4e6b08099439098c44b9874ffe924462743
data/README.md CHANGED
@@ -38,7 +38,7 @@ Supported header variations are `:dark`, `:light`, and `:white` (default is `:li
38
38
  <% end %>
39
39
  <% header.with_primary_navigation_link do %>
40
40
  <%= render SdrViewComponents::Elements::Navigation::DropdownMenuComponent.new(text: 'Logged in: amcollie-preview-dropdown') do |dropdown| %>
41
- <% dropdown.with_link do %>
41
+ <% dropdown.with_item do %>
42
42
  <%= link_to 'Logout', '/Shibboleth.sso/Logout', class: 'dropdown-item' %>
43
43
  <% end %>
44
44
  <% end %>
@@ -63,6 +63,51 @@ The `:dark` variation supports providing an rgb value via the `background_color`
63
63
 
64
64
  By default, the SUL Rosette is included in the header, this can be disabled by setting `rosette: false` in the parameter list when instantiating the header.
65
65
 
66
+ ### Form components
67
+
68
+ SdrViewComponents provides a wrapper for several [ActionView::Helper::Tags](https://api.rubyonrails.org/v8.1.1/classes/ActionView/Helpers/Tags.html).
69
+
70
+ Currently supported tags:
71
+ - Checkbox
72
+ - File
73
+ - RadioButton
74
+ - TextArea
75
+ - TextField
76
+
77
+ #### Composed form components
78
+
79
+ SdrViewComponents provides fully composed (label, help text, and validation) instances of the above components as:
80
+
81
+ - SdrViewComponents::Forms::CheckboxComponent
82
+ - SdrViewComponents::Forms::FileComponent
83
+ - SdrViewComponents::Forms::RadioButtonComponent
84
+ - SdrViewComponents::Forms::TextAreaComponent
85
+ - SdrViewComponents::Forms::TextFieldComponent
86
+
87
+ At a minimum, each of these components must be provided wih the `form:` and `field_name:` parameters. Additionally, you can provide prefixed parameters that will be passed the the individual parts of the components:
88
+
89
+ - `container_`: is used to pass arguments to the surrounding `div` for the component.
90
+ - `input_`: is used to pass arguments to the actual input component.
91
+ - `label_`: is used to pass arguments to the label for the component.
92
+ - `help_`: is used to pass arguments to the help text available for the component.
93
+ - `error_`: is used to pass arguments to the validation output of the component.
94
+
95
+ TODO:
96
+ - Outline the supported params for each prefix
97
+ - Provide an example instantiation and output
98
+
99
+ #### Basic form components
100
+
101
+ Each of the supported components above uses the provided "basic" components:
102
+
103
+ - SdrViewComponents::Forms::BasicCheckboxComponent
104
+ - SdrViewComponents::Forms::BasicFileComponent
105
+ - SdrViewComponents::Forms::BasicRadioButtonComponent
106
+ - SdrViewComponents::Forms::BasicTextAreaComponent
107
+ - SdrViewComponents::Forms::BasicTextFieldComponent
108
+
109
+ At a minimum, each of these components must be provided wih the `form:` and `field_name:` parameters. Additionally, you can provide additional parameters that will be passed to the standard ActionView::Helpers::Tag.
110
+
66
111
  ### General usage:
67
112
 
68
113
  ```
@@ -6,7 +6,7 @@
6
6
  <%= text %>
7
7
  </button>
8
8
  <ul class="dropdown-menu">
9
- <%= items.map do |item| %>
9
+ <% items.each do |item| %>
10
10
  <li><%= item %></li>
11
11
  <% end %>
12
12
  </ul>
@@ -4,18 +4,17 @@ module SdrViewComponents
4
4
  module Forms
5
5
  # Component for form checkbox field
6
6
  class BasicCheckboxComponent < BaseComponent
7
- def initialize(form:, field_name:, data: nil, **args)
7
+ def initialize(form:, field_name:, **args)
8
8
  @form = form
9
9
  @field_name = field_name
10
10
  @args = args
11
- @data = data
12
11
  super()
13
12
  end
14
13
 
15
- attr_reader :args, :data, :form, :field_name, :value
14
+ attr_reader :args, :form, :field_name
16
15
 
17
16
  def call
18
- form.check_box field_name, data:, **args
17
+ form.check_box field_name, **args
19
18
  end
20
19
  end
21
20
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SdrViewComponents
4
+ module Forms
5
+ # Component for form file field
6
+ class BasicFileComponent < BaseComponent
7
+ def initialize(form:, field_name:, **args)
8
+ @form = form
9
+ @field_name = field_name
10
+ @args = args
11
+ super()
12
+ end
13
+
14
+ attr_reader :args, :form, :field_name
15
+
16
+ def call
17
+ form.file_field field_name, **args
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SdrViewComponents
4
+ module Forms
5
+ # Component for form radio button field
6
+ class BasicRadioButtonComponent < BaseComponent
7
+ def initialize(form:, field_name:, value:, **args)
8
+ @form = form
9
+ @field_name = field_name
10
+ @value = value
11
+ @args = args
12
+ super()
13
+ end
14
+
15
+ attr_reader :args, :form, :field_name, :value
16
+
17
+ def call
18
+ form.radio_button field_name, value, **args
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SdrViewComponents
4
+ module Forms
5
+ # Component for form text area field
6
+ class BasicTextAreaComponent < BaseComponent
7
+ def initialize(form:, field_name:, **args)
8
+ @form = form
9
+ @field_name = field_name
10
+ @args = args
11
+ super()
12
+ end
13
+
14
+ attr_reader :args, :form, :field_name
15
+
16
+ def call
17
+ form.text_area field_name, **args
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SdrViewComponents
4
+ module Forms
5
+ # Component for form text field
6
+ class BasicTextFieldComponent < BaseComponent
7
+ def initialize(form:, field_name:, **args)
8
+ @form = form
9
+ @field_name = field_name
10
+ @args = args
11
+ super()
12
+ end
13
+
14
+ attr_reader :args, :form, :field_name
15
+
16
+ def call
17
+ form.text_field field_name, **args
18
+ end
19
+ end
20
+ end
21
+ end
@@ -11,12 +11,8 @@ module SdrViewComponents
11
11
  super
12
12
  end
13
13
 
14
- attr_reader :value
15
-
16
- # The component must implement a `default_component` method in order
17
- # to render in the component slot of the FieldComponent.
18
- def default_component
19
- render SdrViewComponents::Forms::BasicCheckboxComponent.new(form:, field_name:, data:, **input_args)
14
+ def input_component
15
+ SdrViewComponents::Forms::BasicCheckboxComponent.new(form:, field_name:, **input_args)
20
16
  end
21
17
  end
22
18
  end
@@ -1,6 +1,6 @@
1
- <%= tag.div container_args do %>
2
- <%= component %>
1
+ <%= tag.div **container_args do %>
3
2
  <%= render SdrViewComponents::Forms::LabelComponent.new(form:, field_name:, **label_args) %>
4
3
  <%= render SdrViewComponents::Forms::HelpTextComponent.new(**help_text_args) %>
5
- <%= render SdrViewComponents::Forms::InvalidFeedbackComponent.new(form:, field_name:) %>
4
+ <%= render input_component %>
5
+ <%= render SdrViewComponents::Forms::InvalidFeedbackComponent.new(form:, field_name:, **error_args) %>
6
6
  <% end %>
@@ -4,8 +4,6 @@ module SdrViewComponents
4
4
  module Forms
5
5
  # Base component for all form fields.
6
6
  class FieldComponent < BaseComponent
7
- renders_one :component
8
-
9
7
  def initialize(**args)
10
8
  @args = args
11
9
  super()
@@ -13,38 +11,53 @@ module SdrViewComponents
13
11
 
14
12
  attr_reader :args
15
13
 
16
- def input_args
17
- args_for(args:, prefix: 'input_').merge({ aria: field_aria, data: })
14
+ # Any component that inherits from FieldComponent must implement this method
15
+ # or provide its own template.
16
+ def input_component
17
+ raise NotImplementedError
18
18
  end
19
19
 
20
+ ##########################################################################
21
+ # Argument builders for the various sub-components of a form field.
22
+ # These methods extract arguments from the main args hash
23
+ # based on a prefix convention.
24
+
25
+ # Returns a hash of arguments for the container element.
26
+ # Prefix and argument with 'container_' in order for it to be passed to
27
+ # the container div. (i.e. :container_class will be passed as :class to
28
+ # the container div)
20
29
  def container_args
21
30
  args_for(args:, prefix: 'container_')
22
31
  end
23
32
 
24
- def data
25
- @data ||= args.delete(:data) || {}
33
+ # Returns a hash of arguments for the error element.
34
+ # Prefix and argument with 'error_' in order for it to be passed to
35
+ # to the invalid feedback element.
36
+ def error_args
37
+ args_for(args:, prefix: 'error_')
26
38
  end
27
39
 
28
- def error_aria
29
- InvalidFeedbackSupport.arias_for(field_name:, form:).tap do |arias|
30
- arias[:describedby] = merge_actions(arias[:describedby], help_text_id) if help_text_args[:text].present?
31
- end
40
+ # Returns a hash of arguments for the help text element.
41
+ # Prefix and argument with 'help_' in order for it to be passed to
42
+ # to the help text element.
43
+ def help_text_args
44
+ args_for(args:, prefix: 'help_').merge({ id: help_text_id })
32
45
  end
33
46
 
34
- def error_classes
35
- merge_classes(args.fetch(:error_classes, []))
47
+ # Returns a hash of arguments for the input element.
48
+ # Prefix and argument with 'input_' in order for it to be passed to
49
+ # the input element.
50
+ def input_args
51
+ args_for(args:, prefix: 'input_').merge({ aria: field_aria })
36
52
  end
37
53
 
38
- def field_aria
39
- error_aria.tap do |arias|
40
- # Set aria-required if we want to indicate required, but the field
41
- # does not actually have a required attribute
42
- #
43
- # This is used for collection/work forms where we do server-side
44
- # validation and don't want to block form submission on empty fields
45
- arias[:required] = true if @mark_required
46
- end
54
+ # Returns a hash of arguments for the label element.
55
+ # Prefix and argument with 'label_' in order for it to be passed to
56
+ # the label element.
57
+ def label_args
58
+ args_for(args:, prefix: 'label_')
47
59
  end
60
+ ##########################################################################
48
61
 
49
62
  def field_name
50
63
  @field_name ||= args.delete(:field_name)
@@ -54,21 +67,28 @@ module SdrViewComponents
54
67
  @form ||= args.delete(:form)
55
68
  end
56
69
 
57
- def help_text_args
58
- args_for(args:, prefix: 'help_').merge({
59
- id: help_text_id
60
- })
61
- end
70
+ private
62
71
 
63
- def help_text_id
64
- @help_text_id ||= form.field_id(field_name, 'help')
72
+ def error_aria
73
+ InvalidFeedbackSupport.arias_for(field_name:, form:).tap do |arias|
74
+ arias[:describedby] = merge_actions(arias[:describedby], help_text_id) if help_text_args[:text].present?
75
+ end
65
76
  end
66
77
 
67
- def label_args
68
- args_for(args:, prefix: 'label_')
78
+ def field_aria
79
+ error_aria.tap do |arias|
80
+ # Set aria-required if we want to indicate required, but the field
81
+ # does not actually have a required attribute
82
+ #
83
+ # This is used for collection/work forms where we do server-side
84
+ # validation and don't want to block form submission on empty fields
85
+ arias[:required] = true if @args[:mark_required]
86
+ end
69
87
  end
70
88
 
71
- delegate :id, to: :form, prefix: true
89
+ def help_text_id
90
+ @help_text_id ||= form.field_id(field_name, 'help')
91
+ end
72
92
  end
73
93
  end
74
94
  end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SdrViewComponents
4
+ module Forms
5
+ # Component for form file field
6
+ class FileComponent < FieldComponent
7
+ def initialize(**args)
8
+ args[:container_class] = merge_classes('form-file', args[:container_class])
9
+ args[:input_class] = merge_classes('form-file-input', args[:input_class])
10
+ args[:label_default_class] = merge_classes('form-file-label', args[:input_class])
11
+ super
12
+ end
13
+
14
+ def input_component
15
+ SdrViewComponents::Forms::BasicFileComponent.new(form:, field_name:, **input_args)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SdrViewComponents
4
+ module Forms
5
+ # Component for form radio button field
6
+ class RadioButtonComponent < FieldComponent
7
+ def initialize(**args)
8
+ args[:container_class] = merge_classes('form-check', args[:container_class])
9
+ args[:input_class] = merge_classes('form-check-input', args[:input_class])
10
+ args[:label_default_class] = merge_classes('form-check-label', args[:input_class])
11
+ super
12
+ end
13
+
14
+ def input_component
15
+ SdrViewComponents::Forms::BasicRadioButtonComponent.new(form:, field_name:, **input_args)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SdrViewComponents
4
+ module Forms
5
+ # Component for form text area field
6
+ class TextAreaComponent < FieldComponent
7
+ def initialize(**args)
8
+ args[:container_class] = merge_classes('form-text', args[:container_class])
9
+ args[:input_class] = merge_classes('form-control', args[:input_class])
10
+ super
11
+ end
12
+
13
+ def input_component
14
+ SdrViewComponents::Forms::BasicTextAreaComponent.new(form:, field_name:, **input_args)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SdrViewComponents
4
+ module Forms
5
+ # Component for form text field
6
+ class TextFieldComponent < FieldComponent
7
+ def initialize(**args)
8
+ args[:container_class] = merge_classes('field-container', args[:container_class])
9
+ args[:input_class] = merge_classes('form-control', args[:input_class])
10
+ super
11
+ end
12
+
13
+ def input_component
14
+ SdrViewComponents::Forms::BasicTextFieldComponent.new(form:, field_name:, **input_args)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SdrViewComponents
4
- VERSION = '0.1.4'
4
+ VERSION = '0.1.6'
5
5
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sdr_view_components
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Collier
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-12-10 00:00:00.000000000 Z
10
+ date: 2025-12-15 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rails
@@ -86,19 +86,26 @@ files:
86
86
  - app/components/sdr_view_components/elements/tooltip_component.html.erb
87
87
  - app/components/sdr_view_components/elements/tooltip_component.rb
88
88
  - app/components/sdr_view_components/forms/basic_checkbox_component.rb
89
+ - app/components/sdr_view_components/forms/basic_file_component.rb
90
+ - app/components/sdr_view_components/forms/basic_radio_button_component.rb
91
+ - app/components/sdr_view_components/forms/basic_text_area_component.rb
92
+ - app/components/sdr_view_components/forms/basic_text_field_component.rb
89
93
  - app/components/sdr_view_components/forms/button_component.rb
90
- - app/components/sdr_view_components/forms/checkbox_component.html.erb
91
94
  - app/components/sdr_view_components/forms/checkbox_component.rb
92
95
  - app/components/sdr_view_components/forms/field_component.html.erb
93
96
  - app/components/sdr_view_components/forms/field_component.rb
97
+ - app/components/sdr_view_components/forms/file_component.rb
94
98
  - app/components/sdr_view_components/forms/help_text_component.html.erb
95
99
  - app/components/sdr_view_components/forms/help_text_component.rb
96
100
  - app/components/sdr_view_components/forms/invalid_feedback_component.rb
97
101
  - app/components/sdr_view_components/forms/invalid_feedback_support.rb
98
102
  - app/components/sdr_view_components/forms/label_component.html.erb
99
103
  - app/components/sdr_view_components/forms/label_component.rb
104
+ - app/components/sdr_view_components/forms/radio_button_component.rb
100
105
  - app/components/sdr_view_components/forms/submit_component.html.erb
101
106
  - app/components/sdr_view_components/forms/submit_component.rb
107
+ - app/components/sdr_view_components/forms/text_area_component.rb
108
+ - app/components/sdr_view_components/forms/text_field_component.rb
102
109
  - app/components/sdr_view_components/forms/toggle_component.html.erb
103
110
  - app/components/sdr_view_components/forms/toggle_component.rb
104
111
  - app/components/sdr_view_components/forms/toggle_option_component.html.erb
@@ -1,6 +0,0 @@
1
- <%= tag.div container_args do %>
2
- <%= render SdrViewComponents::Forms::BasicCheckboxComponent.new(form:, field_name:, data:, **input_args) %>
3
- <%= render SdrViewComponents::Forms::LabelComponent.new(form:, field_name:, **label_args) %>
4
- <%= render SdrViewComponents::Forms::HelpTextComponent.new(**help_text_args) %>
5
- <%= render SdrViewComponents::Forms::InvalidFeedbackComponent.new(form:, field_name:) %>
6
- <% end %>