Replies: 2 comments 5 replies
-
You could create a
|
Beta Was this translation helpful? Give feedback.
1 reply
-
I've created this helper. I don't know if its 100% bullet proof, tho. Any thoughts @jaredcwhite? module ComponentsHelper
# Shorthand for rendering view_components
#
# Usage:
#
# <%= component :section, title: 'Greeting', classes: 'my--1' do %>
# <p class="Txt--center">Hello world</p>
# <% end %>
def component(name, *args, &block)
component_class = "#{name.to_s.camelcase}::Component".constantize
render_component(component_class.new(*args), &block)
end
ruby2_keywords :component
end <%= component :section, title: 'Greeting', classes: 'my--1' do %> should equal <%= render_component Section::Component.new(title: 'Greeting', classes: 'my--1') do %> |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Overall we love what
view_component
provides for us, but one thing we're running up against is how much visual noise rendering a component creates in our views:= render(ButtonComponent.new(:primary, :inverse, type: :submit))
What we'll probably do is drop the
Component
from the name, at the risk of naming clashes if, say, there were a model with the same name. But it also feels a little weird to me that we explicitly need to call.new
. Maybe more in nitpick territory, but I think what I'd love to have is something like:= render(Button, :primary, :inverse, type: :submit)
Or if we want to keep the component named
ButtonComponent
to avoid naming clashes, maybe we could pass a string instead and infer the name:= render('button', :primary, :inverse, type: :submit)
We might end up monkey patching
render
on our end to support one of these alternatives, but I was wondering if anybody else has come up with something to reduce boilerplate. Or if there is any interest in having something like this built intoview_component
.Beta Was this translation helpful? Give feedback.
All reactions