You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The viewcomponent best practices mention Application-specific ViewComponents that can be used to translate a domain object into general-purpose components. This is very usefull to make your code more DRY but it is currently not possible to pass inherited ViewComponents into slots.
Context
renders_one and renders_many slots in ViewComponent today only accept:
initializer args for the specified component class, or
a block for setting the component’s content.
They do not accept a pre-built component instance.
This creates friction when you want to extend a base component (e.g. BadgeComponent) into a semantic wrapper (e.g. StatusBadgeComponent) and then slot that wrapper into a parent component (e.g. CardComponent).
Right now, developers must either:
Re-map wrapper logic into slot args, or
Use polymorphic slots with boilerplate with_badge_status, with_badge_plain methods.
A more natural developer experience would allow a wrapper component to be passed directly into a slot.
Proposed solution
Allow renders_one and renders_many slots to accept either:
the current form (args → new instance of slot class), or
a pre-built ViewComponent::Base instance (that inherits from the slot class).
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
The viewcomponent best practices mention Application-specific ViewComponents that can be used to translate a domain object into general-purpose components. This is very usefull to make your code more DRY but it is currently not possible to pass inherited ViewComponents into slots.
Context
renders_one
andrenders_many
slots in ViewComponent today only accept:They do not accept a pre-built component instance.
This creates friction when you want to extend a base component (e.g.
BadgeComponent
) into a semantic wrapper (e.g.StatusBadgeComponent
) and then slot that wrapper into a parent component (e.g.CardComponent
).Right now, developers must either:
with_badge_status
,with_badge_plain
methods.A more natural developer experience would allow a wrapper component to be passed directly into a slot.
Proposed solution
Allow
renders_one
andrenders_many
slots to accept either:ViewComponent::Base
instance (that inherits from the slot class).Example
Usage with current slots (works today)
Desired usage with application specific component (proposed)
Under the hood, the
badge
slot would detect if the argument is a BadgeComponent instance:Beta Was this translation helpful? Give feedback.
All reactions