view_component_reflex 2.1.5 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -3
- data/app/components/view_component_reflex/component.rb +11 -6
- data/lib/view_component_reflex/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ac0e4b7fc45ba1963f6d260f66463a00a37fccfb6db10855f608beb8f54126e
|
4
|
+
data.tar.gz: eaa30fcda3a41ffc51f651398d490a51b89d1eb06fdf81619e2a9ee0535b62f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 `->`
|
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
|
-
|
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
|
@@ -54,20 +54,25 @@ module ViewComponentReflex
|
|
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
|
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.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-
|
11
|
+
date: 2020-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|