-
Notifications
You must be signed in to change notification settings - Fork 0
/
tab.ex
150 lines (139 loc) · 3.31 KB
/
tab.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
defmodule PhoenixDuskmoon.Tab do
@moduledoc """
render tab
"""
use PhoenixDuskmoon, :html
@doc """
Generates tabs
## Example
<.dm_tab active_tab_index={0}>
<:tab>Menu1</:tab>
<:tab>Menu2</:tab>
<:tab_content>Menu1 Content</:tab_content>
<:tab_content>Menu2 Content</:tab_content>
</.dm_tab>
"""
@doc type: :component
attr(:id, :any,
default: false,
doc: """
html attribute id
"""
)
attr(:class, :any,
default: "",
doc: """
html attribute class
"""
)
attr(:header_class, :any,
default: "",
doc: """
header html attribute class
"""
)
attr(:orientation, :string,
default: "horizontal",
values: ["horizontal", "vertical"],
doc: """
header html attribute class
"""
)
attr(:active_tab_index, :integer,
default: 0,
doc: """
the index of active tab, if active_tab_name is not set, this will be used
"""
)
attr(:active_tab_name, :string,
default: "",
doc: """
the name of active tab, use for match tab and tab_content
"""
)
attr(:active_tab_class, :any,
doc: """
class of active tab
"""
)
slot(:tab,
required: false,
doc: """
Render tab
"""
) do
attr :id, :any
attr :class, :any
attr :name, :string
end
slot(:tab_content,
required: false,
doc: """
Render tab content
"""
) do
attr :id, :any
attr :class, :any
attr :name, :string
end
def dm_tab(assigns) do
assigns = assigns |> assign_new(:active_tab_class, fn() ->
if assigns[:orientation] == "horizontal" do
"text-blue-400 border-x-0 border-t-0 border-b border-solid border-blue-400"
else
"text-blue-400 border-y-0 border-l-0 border-r border-solid border-blue-400"
end
end)
~H"""
<section
id={@id}
class={[
"flex gap-2",
"w-full px-4",
if(@orientation == "horizontal", do: "flex-col", else: "flex-row"),
@class
]}
>
<header
class={[
"flex justify-start items-center gap-2",
if(@orientation == "horizontal", do: "flex-row", else: "flex-col"),
@header_class
]}
>
<%= for {tab, i} <- Enum.with_index(@tab) do %>
<span
id={Map.get(tab, :id, false)}
class={[
"flex flex-row",
"py-4 px-2",
Map.get(tab, :class, ""),
if(@active_tab_name != "",
do: if(@active_tab_name == Map.get(tab, :name, ""),
do: @active_tab_class,
else: "cursor-pointer"),
else: if(@active_tab_index == i,
do: @active_tab_class,
else: "cursor-pointer"))
]}
phx-click={Map.get(tab, :phx_click, nil)}
>
<%= render_slot(tab) %>
</span>
<% end %>
</header>
<%= for {tab_content, i} <- Enum.with_index(@tab_content) do %>
<%= if @active_tab_name != "" do %>
<%= if @active_tab_name == Map.get(tab_content, :name, "") do %>
<%= render_slot(tab_content) %>
<% end %>
<% else %>
<%= if @active_tab_index == i do %>
<%= render_slot(tab_content) %>
<% end %>
<% end %>
<% end %>
</section>
"""
end
end