loco_motion-rails 0.0.8 → 0.4.0

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.
@@ -0,0 +1,159 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Daisy
4
+ module FormBuilderHelper
5
+ # Extends ActionView::Helpers::FormBuilder with Daisy UI component methods
6
+ def self.included(base)
7
+ base.class_eval do
8
+ # Add the daisy_checkbox method to FormBuilder
9
+ def daisy_checkbox(name, **options)
10
+ # Get the object name from the form builder
11
+ object_name = @object_name.to_s
12
+
13
+ # Create a unique ID if not provided
14
+ options[:id] ||= "#{object_name}_#{name}"
15
+
16
+ # Set the name attribute
17
+ options[:name] = "#{object_name}[#{name}]"
18
+
19
+ # Pass the form builder's object to the component if it exists
20
+ if @object && @object.respond_to?(name) && !options.key?(:checked)
21
+ options[:checked] = @object.send(name)
22
+ end
23
+
24
+ # Render the checkbox component
25
+ @template.daisy_checkbox(**options)
26
+ end
27
+
28
+ # Add the daisy_toggle method to FormBuilder
29
+ def daisy_toggle(name, **options)
30
+ # Get the object name from the form builder
31
+ object_name = @object_name.to_s
32
+
33
+ # Create a unique ID if not provided
34
+ options[:id] ||= "#{object_name}_#{name}"
35
+
36
+ # Set the name attribute
37
+ options[:name] = "#{object_name}[#{name}]"
38
+
39
+ # Pass the form builder's object to the component if it exists
40
+ if @object && @object.respond_to?(name) && !options.key?(:checked)
41
+ options[:checked] = @object.send(name)
42
+ end
43
+
44
+ # Render the toggle component
45
+ @template.daisy_toggle(**options)
46
+ end
47
+
48
+ # Add the daisy_radio method to FormBuilder
49
+ def daisy_radio(name, **options)
50
+ # Get the object name from the form builder
51
+ object_name = @object_name.to_s
52
+
53
+ # Create a unique ID if not provided
54
+ value = options[:value].to_s
55
+ options[:id] ||= "#{object_name}_#{name}_#{value}"
56
+
57
+ # Set the name attribute
58
+ options[:name] = "#{object_name}[#{name}]"
59
+
60
+ # Pass the form builder's object to the component if it exists
61
+ if @object && @object.respond_to?(name) && !options.key?(:checked)
62
+ options[:checked] = @object.send(name).to_s == value
63
+ end
64
+
65
+ # Render the radio button component
66
+ @template.daisy_radio(**options)
67
+ end
68
+
69
+ # Add the daisy_label method to FormBuilder
70
+ def daisy_label(name, text = nil, **options, &block)
71
+ # Get the object name from the form builder
72
+ object_name = @object_name.to_s
73
+
74
+ # Create a for_id based on the name if not provided
75
+ options[:for] ||= "#{object_name}_#{name}"
76
+
77
+ # Add a humanized title from the name if not provided
78
+ options[:title] ||= text || name.to_s.humanize
79
+
80
+ # Render the label component
81
+ @template.daisy_label(**options, &block)
82
+ end
83
+
84
+ # Add the daisy_range method to FormBuilder
85
+ def daisy_range(method, **options)
86
+ render_daisy_component(Daisy::DataInput::RangeComponent, method, **options)
87
+ end
88
+
89
+ # Add the daisy_rating method to FormBuilder
90
+ def daisy_rating(method, **options)
91
+ render_daisy_component(Daisy::DataInput::RatingComponent, method, **options)
92
+ end
93
+
94
+ # Add the daisy_file_input method to FormBuilder
95
+ def daisy_file_input(method, **options)
96
+ render_daisy_component(Daisy::DataInput::FileInputComponent, method, **options)
97
+ end
98
+
99
+ # Add the daisy_text_input method to FormBuilder
100
+ def daisy_text_input(method, **options)
101
+ render_daisy_component(Daisy::DataInput::TextInputComponent, method, **options)
102
+ end
103
+
104
+ # Add the daisy_text_area method to FormBuilder
105
+ def daisy_text_area(method, **options)
106
+ render_daisy_component(Daisy::DataInput::TextAreaComponent, method, **options)
107
+ end
108
+
109
+ # Add the daisy_select method to FormBuilder
110
+ def daisy_select(method, options: nil, option_groups: nil, placeholder: nil,
111
+ options_css: nil, options_html: {}, **args, &block)
112
+ # Extract the name from the form builder's object_name and method
113
+ name = "#{object_name}[#{method}]"
114
+
115
+ # Get the current value from the object
116
+ value = object.try(method)
117
+
118
+ # Generate a default ID if not provided
119
+ id = args[:id] || "#{object_name}_#{method}"
120
+
121
+ # Build the component with the extracted form values and any additional options
122
+ @template.daisy_select(
123
+ name: name,
124
+ id: id,
125
+ value: value,
126
+ options: options,
127
+ options_css: options_css,
128
+ options_html: options_html,
129
+ placeholder: placeholder,
130
+ **args,
131
+ &block
132
+ )
133
+ end
134
+
135
+ private
136
+
137
+ def render_daisy_component(component_class, method, **options)
138
+ # Get the object name from the form builder
139
+ object_name = @object_name.to_s
140
+
141
+ # Create a unique ID if not provided
142
+ options[:id] ||= "#{object_name}_#{method}"
143
+
144
+ # Set the name attribute
145
+ options[:name] ||= "#{object_name}[#{method}]"
146
+
147
+ # Pass the form builder's object to the component if it exists
148
+ options[:value] ||= object.try(method)
149
+
150
+ # Render the component
151
+ @template.render component_class.new(**options)
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end
157
+
158
+ # Include the FormBuilderHelper in ActionView::Helpers::FormBuilder
159
+ ActionView::Helpers::FormBuilder.include(Daisy::FormBuilderHelper)
data/lib/daisy.rb CHANGED
@@ -22,6 +22,11 @@ module Daisy
22
22
  #
