Skip to content

Add ability to render custom attributes from the controller #384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

kaiwood
Copy link
Contributor

@kaiwood kaiwood commented Oct 27, 2015

Currently, all custom attributes are sliced from options when using the controller renderer, instead of forwarding it to the view helper, making it impossible to set a custom class name or id.

This patch changes the behaviour to act more like render_component.

@rmosolgo
Copy link
Member

Very nice, I definitely agree we should allow more options to be passed through the controller renderer!

But right now it's pushing through a bunch of ActionController::Base#render options, too:

<span 
  class=\"custom-class\" 
  data-react-class=\"TodoList\" 
  data-react-props=\"{&quot;todos&quot;:[&quot;Render this inline&quot;]}\" 
  id=\"custom-id\" 
  layout=\"#&lt;Proc:0x000000038db4b0@/home/travis/.rvm/gems/ruby-2.2.0/gems/actionpack-3.2.22/lib/abstract_controller/layouts.rb:383&gt;\" 
  prefixes=\"server application\" 
  props=\"{:todos=&gt;[&quot;Render this inline&quot;]}\" 
  template=\"inline_component\"
>

(I got this from the Travis CI run)

What do you think about just adding some more options to the whitelist?

@kaiwood
Copy link
Contributor Author

kaiwood commented Oct 27, 2015

Extending the whitelist was my first thought, as my own intention was to have access to the class and id attribute for styling.

But shouldn't at least data-* or aria-* attributes whitelisted too? Might not be to far fetched that someone needs access to those, e.g. for some kind of plugin or accessibility thingy...

I'll take a look at it, didn't mean to break Travis anyway. :)

@rmosolgo
Copy link
Member

That's a good point, I wonder if just whitelisting :data, :aria would do the trick, since you can often pass those like data: { remote: true }

Kai Wood added 2 commits October 28, 2015 11:56
 Currently, all custom attributes are sliced from options when using the controller renderer, instead of forwarding it to the view helper, making it impossible to set a custom class name or id.

 This patch changes the behaviour to allow class, id, custom data-* and aria-* attributes.
@kaiwood kaiwood force-pushed the custom_attributes_in_component_renderer branch from c1eff92 to a793672 Compare October 28, 2015 11:48
@kaiwood
Copy link
Contributor Author

kaiwood commented Oct 28, 2015

Ok, i restored the whitelisting and added class, id, data and aria.

Stuff like data: {remote: true} works as expected. The thing with aria-* is, that the nested syntax was added in rails/rails@a794fd2 and is only supported in Rails 4.2 upwards…

We would need to add all 35 aria-attributes from the spec to the whitelist for the versions prior, looks like a little bit over the top to me. I think we can get away with this in the current form :)

@rmosolgo
Copy link
Member

🎉 Looks great! Down the road, if someone needs more attributes than this, we could extract a REACT_COMPONENT_OPTIONS array which people could mutate as they see fit.

Thanks for this improvement!

rmosolgo pushed a commit that referenced this pull request Oct 29, 2015
…enderer

Add ability to render custom attributes from the controller
@rmosolgo rmosolgo merged commit 88c6b64 into reactjs:master Oct 29, 2015
@kaiwood kaiwood deleted the custom_attributes_in_component_renderer branch October 30, 2015 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants