@@ -2,44 +2,19 @@ defmodule ComponentsGuideWeb.CalendarComponent do
2
2
use ComponentsGuideWeb , :component
3
3
4
4
def calendar_grid ( assigns ) do
5
- % { year: year , month: month } = assigns
6
-
7
- day =
8
- case assigns [ :current_date ] do
9
- nil ->
10
- nil
11
-
12
- % { day: day } ->
13
- day
14
- end
15
-
16
- # %{year: year, month: month, day: day} = today = assigns[:date]
17
- date = Date . new! ( year , month , 1 )
18
- start_date = Date . beginning_of_month ( date )
19
- end_date = Date . end_of_month ( date )
20
- date_range = Date . range ( start_date , end_date )
21
-
22
- day_of_week = Date . day_of_week ( start_date )
23
- day_offset = 1 - day_of_week
24
- max_week = div ( end_date . day + day_of_week + 5 , 7 )
5
+ % { current_date: current_date } = assigns
6
+ current_row_start_date = Date . beginning_of_week ( current_date )
7
+ current_day_of_week = Date . day_of_week ( current_date )
25
8
26
9
assigns =
27
10
assigns
28
11
|> Map . merge ( % {
29
- date_range: date_range ,
30
- year: year ,
31
- month: month ,
32
- day: day ,
33
- current: Date . new! ( year , month , day ) ,
34
- day_of_week: day_of_week ,
35
- day_offset: day_offset
12
+ current_row_start_date: current_row_start_date ,
13
+ current_day_of_week: current_day_of_week
36
14
} )
37
15
|> Map . put_new ( :extra , fn _ -> "" end )
38
16
39
17
~H"""
40
- <%= if false do %>
41
- < h2 class = "text-center " > <%= Calendar . strftime ( @ date_range . first , "%B %Y" ) %> </ h2 >
42
- <% end %>
43
18
< table class = "text-center " >
44
19
< thead class = "border-0 " >
45
20
< tr >
@@ -53,27 +28,11 @@ defmodule ComponentsGuideWeb.CalendarComponent do
53
28
</ tr >
54
29
</ thead >
55
30
< tbody >
56
- <%= for week_n <- 1 .. max_week , false do %>
57
- < tr class = "min-h-16 " >
58
- <%= for weekday <- 0 .. 6 do %>
59
- <% day = weekday + 1 + day_offset + ( ( week_n - 1 ) * 7 ) %>
60
- <%= if day in @ date_range . first . day .. @ date_range . last . day do %>
61
- <% current? = day == @ day %>
62
- < td aria-current = { if current? , do: "date" , else: "false" } class = { td_class ( % { current?: current? , weekday: weekday } ) } >
63
- < div class = "text-sm " > <%= day %> </ div >
64
- <%= @ extra . ( Date . new! ( year , month , day ) ) %>
65
- </ td >
66
- <% else %>
67
- < td role = "presentation " class = "" > </ td >
68
- <% end %>
69
- <% end %>
70
- </ tr >
71
- <% end %>
72
31
<%= for week_offset <- - 4 .. 4 do %>
73
32
< tr class = "min-h-16 group " >
74
- <%= for weekday <- 0 .. 6 do %>
75
- <% date = Date . add ( @ current , ( week_offset - 1 ) * 7 + weekday - day_offset ) %>
76
- <% current_day? = week_offset == 0 && day_of_week == weekday %>
33
+ <%= for weekday <- 1 .. 7 do %>
34
+ <% date = Date . add ( @ current_row_start_date , week_offset * 7 + ( weekday - 1 ) ) %>
35
+ <% current_day? = week_offset == 0 && @ current_day_of_week == weekday %>
77
36
< td aria-current = { if current_day? , do: "date" , else: "false" } class = { td_class ( % { current_day?: current_day? , weekday: weekday , week_offset: week_offset } ) } >
78
37
< div class = { td_text_class ( week_offset ) } > <%= Calendar . strftime ( date , "%d %b" ) %> </ div >
79
38
<%= @ extra . ( date ) %>
@@ -87,7 +46,7 @@ defmodule ComponentsGuideWeb.CalendarComponent do
87
46
end
88
47
89
48
defp td_class ( % { current_day?: true } ) , do: "bg-green-900/90 text-green-100"
90
- defp td_class ( % { weekday: weekday } ) when weekday in [ 5 , 6 ] , do: "bg-black/40"
49
+ defp td_class ( % { weekday: weekday } ) when weekday in [ 6 , 7 ] , do: "bg-black/40"
91
50
defp td_class ( % { week_offset: 0 } ) , do: "bg-green-900/25"
92
51
defp td_class ( _ ) , do: "bg-black"
93
52
0 commit comments