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
Reusable slots #1038
Comments
|
It seems that there'd also be some changes to make in the Base component for reuse to work when blocks are passed to the slots. The <%= render LoopingComponent.new (records) do |component| %>
<% component.slot do |slot| %>
<%= slot.record.date.strftime('%d %b %Y') %>
<% end %>
<% end %>I've noticed that |
|
I had a reply drafted for this and lost it, so I might end up skipping necessary details here, apologies. So it looks to me like the ask is to be able to change the What I would recommend doing instead is either:
<% data.each do |record| %>
<% c.slot do %>
<% Do things with the content here %>
<% end %>
<% end %>or:
Using a lambda slot with Hope this helps! |
|
Thanks for the reply. I think I need to add some clarifications as the two options don't really help solve what made me look for lifting the caching of the content:
The concrete use case I'm working on is building a data table component. In terms of slots (and with the described caching mechanism lifted), it uses two class DataTableComponent < ViewComponent::Base
renders_many :headings, HeadingComponent # Not repeated
renders_many :cells, CellComponent # <- That's the repeated one
def initialize(records: [])
end
def column(*args, **kwargs, &block)
# Arguments passed as is for brevity, but some triaging would happen here
# to pick what goes to which part of the column
heading(*args, **kwargs)
cell(*args, **kwargs, &block)
end
endThe table encapsulates the looping as its doesn't naively iterate over the records. As examples, it also needs to:
When used the aim is to provide an API similar to: <%= render DataTableComponent.new(selectable: true) do |table| %>
<% table.column :name, accessible_name: true %>
<% table.column :email, cell_attributes: {class: 'text-muted'} %>
<% table.column :edit do |cell| %>
<%= link_to "Edit", record_edit_path(table.current_record), 'aria-labelledby': "#{cell.heading_id} #{table.accessible_name_cell_id}" %>
<% end %>
<% end %>By reusing the CellComponent and re-running the block, the content of the cell gets the right references for each record of the table, based on the internal state of the DataTable component. I'm aware that this could be achieved with something like: class DataTableComponent < ViewComponent::Base
renders_many :headings, HeadingComponent # Not repeated
def initialize(records: [])
@cells = []
end
def cell(*args, **kwargs, &block)
@cells.push([args, kwargs, block])
end
def column(*args, **kwargs, &block)
heading(*args, **kwargs)
cell(*args, **kwargs, &block)
end
end<% @records.each do |record| %>
<% @cells.each do |cell| %>
<%= render CellComponent.new(*cell[0],**cell[1]), &cell[2] %>
<% end %>
<% end %>However, this makes things look like the table has a Hope this makes what I'm trying to achieve clearer |
|
Hey Romaric, sorry for the radio silence on my part - I'm hoping to sit down and have a proper look at this at some point soon. Hopefully one of the others can tag in to help in the meantime. |
|
No worries, it's not blocking for me. It's just something I think could be handy to have out of the box in the library as it really helps encapsulate little front-end helper logic on components that iterate over data. |
|
Would you be open to a PR adding a |
|
@romaricpascal at this point I would not accept a patch for adding @boardfish do you have time to look into this more, or should I take it on? |
|
I wanted to take the time to get my head around it but I haven't been able to make that time for a while. It might be best for you to pick it up @joelhawksley. Thanks! |
|
No worries. In the meantime, I did find something that works without reaching to instance variables for configuring a slot once, but rendering it repeatedly. It relies on taking advantage that missing methods on the Slot will get forwarded to the component it holds (but only if it's a # in component
renders_many :items, -> (*args, **kwargs, &block) {
ComponentTemplate.new(TagComponent, *args, **kwargs, &block)
}<% records.each do |record| %>
<%= items.each do |item| %>
<% item.render_within(self, record: record) %>
<% end %>
<% end %>The # Needs to be a ViewComponent::Base so that the slot forwards the missing_method
class ComponentTemplate < ViewComponent::Base
def initialize(component_class, *args, **attributes, &block)
@component_class = component_class
@attributes = attributes
@block = block
end
def render_within(parent_component, **attributes)
@parent_component = parent_component
@parent_component.render(@component_class.new(merge_attributes(attributes, @attributes)), &@block)
end
def merge_attributes(*attributes_list, **attributes)
@parent_component.helpers.merge_attributes(*attributes_list, **attributes)
end
def call
raise "ComponentTemplate is meant to be used through `#render_within`"
end
endBesides accessing the That's a bit of gymnatics but I guess less risky than continuing to reach for these instance variables. It'd still be great to have an API where we can just do: class Table
renders_many :cells, reusable: true
def initialize(records: nil)
@records = records
end
endAnd then <table>
<% @records.each do |record| %>
<tr>
<%= cells.each do |cell| %>
<%#
`with_` could be a marker for slots to forward the method
but return itself so `to_s` could be invoked
%>
<% cell.with_attributes(record: record) %>
<% end %>
</tr>
<% end %>
</table>It's not just useful for tables, things like a TabsComponent displaying tabs for each records but providing help with all the ids and such required for accessibility would also benefit from being able to reuse slots. I guess anytime you need to iterate on records, but also on slots. |


Feature request
Allow a component to render the same slot multiple times with different outputs. At the moment, each subsequent output of a given slot produces the same output as the first.
This gets in the way when trying to create a component looping over some records internally, rendering the same slot for each. But offering a unique entry point for configuring the rendering of that slot.
Something like a data table would be a prime use case for it.
Looking at the code, it seems the main blocker for it is that Line 29 in SlotV2
This can be worked around with:
However, how safe an API is it to reach for that
@contentinstance variable?It'd be great to have an API to handle that, either through:
clear_contentmethod onSlotV2that exectutes the workaroundSlotV2's constructor to not cache the content and a way forrenders_one/renders_manyto pass that through? Or maybe to pass the class of the slot to use, allowing users to extend if necessary?There's also the option of disabling the caching altogether, but I guess if it's there it's because it must bring some performance gains in some situations.
The text was updated successfully, but these errors were encountered: