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

Commit 47b1f21

Browse files
adamcreekroadzetachang
authored andcommitted
adds haml like data param typing, closes #99
# Conflicts: # lib/react/top_level.rb # spec/react/dsl_spec.rb
1 parent caacdec commit 47b1f21

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

lib/react/api.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ def self.convert_props(properties)
108108
props["className"] = value
109109
elsif ["style", "dangerously_set_inner_HTML"].include? key
110110
props[lower_camelize(key)] = value.to_n
111+
elsif React::HASH_ATTRIBUTES.include?(key) && value.is_a?(Hash)
112+
value.each { |k, v| props["#{key}-#{k.tr('_', '-')}"] = v.to_n }
111113
else
112114
props[React.html_attr?(lower_camelize(key)) ? lower_camelize(key) : key] = value
113115
end

lib/react/top_level.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ module React
3434
strokeOpacity strokeWidth textAnchor transform version
3535
viewBox x1 x2 x xlinkActuate xlinkArcrole xlinkHref xlinkRole
3636
xlinkShow xlinkTitle xlinkType xmlBase xmlLang xmlSpace y1 y2 y)
37+
HASH_ATTRIBUTES = %w(data aria)
38+
3739

3840
def self.html_tag?(name)
3941
tags = HTML_TAGS

spec/react/dsl_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,32 @@ def render
136136
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<div>Hello&nbsp;&nbsp;Goodby</div>')
137137
end
138138

139+
it 'should convert a hash param to hyphenated html attributes if in React::HASH_ATTRIBUTES' do
140+
stub_const 'Foo', Class.new
141+
Foo.class_eval do
142+
include React::Component
143+
def render
144+
div(data: { foo: :bar }, aria: { foo_bar: :foo })
145+
end
146+
end
147+
148+
expect(React.render_to_static_markup(React.create_element(Foo)))
149+
.to eq('<div data-foo="bar" aria-foo-bar="foo"></div>')
150+
end
151+
152+
it 'should not convert a hash param to hyphenated html attributes if not in React::HASH_ATTRIBUTES' do
153+
stub_const 'Foo', Class.new
154+
Foo.class_eval do
155+
include React::Component
156+
def render
157+
div(title: { bar: :foo })
158+
end
159+
end
160+
161+
expect(React.render_to_static_markup(React.create_element(Foo)))
162+
.to eq('<div title="{&quot;bar&quot;=&gt;&quot;foo&quot;}"></div>')
163+
end
164+
139165
it "will remove all elements passed as params from the rendering buffer" do
140166
stub_const 'X2', Class.new
141167
X2.class_eval do

0 commit comments

Comments
 (0)