view_component_reflex 2.1.3 → 2.2.2
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 +4 -4
- data/README.md +25 -5
- data/app/components/view_component_reflex/component.rb +18 -13
- data/lib/view_component_reflex.rb +2 -2
- data/lib/view_component_reflex/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 884879671f11072c36d18205c1bb452c8b0ff9e2e50c6e57b70e1bfb602f1b87
|
4
|
+
data.tar.gz: dd3ac067aaa30fa45785b6abd1b609b7a09fadf9c11ca272d829271675750fb9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6740d002a2fae276c4ff60433485461cb43a90ac4a6fdb2431fa757b326fc2f882f86b662d7b23ba735f65ca89d87a047ebdb5f7874f3db08826fe3cb880d39c
|
7
|
+
data.tar.gz: fc558e67327441ea6c1f813f7d005e57ce6b127221e4ab34835c78e1a7d6a0b2fd25e406de405ae615a6d204d691e8a94d669eb28df7bb64192e770bd88f06df
|
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 `->`
|
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.
|
@@ -136,7 +156,7 @@ end
|
|
136
156
|
Returns the unique selector for this component. Useful to pass to `refresh!` when refreshing custom elements.
|
137
157
|
|
138
158
|
### prevent_refresh!
|
139
|
-
By default, VCR will re-render your component after it executes your method. `
|
159
|
+
By default, VCR will re-render your component after it executes your method. `prevent_refresh!` prevents this from happening.
|
140
160
|
|
141
161
|
```ruby
|
142
162
|
def my_method
|
@@ -219,7 +239,7 @@ ViewComponentReflex uses session for its state by default. To change this, add
|
|
219
239
|
an initializer to `config/initializers/view_component_reflex.rb`.
|
220
240
|
|
221
241
|
```ruby
|
222
|
-
ViewComponentReflex.configure do |config|
|
242
|
+
ViewComponentReflex::Engine.configure do |config|
|
223
243
|
config.state_adapter = YourAdapter
|
224
244
|
end
|
225
245
|
```
|
@@ -279,9 +299,9 @@ Or install it yourself as:
|
|
279
299
|
$ gem install view_component_reflex
|
280
300
|
```
|
281
301
|
|
282
|
-
|
302
|
+
# Common problems
|
283
303
|
|
284
|
-
|
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
|
@@ -2,10 +2,10 @@ module ViewComponentReflex
|
|
2
2
|
class Component < ViewComponent::Base
|
3
3
|
class << self
|
4
4
|
def init_stimulus_reflex
|
5
|
-
if name.include? "::"
|
6
|
-
|
5
|
+
@stimulus_reflex ||= if name.include? "::"
|
6
|
+
module_parent.const_set(name.split("::").last + "Reflex", Class.new(ViewComponentReflex::Reflex))
|
7
7
|
else
|
8
|
-
|
8
|
+
Object.const_set(name + "Reflex", Class.new(ViewComponentReflex::Reflex))
|
9
9
|
end
|
10
10
|
@stimulus_reflex.component_class = self
|
11
11
|
end
|
@@ -24,11 +24,11 @@ module ViewComponentReflex
|
|
24
24
|
init_key
|
25
25
|
|
26
26
|
tag = :div
|
27
|
-
if opts_or_tag.is_a? Hash
|
28
|
-
|
27
|
+
options = if opts_or_tag.is_a? Hash
|
28
|
+
opts_or_tag
|
29
29
|
else
|
30
30
|
tag = opts_or_tag
|
31
|
-
|
31
|
+
opts
|
32
32
|
end
|
33
33
|
options[:data] = {
|
34
34
|
controller: self.class.stimulus_controller,
|
@@ -49,25 +49,30 @@ module ViewComponentReflex
|
|
49
49
|
def init_key
|
50
50
|
# we want the erb file that renders the component. `caller` gives the file name,
|
51
51
|
# and line number, which should be unique. We hash it to make it a nice number
|
52
|
-
key = caller.select { |p| p.
|
52
|
+
key = caller.select { |p| p.match? /.\.html\.(haml|erb|slim)/ }[1]&.hash.to_s
|
53
53
|
key += collection_key.to_s if collection_key
|
54
54
|
@key = key
|
55
55
|
end
|
56
56
|
|
57
|
-
|
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
|
-
|
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,
|
73
|
+
merge_data_attributes(content_or_options_with_block, reflex_data_attributes(reflex))
|
69
74
|
else
|
70
|
-
merge_data_attributes(options,
|
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", {
|
101
|
-
ViewComponentReflex::Engine.state_adapter.set_state(request, controller, @key, {
|
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,8 +1,8 @@
|
|
1
|
+
require "stimulus_reflex"
|
1
2
|
require "view_component_reflex/state_adapter/session"
|
2
3
|
require "view_component_reflex/state_adapter/memory"
|
3
|
-
require "view_component_reflex/engine"
|
4
|
-
require "stimulus_reflex"
|
5
4
|
require "view_component_reflex/reflex"
|
5
|
+
require "view_component_reflex/engine"
|
6
6
|
|
7
7
|
module ViewComponentReflex
|
8
8
|
# Your code goes here...
|
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.
|
4
|
+
version: 2.2.2
|
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-
|
11
|
+
date: 2020-09-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -34,16 +34,16 @@ dependencies:
|
|
34
34
|
name: stimulus_reflex
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- -
|
37
|
+
- - '='
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version:
|
39
|
+
version: 3.3.0.pre2
|
40
40
|
type: :runtime
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
|
-
- -
|
44
|
+
- - '='
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version:
|
46
|
+
version: 3.3.0.pre2
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: view_component
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|