File tree 4 files changed +42
-13
lines changed
4 files changed +42
-13
lines changed Original file line number Diff line number Diff line change 14
14
// `data-react-class` DOM elements
15
15
findDOMNodes : function ( searchSelector ) {
16
16
// we will use fully qualified paths as we do not bind the callbacks
17
- var selector ;
18
- if ( typeof searchSelector === 'undefined' ) {
19
- var selector = '[' + window . ReactRailsUJS . CLASS_NAME_ATTR + ']' ;
20
- } else {
21
- var selector = searchSelector + ' [' + window . ReactRailsUJS . CLASS_NAME_ATTR + ']' ;
17
+ var selector , parent ;
18
+
19
+ switch ( typeof searchSelector ) {
20
+ case 'undefined' :
21
+ selector = '[' + window . ReactRailsUJS . CLASS_NAME_ATTR + ']' ;
22
+ parent = document ;
23
+ break ;
24
+ case 'object' :
25
+ selector = '[' + window . ReactRailsUJS . CLASS_NAME_ATTR + ']' ;
26
+ parent = searchSelector ;
27
+ break ;
28
+ case 'string' :
29
+ selector = searchSelector + ' [' + window . ReactRailsUJS . CLASS_NAME_ATTR + ']' ;
30
+ parent = document ;
31
+ break
32
+ default :
33
+ break ;
22
34
}
23
35
24
36
if ( $ ) {
25
- return $ ( selector ) ;
37
+ return $ ( selector , parent ) ;
26
38
} else {
27
- return document . querySelectorAll ( selector ) ;
39
+ return parent . querySelectorAll ( selector ) ;
28
40
}
29
41
} ,
30
42
Original file line number Diff line number Diff line change 2
2
< li > <%= link_to 'Alice' , page_path ( :id => 0 ) %> </ li >
3
3
< li > <%= link_to 'Bob' , page_path ( :id => 1 ) %> </ li >
4
4
</ ul >
5
+
5
6
< div id ='test-component '>
6
7
<%= react_component 'HelloMessage' , :name => @name %>
7
8
</ div >
8
- < a href ='# ' onClick ="ReactRailsUJS.unmountComponents('#test-component') "> Unmount at #test-component</ a >
9
- < a href ='# ' onClick ="ReactRailsUJS.mountComponents('#test-component') "> Mount at #test-component</ a >
9
+
10
+ < a href ='# ' onClick ="ReactRailsUJS.unmountComponents('#test-component') "> Unmount at selector #test-component</ a >
11
+ < a href ='# ' onClick ="ReactRailsUJS.mountComponents('#test-component') "> Mount at selector #test-component</ a >
12
+
13
+ < a href ='# ' onClick ="ReactRailsUJS.unmountComponents(document.querySelector('#test-component')) "> Unmount at node #test-component</ a >
14
+ < a href ='# ' onClick ="ReactRailsUJS.mountComponents(document.querySelector('#test-component')) "> Mount at node #test-component</ a >
Original file line number Diff line number Diff line change @@ -34,7 +34,8 @@ def copy_directory(dir)
34
34
assert_application_file_modified
35
35
end
36
36
37
- test "modifies `application.js` it's empty" do
37
+ test "modifies `application.js` if it's empty" do
38
+ FileUtils . mkdir_p destination_root + '/app/assets/javascripts/'
38
39
File . write ( destination_root + '/app/assets/javascripts/application.js' , '' )
39
40
40
41
run_generator
Original file line number Diff line number Diff line change @@ -87,14 +87,25 @@ class ViewHelperTest < ActionDispatch::IntegrationTest
87
87
assert page . has_content? ( 'Hello Bob' )
88
88
end
89
89
90
- test 'react_ujs can unount at node ' do
90
+ test 'react_ujs can unmount/mount using a selector reference ' do
91
91
visit '/pages/1'
92
92
assert page . has_content? ( 'Hello Bob' )
93
93
94
- page . click_link ' Unmount at #test-component'
94
+ page . click_link " Unmount at selector #test-component"
95
95
assert page . has_no_content? ( 'Hello Bob' )
96
96
97
- page . click_link 'Mount at #test-component'
97
+ page . click_link "Mount at selector #test-component"
98
+ assert page . has_content? ( 'Hello Bob' )
99
+ end
100
+
101
+ test 'react_ujs can unmount/mount using a dom node context' do
102
+ visit '/pages/1'
103
+ assert page . has_content? ( 'Hello Bob' )
104
+
105
+ page . click_link "Unmount at node #test-component"
106
+ assert page . has_no_content? ( 'Hello Bob' )
107
+
108
+ page . click_link "Mount at node #test-component"
98
109
assert page . has_content? ( 'Hello Bob' )
99
110
end
100
111
You can’t perform that action at this time.
0 commit comments