23
23
  module Feedback; end
24
24
 
25
+ #
26
+ # Holds all Data Input-type components.
27
+ #
28
+ module DataInput; end
29
+
25
30
  #
26
31
  # Holds all Layout-type components.
27
32
  #
@@ -1,6 +1,7 @@
1
1
  class LocoMotion::BaseComponent < ViewComponent::Base
2
2
 
3
3
  SELF_CLOSING_TAGS = %i[area base br col embed hr img input keygen link meta param source track wbr].freeze
4
+ EMPTY_PART_IGNORED_TAGS = %i[textarea].freeze
4
5
 
5
6
  include Heroicons::IconsHelper
6
7
 
@@ -10,7 +11,7 @@ class LocoMotion::BaseComponent < ViewComponent::Base
10
11
  class_attribute :valid_sizes, default: []
11
12
 
12
13
  #
13
- # Return the current configruation of this component.
14
+ # Return the current configuration of this component.
14
15
  #
15
16
  # @return LocoMotion::ComponentConfig
16
17
  #
@@ -225,12 +226,18 @@ class LocoMotion::BaseComponent < ViewComponent::Base
225
226
  tag(tag_name, **rendered_html(part_name))
226
227
  else
227
228
  content_tag(tag_name, **rendered_html(part_name)) do
228
- "<!-- Empty Part Block //-->".html_safe
229
+ empty_part_content(tag_name)
229
230
  end
230
231
  end
231
232
  end
232
233
  end
233
234
 
235
+ def empty_part_content(tag_name)
236
+ unless EMPTY_PART_IGNORED_TAGS.include?(tag_name.to_sym)
237
+ "<!-- Empty Part Block //-->".html_safe
238
+ end
239
+ end
240
+
234
241
  #
235
242
  # Returns the user-provided or component-default HTML tag-name.
236
243
  #
@@ -4,5 +4,11 @@ module LocoMotion
4
4
 
5
5
  config.autoload_paths << "#{root}/app"
6
6
  config.autoload_paths << "#{root}/lib"
7
+
8
+ initializer "loco_motion.form_builder_extensions" do
9
+ ActiveSupport.on_load(:action_view) do
10
+ require_relative "../../app/helpers/daisy/form_builder_helper"
11
+ end
12
+ end
7
13
  end
8
14
  end
@@ -29,6 +29,18 @@ module LocoMotion
29
29
  "Daisy::DataDisplay::TableComponent" => { names: "table", group: "Data", title: "Tables", example: "tables" },
30
30
  "Daisy::DataDisplay::TimelineComponent" => { names: "timeline", group: "Data", title: "Timelines", example: "timelines" },
31
31
 
32
+ # Data Input
33
+ "Daisy::DataInput::CheckboxComponent" => { names: "checkbox", group: "Data Input", title: "Checkboxes", example: "checkboxes" },
34
+ "Daisy::DataInput::FileInputComponent" => { names: "file_input", group: "Data Input", title: "File Inputs", example: "file_inputs" },
35
+ "Daisy::DataInput::LabelComponent" => { names: "label", group: "Data Input", title: "Labels", example: "labels" },
36
+ "Daisy::DataInput::RadioButtonComponent" => { names: "radio", group: "Data Input", title: "Radio Buttons", example: "radio_buttons" },
37
+ "Daisy::DataInput::RangeComponent" => { names: "range", group: "Data Input", title: "Ranges", example: "ranges" },
38
+ "Daisy::DataInput::RatingComponent" => { names: "rating", group: "Data Input", title: "Ratings", example: "ratings" },
39
+ "Daisy::DataInput::SelectComponent" => { names: "select", group: "Data Input", title: "Selects", example: "selects" },
40
+ "Daisy::DataInput::TextInputComponent" => { names: "text_input", group: "Data Input", title: "Text Inputs", example: "text_inputs" },
41
+ "Daisy::DataInput::TextAreaComponent" => { names: "text_area", group: "Data Input", title: "Text Areas", example: "text_areas" },
42
+ "Daisy::DataInput::ToggleComponent" => { names: "toggle", group: "Data Input", title: "Toggles", example: "toggles" },
43
+
32
44
  # Navigation
