Narven
Surface render slot of child component
Hi,
I’m trying to create a Tab component with TabItem children using Surface. Currently it looks like this:
Tabs.ex
defmodule DoomWeb.Components.Tabs.Tabs do
use Surface.LiveComponent
prop(initial, :atom, required: true)
data(selected_tab, :atom, default: nil)
slot(tabs)
def mount(socket) do
{
:ok,
socket
|> assign(:selected_tab, :plans)
}
end
def handle_event(event, params, socket) do
case event do
n when n == "change_tab" ->
on_change_tab(params, socket)
_ ->
{
:noreply,
socket
}
end
end
defp on_change_tab(%{"tab" => tab_id}, socket) do
{
:noreply,
socket
|> assign(:selected_tab, String.to_atom(tab_id))
}
end
def render(assigns) do
~F"""
<div class="flex flex-wrap w-full">
<ul class="flex mb-0 list-none flex-wrap pb-0 flex-row border-b-1">
{#for tab <- @tabs}
<li
class="-mb-px mr-2 last:mr-0 flex-auto text-left"
:values={tab: tab.id}
:on-click="change_tab"
>
<a class="text-md font-bold px-0 rounded-t block leading-normal cursor-pointer text-skin-base">
{tab.title}
</a>
</li>
{/for}
</ul>
{#for tab <- @tabs}
<div class="block p-2 text-sm" :if={@selected_tab == tab.id}>
<#slot /> <-------- HERE how can i access tab item .slot??
</div>
{/for}
</div>
"""
end
end
TabItem.ex
defmodule DoomWeb.Components.Tabs.TabItem do
use Surface.Component, slot: "tabs"
prop(id, :atom, required: true)
prop(title, :string, required: true)
prop(icon, :string)
slot(default)
end
Then I can use it:
<Tabs id="subscription" initial={:plans}>
<TabItem id={:plans} title={gettext("Plans")}>
plans
</TabItem>
<TabItem id={:billing_history} title={gettext("Billing History")}>
billing history
</TabItem>
<TabItem id={:payment_method} title={gettext("Payment Method")}>
payment method
</TabItem>
</Tabs>
My question is how can I access inside the for expression on the Tab.ex the slot of each of the TabItem, so that I can render them?
Marked As Solved
tiagoefmoraes
It should be:
<#slot for={tab}/>
1
Popular in Questions
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
I would like to know what is the best IDE for elixir development?
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project.
Baby step #1 is extracting the number ...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
Other popular topics
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New








