view_component_reflex 0.4.0 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -0
- data/app/components/view_component_reflex/component.rb +18 -6
- data/lib/view_component_reflex/engine.rb +0 -13
- 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: f994d68e2d141a7ddb6f256d64fb55350e85011b0cbea0bcb9184fda24b35df6
|
4
|
+
data.tar.gz: bb4f268de2b510782c60ab164e76f79322cc478f8b100a7711742e50ee2f3f4c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a69bcd9c9b98a994d1ac00b81bfdc3993e6564d2ae3fc8bcd0559e07c663a2beefa7b3b6efff118279e88560954cb6cbd7413ea0ce7936463570ccaaaf0051b5
|
7
|
+
data.tar.gz: 02b12f819add7a693360fd211a5d591d723f1569f880cd781472e89187187743908c64056ec8a1b098838e007a2b9a2340a52631b8a9a5794469b3d8e23181a3
|
data/README.md
CHANGED
@@ -11,12 +11,15 @@ To add a reflex to your component, use the `reflex` method.
|
|
11
11
|
```ruby
|
12
12
|
reflex :my_cool_reflex do
|
13
13
|
# do stuff
|
14
|
+
refresh!
|
14
15
|
end
|
15
16
|
```
|
16
17
|
|
17
18
|
This will act as if you created a reflex with the method `my_cool_stuff`. To call this reflex, add `data-reflex="click->MyComponentReflex#my_cool_reflex"`, just like you're
|
18
19
|
using stimulus reflex.
|
19
20
|
|
21
|
+
#####note: A reflex will not automatically re-render the component upon its completion. A component will re-render whenever the `set_state` or `refresh!` method is called.
|
22
|
+
|
20
23
|
In addition to calling reflexes, there is a rudimentary state system. You can initialize component-local state with `initialize_state(obj)`, where `obj` is a hash.
|
21
24
|
|
22
25
|
You can access state with the `state` helper. See the code below for an example. Calling `set_state` will set the state,
|
@@ -12,9 +12,8 @@ module ViewComponentReflex
|
|
12
12
|
ViewComponentReflex::Engine.state_adapter.state(request, element.dataset[:key])
|
13
13
|
end
|
14
14
|
|
15
|
-
def
|
16
|
-
|
17
|
-
@channel.render_page_and_broadcast_morph(self, nil, {
|
15
|
+
def refresh!(primary_selector = "[data-controller=\"#{stimulus_controller}\"]", *selectors)
|
16
|
+
@channel.send :render_page_and_broadcast_morph, self, [primary_selector, *selectors], {
|
18
17
|
dataset: element.dataset.to_h,
|
19
18
|
args: [],
|
20
19
|
attrs: element.attributes.to_h,
|
@@ -22,7 +21,16 @@ module ViewComponentReflex
|
|
22
21
|
target: "#{self.class.name}##{method_name}",
|
23
22
|
url: request.url,
|
24
23
|
permanentAttributeName: "data-reflex-permanent"
|
25
|
-
}
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
def refresh_all!
|
28
|
+
refresh!('body')
|
29
|
+
end
|
30
|
+
|
31
|
+
def set_state(new_state = {}, primary_selector = nil, *selectors)
|
32
|
+
ViewComponentReflex::Engine.state_adapter.set_state(self, element.dataset[:key], new_state)
|
33
|
+
refresh!(primary_selector, *selectors)
|
26
34
|
end
|
27
35
|
|
28
36
|
before_reflex do |reflex, *args|
|
@@ -51,6 +59,10 @@ module ViewComponentReflex
|
|
51
59
|
@state = obj
|
52
60
|
end
|
53
61
|
|
62
|
+
def stimulus_reflex?
|
63
|
+
helpers.controller.instance_variable_get(:@stimulus_reflex)
|
64
|
+
end
|
65
|
+
|
54
66
|
# key is required if you're using state
|
55
67
|
# We can't initialize the session state in the initial method
|
56
68
|
# because it doesn't have a view_context yet
|
@@ -59,11 +71,11 @@ module ViewComponentReflex
|
|
59
71
|
@key ||= caller.find { |p| p.include? ".html.erb" }&.hash.to_s
|
60
72
|
|
61
73
|
# initialize session state
|
62
|
-
if session[@key].nil?
|
74
|
+
if !stimulus_reflex? || session[@key].nil?
|
63
75
|
ViewComponentReflex::Engine.state_adapter.store_state(request, @key, @state)
|
64
76
|
ViewComponentReflex::Engine.state_adapter.store_state(request, "#{@key}_initial", @state)
|
65
77
|
else
|
66
|
-
ViewComponentReflex::Engine.state_adapter.reconcile_state(request, @key, @state)
|
78
|
+
# ViewComponentReflex::Engine.state_adapter.reconcile_state(request, @key, @state)
|
67
79
|
end
|
68
80
|
@key
|
69
81
|
end
|
@@ -9,18 +9,5 @@ module ViewComponentReflex
|
|
9
9
|
def self.configure
|
10
10
|
yield self if block_given?
|
11
11
|
end
|
12
|
-
|
13
|
-
config.to_prepare do
|
14
|
-
class StimulusReflex::Channel < ActionCable::Channel::Base
|
15
|
-
def render_page_and_broadcast_morph(reflex, selectors, data = {})
|
16
|
-
html = render_page(reflex)
|
17
|
-
if reflex.respond_to? :stimulus_controller
|
18
|
-
selectors = ["[data-controller=\"#{reflex.stimulus_controller}\"]"]
|
19
|
-
end
|
20
|
-
broadcast_morphs selectors, data, html if html.present?
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
12
|
end
|
26
13
|
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: 0.
|
4
|
+
version: 0.6.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-06-
|
11
|
+
date: 2020-06-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|