33
45
  "Daisy::Navigation::BreadcrumbsComponent" => { names: "breadcrumbs", group: "Navigation", title: "Breadcrumbs", example: "breadcrumbs" },
34
46
  "Daisy::Navigation::BottomNavComponent" => { names: "bottom_nav", group: "Navigation", title: "Bottom Navs", example: "bottom_navs" },
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LocoMotion
4
- VERSION = "0.0.8"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loco_motion-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Topher Fangio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-03-01 00:00:00.000000000 Z
11
+ date: 2025-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: haml-rails
@@ -42,16 +42,22 @@ dependencies:
42
42
  name: rails
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '6.1'
48
+ - - "<"
49
+ - !ruby/object:Gem::Version
50
+ version: '8.0'
48
51
  type: :runtime
49
52
  prerelease: false
50
53
  version_requirements: !ruby/object:Gem::Requirement
51
54
  requirements:
52
- - - "~>"
55
+ - - ">="
53
56
  - !ruby/object:Gem::Version
54
57
  version: '6.1'
58
+ - - "<"
59
+ - !ruby/object:Gem::Version
60
+ version: '8.0'
55
61
  - !ruby/object:Gem::Dependency
56
62
  name: view_component
57
63
  requirement: !ruby/object:Gem::Requirement
@@ -238,16 +244,22 @@ dependencies:
238
244
  name: rails
239
245
  requirement: !ruby/object:Gem::Requirement
240
246
  requirements:
241
- - - "~>"
247
+ - - ">="
242
248
  - !ruby/object:Gem::Version
243
249
  version: '6.1'
250
+ - - "<"
251
+ - !ruby/object:Gem::Version
252
+ version: '8.0'
244
253
  type: :development
245
254
  prerelease: false
246
255
  version_requirements: !ruby/object:Gem::Requirement
247
256
  requirements:
248
- - - "~>"
257
+ - - ">="
249
258
  - !ruby/object:Gem::Version
250
259
  version: '6.1'
260
+ - - "<"
261
+ - !ruby/object:Gem::Version
262
+ version: '8.0'
251
263
  - !ruby/object:Gem::Dependency
252
264
  name: redcarpet
253
265
  requirement: !ruby/object:Gem::Requirement
@@ -384,6 +396,19 @@ files:
384
396
  - app/components/daisy/data_display/timeline_component.rb
385
397
  - app/components/daisy/data_display/timeline_event_component.html.haml
386
398
  - app/components/daisy/data_display/timeline_event_component.rb
399
+ - app/components/daisy/data_input/checkbox_component.rb
400
+ - app/components/daisy/data_input/file_input_component.rb
401
+ - app/components/daisy/data_input/label_component.rb
402
+ - app/components/daisy/data_input/radio_button_component.rb
403
+ - app/components/daisy/data_input/range_component.rb
404
+ - app/components/daisy/data_input/rating_component.html.haml
405
+ - app/components/daisy/data_input/rating_component.rb
406
+ - app/components/daisy/data_input/select_component.html.haml
407
+ - app/components/daisy/data_input/select_component.rb
408
+ - app/components/daisy/data_input/text_area_component.rb
409
+ - app/components/daisy/data_input/text_input_component.html.haml
410
+ - app/components/daisy/data_input/text_input_component.rb
411
+ - app/components/daisy/data_input/toggle_component.rb
387
412
  - app/components/daisy/feedback/alert_component.html.haml
388
413
  - app/components/daisy/feedback/alert_component.rb
389
414
  - app/components/daisy/feedback/loading_component.rb
@@ -418,6 +443,7 @@ files:
418
443
  - app/components/daisy/navigation/tabs_component.html.haml
419
444
  - app/components/daisy/navigation/tabs_component.rb
420
445
  - app/components/hero/icon_component.rb
446
+ - app/helpers/daisy/form_builder_helper.rb
421
447
  - lib/daisy.rb
422
448
  - lib/hero.rb
423
449
  - lib/loco_motion.rb