Replies: 3 comments 7 replies
-
Just a quick update after having re-written the portion of my app. I ended up passing components in the constructor. I felt like this afforded me the most declarativeness. In cases where I don't diverge, I use a shared component. In cases where the context is such that I need specific code, I construct a specific component that conforms to the expected interface. One of the pages ended up looking like this: build = Shared::Build.new(some: "stuff")
notes = Shared::Notes.new(some: "stuff")
settings = SpecificNamespace::Settings.new(some: "stuff")
build_tab = Shared::Tab.new(title: "Build", icon: "fa-puzzle-piece", panel: build)
notes_tab = Shared::Tab.new(title: "Notes", icon: "fa-pencil", panel: notes)
settings_tab = Shared::Tab.new(title: "Settings", icon: "fa-cog", panel: settings)
tab_container = Shared::TabContainer.new(tabs: [build_tab, notes_tab, settings_tab])
export_modal = SpecificNamespace::ExportModal.new(title: "Something")
exporter = Shared::Exporter.new(modal: export_modal)
Builder.new(tab_container: tab_container, exporter: exporter) Then there are competing pages that have a similar shape but use components from a different namespace. Once I have it working, I'm going to see if I can rework it to use slots v2. |
Beta Was this translation helpful? Give feedback.
-
I found this discussion after running into the same trouble today. My use case is a table: / index.html.slim
/ This does not work, it's just wishful thinking
= render TableComponent.new do |table|
- @records.each do |record|
- table.row(url: record.url) do |row|
- row.column 'Date'
= record.date I thought I'd let Now I'm not sure how to move forward. If I do the following then the row information would be completely detached and inaccessible from the outer table component: / index.html.slim
/ This does work but is not what I want
= render TableComponent.new do |table|
- @records.each do |record|
= render RowComponent.new do |row|
- row.column 'Date'
= record.date Now, I could do something like this but it would be awkward trying to "calculate" the number of rows given this input. / index.html.slim
/ This does work but is cumbersome
= render TableComponent.new do |table|
- @records.each do |record|
- table.row_column 'Date'
= record.date
- table.row_action 'Show'
= link_to record_path(record)
- table.row_action 'Edit'
= link_to edit_record_path(record) I tried it, it works, but nested components felt more intuitive, so I tried that, but it doesn't work. I'd appreciate any ideas :) Is this a bug or a feature? |
Beta Was this translation helpful? Give feedback.
-
Closing this. In hindsight, after using view components a lot more -- and the library getting a lot better, I don't think this is relevant anymore. |
Beta Was this translation helpful? Give feedback.
-
Hi there 👋 Loving the library so far. Something similar to what I've wanted in rails views for a long time.
I work on an app that has some reasonably complicated views. In particular, these views get rendered in different contexts (surprise!). The shape are similar, but the components arrive at that shape in different ways -- and sometimes isn't relevant for a particular context.
I'm trying to take advantage of slots, but I'm wondering if I'm using the wrong tool for the job.
I have the urge to go deeper, because the "thing" slot actually has slots of its own, and I want to make sure to fill them in with components that are relevant to
SomePageComponentA
(as opposed toSomePageComponentB
).In other words, top level components that have the same shape, but their slots are filled in with different variants based on some context.
As far as I can tell, the first level slot declarations do not yield a block, and are unable to inject slots of their own at the point of initial render. In other words, at least from my experimentation, you cannot do:
Instead, it seems you have to have a template for "thing", which renders its slots. This is kind of fine, but I have all the context I need at the initial render and I'd like to just declare exactly what components fill in each slot -- instead of threading some data down to lower level components and making them responsible for deciding.
My solution has been to just pass the nested components I want to render into the component constructors, instead of taking advantage of slots.
Then "thing" can render "nested_thing" without caring too much about the fact that it's, say, for A or for B.
I'm curious if I'm missing a concept or trying to fit a square peg in a round hole. Apologies if I was too abstract, more than happy to try again.
Thanks in advance for the discussion :)
Beta Was this translation helpful? Give feedback.
All reactions