view_component_reflex 1.0.1 → 1.1.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 +19 -0
- data/app/components/view_component_reflex/component.rb +14 -5
- data/lib/view_component_reflex/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 42797141d9ed8ecfb013f8f68c9e073b9ee6db4fe4eb04f1c7484babbd2c4e06
|
4
|
+
data.tar.gz: 9dfb73b083e7e1ac3417a676f37962c1861435310e0f04934d16cea5824f6eb3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4505fedd6b66cf2e5b31fe3cdeb8ec3b337a0c6b8de79082926d637c39dd74770b562280ae6f61bc79f434b2184e993c706dcd7fac088d436b185e56228f7d28
|
7
|
+
data.tar.gz: ed2b650393469281ecd997a7589f6e9999a1aeda25e624193c54c61c1d7e7b78b012718945c409c5db8db86588a7ea64e1be547a4d351e92b377329eef1d7e18
|
data/README.md
CHANGED
@@ -34,6 +34,25 @@ end
|
|
34
34
|
<% end %>
|
35
35
|
```
|
36
36
|
|
37
|
+
## Collections
|
38
|
+
|
39
|
+
In order to reconcile state to components in collections, you can specify a `collection_key` method that returns some
|
40
|
+
value unique to that component.
|
41
|
+
|
42
|
+
```
|
43
|
+
class TodoComponent < ViewComponentReflex::Component
|
44
|
+
def initialize(todo:)
|
45
|
+
@todo = todo
|
46
|
+
end
|
47
|
+
|
48
|
+
def collection_key
|
49
|
+
@todo.id
|
50
|
+
end
|
51
|
+
end
|
52
|
+
#
|
53
|
+
<%= render(TodoComponent.with_collection(Todo.all)) %>
|
54
|
+
```
|
55
|
+
|
37
56
|
## Custom State Adapters
|
38
57
|
|
39
58
|
ViewComponentReflex uses session for its state by default. To change this, add
|
@@ -4,7 +4,7 @@ module ViewComponentReflex
|
|
4
4
|
def init_stimulus_reflex
|
5
5
|
klass = self
|
6
6
|
@stimulus_reflex ||= Object.const_set(name + "Reflex", Class.new(StimulusReflex::Reflex) {
|
7
|
-
def refresh!(primary_selector = "[data-controller
|
7
|
+
def refresh!(primary_selector = "[data-controller~=\"#{stimulus_controller}\"][data-key=\"#{element.dataset[:key]}\"]", *selectors)
|
8
8
|
save_state
|
9
9
|
@channel.send :render_page_and_broadcast_morph, self, [primary_selector, *selectors], {
|
10
10
|
"dataset" => element.dataset.to_h,
|
@@ -98,9 +98,18 @@ module ViewComponentReflex
|
|
98
98
|
helpers.controller.instance_variable_get(:@stimulus_reflex)
|
99
99
|
end
|
100
100
|
|
101
|
-
def component_controller(&blk)
|
102
|
-
|
103
|
-
|
101
|
+
def component_controller(opts = {}, &blk)
|
102
|
+
self.class.init_stimulus_reflex
|
103
|
+
opts[:data] = {
|
104
|
+
controller: self.class.stimulus_controller,
|
105
|
+
key: key,
|
106
|
+
**(opts[:data] || {})
|
107
|
+
}
|
108
|
+
content_tag :div, capture(&blk), opts
|
109
|
+
end
|
110
|
+
|
111
|
+
def collection_key
|
112
|
+
nil
|
104
113
|
end
|
105
114
|
|
106
115
|
# key is required if you're using state
|
@@ -108,10 +117,10 @@ module ViewComponentReflex
|
|
108
117
|
# because it doesn't have a view_context yet
|
109
118
|
# This is the next best place to do it
|
110
119
|
def key
|
111
|
-
self.class.init_stimulus_reflex
|
112
120
|
# we want the erb file that renders the component. `caller` gives the file name,
|
113
121
|
# and line number, which should be unique. We hash it to make it a nice number
|
114
122
|
key = caller.select { |p| p.include? ".html.erb" }[1]&.hash.to_s
|
123
|
+
key += collection_key.to_s if collection_key
|
115
124
|
if @key.nil? || @key.empty?
|
116
125
|
@key = key
|
117
126
|
end
|