Skip to content

Commit 62a80fa

Browse files
committed
Use slot in calendar component
1 parent e075184 commit 62a80fa

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/components_guide_web/components/calendar_component.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
defmodule ComponentsGuideWeb.CalendarComponent do
22
use ComponentsGuideWeb, :component
3+
4+
attr :current_date, Date, required: true
5+
6+
slot :cell_content
37

48
def calendar_grid(assigns) do
59
%{current_date: current_date} = assigns
@@ -12,7 +16,6 @@ defmodule ComponentsGuideWeb.CalendarComponent do
1216
current_row_start_date: current_row_start_date,
1317
current_day_of_week: current_day_of_week
1418
})
15-
|> Map.put_new(:extra, fn _ -> "" end)
1619

1720
~H"""
1821
<table class="text-center">
@@ -35,7 +38,7 @@ defmodule ComponentsGuideWeb.CalendarComponent do
3538
<% current_day? = week_offset == 0 && @current_day_of_week == weekday %>
3639
<td aria-current={if current_day?, do: "date", else: "false"} class={td_class(%{current_day?: current_day?, weekday: weekday, week_offset: week_offset})}>
3740
<div class={td_text_class(week_offset, weekday, current_day?)}><%= Calendar.strftime(date, "%d %b") %></div>
38-
<%= @extra.(date) %>
41+
<%= render_slot(@cell_content, date) %>
3942
</td>
4043
<% end %>
4144
</tr>

lib/components_guide_web/components/dev_calendar_component.ex

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ defmodule ComponentsGuideWeb.DevCalendarComponent do
1717

1818
assigns = %{
1919
today: today,
20-
calendar_extra: calendar_extra
20+
calendar_extra: calendar_extra,
21+
links: links
2122
}
2223

2324
~H"""
24-
<CalendarComponent.calendar_grid current_date={@today} extra={@calendar_extra} />
25+
<CalendarComponent.calendar_grid current_date={@today}>
26+
<:cell_content :let={date}>
27+
<%= @calendar_extra.(date) %>
28+
</:cell_content>
29+
</CalendarComponent.calendar_grid>
2530
"""
2631
end
2732

0 commit comments

Comments
 (0)