Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit 85cb5b2

Browse files
committed
added merging of data and aria hashes in #114, removed #269 for performance reasons
1 parent 212ea1c commit 85cb5b2

File tree

5 files changed

+32
-52
lines changed

5 files changed

+32
-52
lines changed

lib/react/api.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ def self.convert_props(args)
203203
raise "The style param must be a Hash"
204204
end
205205
properties['style'] = (properties['style'] || {}).merge(value)
206+
elsif React::HASH_ATTRIBUTES.include?(key) && value.is_a?(Hash)
207+
properties[key] = (properties[key] || {}).merge(value)
206208
else
207209
properties[key] = value
208210
end
@@ -234,7 +236,7 @@ def self.convert_props(args)
234236
}
235237
}
236238
elsif React::HASH_ATTRIBUTES.include?(key) && value.is_a?(Hash)
237-
value.each { |k, v| props["#{key}-#{k.tr('_', '-')}"] = v.to_n }
239+
value.each { |k, v| props["#{key}-#{k.gsub(/__|_/, '__' => '_', '_' => '-')}"] = v.to_n }
238240
else
239241
props[React.html_attr?(lower_camelize(key)) ? lower_camelize(key) : key] = value
240242
end

lib/react/component/tags.rb

+13-22
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ def present(component, *params, &children)
2020
React::RenderingContext.render(component, *params, &children)
2121
end
2222

23-
# define each predefined tag (downcase) as an instance method (deprecated) and as a component (upcase)
23+
# define each predefined tag (upcase) as an instance method and a constant
24+
# deprecated: define each predefined tag (downcase) as the alias of the instance method
25+
2426
HTML_TAGS.each do |tag|
2527

26-
# deprecated - remove
28+
define_method(tag.upcase) do |*params, &children|
29+
React::RenderingContext.render(tag, *params, &children)
30+
end
31+
32+
const_set tag.upcase, tag
33+
34+
# deprecated: remove
2735
if tag == 'p'
2836
define_method(tag) do |*params, &children|
2937
if children || params.count == 0 || (params.count == 1 && params.first.is_a?(Hash))
@@ -33,29 +41,12 @@ def present(component, *params, &children)
3341
end
3442
end
3543
else
36-
define_method(tag) do |*params, &children|
37-
React::RenderingContext.render(tag, *params, &children)
38-
end
39-
end
40-
41-
# new style: allows custom hooks to be added and/or the render method to
42-
# be modified. i.e. see how hyper-mesh deals with defaultValues in input tags
43-
44-
klass = Class.new(Hyperloop::Component) do
45-
# its complicated but the automatic inclusion of the Mixin is setup after all
46-
# the files are loaded, so at this point we have to manually load it.
47-
include Hyperloop::Component::Mixin
48-
collect_other_params_as :opts
49-
# we simply pass along all the params and children with the tag string name
50-
render { React::RenderingContext.render(tag, params.opts, &children) }
51-
52-
# after_error do |error, info|
53-
# raise error
54-
# end
44+
alias_method tag, tag.upcase
5545
end
56-
const_set(tag.upcase, klass)
46+
# end of deprecated code
5747
end
5848

49+
# this is used for haml style (i.e. DIV.foo.bar) class tags which is deprecated
5950
def self.html_tag_class_for(tag)
6051
downcased_tag = tag.downcase
6152
if tag =~ /[A-Z]+/ && HTML_TAGS.include?(downcased_tag)

spec/react/builtin_tags_spec.rb

+6-21
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
require 'spec_helper'
22

3-
describe 'Builtin Tags', js: true do
4-
it "built in tags render method can be redefined" do
3+
describe 'redefining builtin tags', js: true do
4+
it "built in tags can be redefined" do
55
mount 'Foo' do
6+
React::Component::Tags.remove_method :DIV
7+
React::Component::Tags.send(:remove_const, :DIV)
68
class React::Component::Tags::DIV < Hyperloop::Component
9+
others :opts
710
render do
811
params.opts['data-render-time'] = Time.now
9-
present "div", params.opts, &children
12+
present :div, params.opts, data: {render_time: Time.now}, &children
1013
end
1114
end
1215
class Foo < Hyperloop::Component
@@ -17,22 +20,4 @@ class Foo < Hyperloop::Component
1720
end
1821
expect(Time.parse(find('#tp')['data-render-time'])).to be <= Time.now
1922
end
20-
21-
it "built in tags other callbacks can be added" do
22-
mount 'Foo' do
23-
class React::Component::Tags::DIV < Hyperloop::Component
24-
def add_time
25-
params.opts['data-render-time'] = Time.now
26-
end
27-
before_mount :add_time
28-
before_update :add_time
29-
end
30-
class Foo < Hyperloop::Component
31-
render(DIV, id: :tp) do
32-
"hello"
33-
end
34-
end
35-
end
36-
expect(Time.parse(find('#tp')['data-render-time'])).to be <= Time.now
37-
end
3823
end

spec/react/component_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def render
132132
end
133133
end
134134
expect_evaluate_ruby('Foo.get_error').to eq('ErrorFoo Error')
135-
expect_evaluate_ruby('Foo.get_info').to eq("\n in ErrorFoo\n in div\n in React::Component::Tags::DIV\n in Foo\n in React::TopLevelRailsComponent")
135+
expect_evaluate_ruby('Foo.get_info').to eq("\n in ErrorFoo\n in div\n in Foo\n in React::TopLevelRailsComponent")
136136
end
137137
end
138138

spec/react/param_declaration_spec.rb

+9-7
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,14 @@ def render
186186
it 'allows passing and merging complex arguments to params' do
187187
mount 'Tester' do
188188
class TakesParams < Hyperloop::Component
189-
param :flag
190-
param :a
191-
param :b
192-
param :c
193-
param :d
189+
param :flag
190+
param :a
191+
param :b
192+
param :c
193+
param :d
194194
others :opts
195195
render do
196-
DIV(params.opts, id: :tp, class: "another-class", style: {marginLeft: 12}) do
196+
DIV(params.opts, id: :tp, class: "another-class", style: {marginLeft: 12}, data: {foo: :hi}) do
197197
"flag: #{params.flag}, a: #{params.a}, b: #{params.b}, c: #{params.c}, d: #{params.d}"
198198
end
199199
end
@@ -202,7 +202,7 @@ class Tester < Hyperloop::Component
202202
render do
203203
TakesParams(
204204
:flag,
205-
{a: 1, b: 2, class: [:x, :y], className: 'foo', class_name: 'bar baz', style: {marginRight: 12}},
205+
{a: 1, b: 2, class: [:x, :y], className: 'foo', class_name: 'bar baz', style: {marginRight: 12}, data: {bar: :there}},
206206
c: 3, d: 4
207207
)
208208
end
@@ -212,6 +212,8 @@ class Tester < Hyperloop::Component
212212
expect(tp[:class].split).to contain_exactly("x", "y", "foo", "bar", "baz", "another-class")
213213
expect(tp[:style]).to match('margin-right: 12px')
214214
expect(tp[:style]).to match('margin-left: 12px')
215+
expect(tp['data-foo']).to eq("hi")
216+
expect(tp['data-bar']).to eq("there")
215217
expect(tp).to have_content('flag: true, a: 1, b: 2, c: 3, d: 4')
216218
end
217219

0 commit comments

Comments
 (0)