Skip to content

Commit e33954c

Browse files
author
Robert Mosolgo
committed
Merge pull request #358 from ximus/support-dom-node-context
Allow to pass DOM object as parent selector (second PR)
2 parents d7ecd8b + d124531 commit e33954c

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

lib/assets/javascripts/react_ujs.js.erb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,29 @@
1414
// `data-react-class` DOM elements
1515
findDOMNodes: function(searchSelector) {
1616
// 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;
2234
}
2335

2436
if ($) {
25-
return $(selector);
37+
return $(selector, parent);
2638
} else {
27-
return document.querySelectorAll(selector);
39+
return parent.querySelectorAll(selector);
2840
}
2941
},
3042

test/dummy/app/views/pages/show.html.erb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22
<li><%= link_to 'Alice', page_path(:id => 0) %></li>
33
<li><%= link_to 'Bob', page_path(:id => 1) %></li>
44
</ul>
5+
56
<div id='test-component'>
67
<%= react_component 'HelloMessage', :name => @name %>
78
</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>

test/generators/install_generator_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ def copy_directory(dir)
3434
assert_application_file_modified
3535
end
3636

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/'
3839
File.write(destination_root + '/app/assets/javascripts/application.js', '')
3940

4041
run_generator

test/react/rails/view_helper_test.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,25 @@ class ViewHelperTest < ActionDispatch::IntegrationTest
8787
assert page.has_content?('Hello Bob')
8888
end
8989

90-
test 'react_ujs can unount at node' do
90+
test 'react_ujs can unmount/mount using a selector reference' do
9191
visit '/pages/1'
9292
assert page.has_content?('Hello Bob')
9393

94-
page.click_link 'Unmount at #test-component'
94+
page.click_link "Unmount at selector #test-component"
9595
assert page.has_no_content?('Hello Bob')
9696

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"
98109
assert page.has_content?('Hello Bob')
99110
end
100111

0 commit comments

Comments
 (0)