view_component_reflex 2.1.5 → 2.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3cba771f1bff88b05863347a7bd1d071b8c931cdab779b65223b05e103a841d7
4
- data.tar.gz: 9251cf944ebdc965b26afb5d4e15002a98c230a6efb3dc528f3e88873843f22d
3
+ metadata.gz: 4ac0e4b7fc45ba1963f6d260f66463a00a37fccfb6db10855f608beb8f54126e
4
+ data.tar.gz: eaa30fcda3a41ffc51f651398d490a51b89d1eb06fdf81619e2a9ee0535b62f7
5
5
  SHA512:
6
- metadata.gz: c35f1bf68654bdb97acb33af4e7552fd3b6fb3f3bbf8cf599b214a35340a8648ed614c6ca4281df19446ce997be4ee493b56e08f3480b10d4d84cab38ccd68fb
7
- data.tar.gz: 93a5a0702e21814df172fef57d1cadbc4c91c9d8123affb3d331e3fa8cc98e0c6055257f2c8754e86d6a1ef05170c7aa8ad806455ad8610c650f59aa3a4fc417
6
+ metadata.gz: aa7fed3d3bd4aa4718c5c5358c798b6e0a9edb028d26bb26228989cf6c17f9348036a5ab4d3932c376cb632b7eb145d1e7e1f37a44405e6188609cf1b3942bd8
7
+ data.tar.gz: f761bbdc0c3a11edbcc01e49fe704fa4a8dac9fdf167a5068c2187d28bb35b400afe542edd2d2b7fe2964a0d5a224a299d00de660a47c26525162e12b8d5efea
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  ViewComponentReflex allows you to write reflexes right in your view component code.
4
4
 
5
+ It builds upon [stimulus_reflex](https://github.com/hopsoft/stimulus_reflex) and [view_component](https://github.com/github/view_component)
6
+
5
7
  ## Usage
6
8
 
7
9
  You can add reflexes to your component by adding inheriting from `ViewComponentReflex::Component`.
@@ -88,12 +90,30 @@ This shares the same definition as `content_tag`, except it accepts a reflex as
88
90
 
89
91
  Would add a click handler to the `increment` method on your component.
90
92
 
91
- To use a non-click event, specific that with `->` notiation
93
+ To use a non-click event, specific that with `->` notation
92
94
 
93
95
  ```erb
94
96
  <%= reflex_tag "mouseenter->increment", :button, "Click me!" %>
95
97
  ```
96
98
 
99
+ ### reflex_data_attributes(reflex)
100
+
101
+ This helper will give you the data attributes used in the reflex_tag above if you want to build your own elements.
102
+
103
+ Build your own tag:
104
+
105
+ ```erb
106
+ <%= link_to (image_tag photo.image.url(:medium)), data: reflex_data_attributes(:increment) %>
107
+ ```
108
+
109
+ Render a ViewComponent
110
+
111
+ ```erb
112
+ <%= render ButtonComponent.new(data: reflex_data_attributes("mouseenter->increment")) %>
113
+ ```
114
+
115
+ Make sure that you assign the reflex_data_attributes to the correct element in your component.
116
+
97
117
  ### collection_key
98
118
  If you're rendering a component as a collection with `MyComponent.with_collection(SomeCollection)`, you must define this method to return some unique value for the component.
99
119
  This is used to reconcile state in the background.
@@ -279,9 +299,9 @@ Or install it yourself as:
279
299
  $ gem install view_component_reflex
280
300
  ```
281
301
 
282
- ## Common problems
302
+ # Common problems
283
303
 
284
- # Uninitialized constants \<component\>Reflex
304
+ ## Uninitialized constants \<component\>Reflex
285
305
  A component needs to be wrapped in `<%= component_controller do %>` in order to properly initialize, otherwise the Reflex class won't get created.
286
306
 
287
307
  ## License
@@ -54,20 +54,25 @@ module ViewComponentReflex
54
54
  @key = key
55
55
  end
56
56
 
57
- def reflex_tag(reflex, name, content_or_options_with_block = {}, options = {}, escape = true, &block)
57
+ # Helper to use to create the proper reflex data attributes for an element
58
+ def reflex_data_attributes(reflex)
58
59
  action, method = reflex.to_s.split("->")
59
60
  if method.nil?
60
61
  method = action
61
62
  action = "click"
62
63
  end
63
- data_attributes = {
64
+
65
+ {
64
66
  reflex: "#{action}->#{self.class.name}##{method}",
65
67
  key: key
66
68
  }
69
+ end
70
+
71
+ def reflex_tag(reflex, name, content_or_options_with_block = {}, options = {}, escape = true, &block)
67
72
  if content_or_options_with_block.is_a?(Hash)
68
- merge_data_attributes(content_or_options_with_block, data_attributes)
73
+ merge_data_attributes(content_or_options_with_block, reflex_data_attributes(reflex))
69
74
  else
70
- merge_data_attributes(options, data_attributes)
75
+ merge_data_attributes(options, reflex_data_attributes(reflex))
71
76
  end
72
77
  content_tag(name, content_or_options_with_block, options, escape, &block)
73
78
  end
@@ -97,8 +102,8 @@ module ViewComponentReflex
97
102
  ViewComponentReflex::Engine.state_adapter.state(request, @key).each do |k, v|
98
103
  instance_value = instance_variable_get(k)
99
104
  if permit_parameter?(initial_state[k], instance_value)
100
- ViewComponentReflex::Engine.state_adapter.set_state(request, controller, "#{@key}_initial", { k => instance_value })
101
- ViewComponentReflex::Engine.state_adapter.set_state(request, controller, @key, { k => instance_value })
105
+ ViewComponentReflex::Engine.state_adapter.set_state(request, controller, "#{@key}_initial", {k => instance_value})
106
+ ViewComponentReflex::Engine.state_adapter.set_state(request, controller, @key, {k => instance_value})
102
107
  else
103
108
  instance_variable_set(k, v)
104
109
  end
@@ -1,3 +1,3 @@
1
1
  module ViewComponentReflex
2
- VERSION = '2.1.5'
2
+ VERSION = '2.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: view_component_reflex
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.5
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua LeBlanc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-16 00:00:00.000000000 Z
11
+ date: 2020-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails