@@ -154,32 +154,13 @@ fn replace_month(date: Date, month: Month) -> Date {
154154 . expect ( "invalid or out-of-range date" )
155155}
156156
157- fn gt ( lhs_month : Month , rhs_month : Month ) -> bool {
158- let lhs = lhs_month as u8 ;
159- let rhs = rhs_month as u8 ;
160- lhs > rhs
161- }
162-
163- fn nth_month_prev ( date : Date , n : u8 ) -> Option < Date > {
164- match n {
165- 0 => Some ( date) ,
166- n => {
167- let month = date. month ( ) ;
168- let nth_month: Month = month. nth_prev ( n) ;
169- let year = date. year ( ) - if gt ( month, nth_month) { 0 } else { 1 } ;
170- let max_day = nth_month. length ( year) ;
171- Date :: from_calendar_date ( year, nth_month, date. day ( ) . min ( max_day) ) . ok ( )
172- }
173- }
174- }
175-
176157fn nth_month_next ( date : Date , n : u8 ) -> Option < Date > {
177158 match n {
178159 0 => Some ( date) ,
179160 n => {
180161 let month = date. month ( ) ;
181162 let nth_month = month. nth_next ( n) ;
182- let year = date. year ( ) + if gt ( month, nth_month) { 1 } else { 0 } ;
163+ let year = date. year ( ) + if month > nth_month { 1 } else { 0 } ;
183164 let max_day = nth_month. length ( year) ;
184165 Date :: from_calendar_date ( year, nth_month, date. day ( ) . min ( max_day) ) . ok ( )
185166 }
@@ -328,6 +309,7 @@ pub struct BaseCalendarContext {
328309 today : Date ,
329310 first_day_of_week : Weekday ,
330311 enabled_date_range : DateRange ,
312+ month_count : u8 ,
331313}
332314
333315impl BaseCalendarContext {
@@ -524,6 +506,7 @@ pub fn Calendar(props: CalendarProps) -> Element {
524506 today : props. today ,
525507 first_day_of_week : props. first_day_of_week ,
526508 enabled_date_range : DateRange :: new ( props. min_date , props. max_date ) ,
509+ month_count : props. month_count ,
527510 } ) ;
528511 // Create Calendar context provider for child components
529512 use_context_provider ( || CalendarContext {
@@ -781,6 +764,7 @@ pub fn RangeCalendar(props: RangeCalendarProps) -> Element {
781764 today : props. today ,
782765 first_day_of_week : props. first_day_of_week ,
783766 enabled_date_range : DateRange :: new ( props. min_date , props. max_date ) ,
767+ month_count : props. month_count ,
784768 } ) ;
785769
786770 // Create RangeCalendar context provider for child components
@@ -890,7 +874,7 @@ struct CalendarViewProps {
890874 pub children : Element ,
891875}
892876
893- #[ derive( Clone , PartialEq ) ]
877+ #[ derive( Copy , Clone , PartialEq ) ]
894878struct CalendarViewContext {
895879 offset : u8 ,
896880 view_date : Signal < Date > ,
@@ -902,25 +886,17 @@ impl CalendarViewContext {
902886 self . view_date . set ( new_date) ;
903887 }
904888
905- fn sub_months ( & self , date : Date ) -> Option < Date > {
906- nth_month_prev ( date, self . offset . max ( 1 ) )
907- }
908-
909- fn add_months ( & self , date : Date ) -> Option < Date > {
910- nth_month_next ( date, self . offset . max ( 1 ) )
911- }
912-
913889 fn replace_year ( & self , date : Date , year : i32 ) -> Date {
914890 let month = date. month ( ) ;
915891 let view_month = ( self . view_date ) ( ) . month ( ) ;
916- let year = year - if gt ( month, view_month) { 1 } else { 0 } ;
892+ let year = year - if month > view_month { 1 } else { 0 } ;
917893 date. replace_year ( year) . unwrap_or ( date)
918894 }
919895
920896 fn replace_month ( & self , date : Date , month : Month ) -> Date {
921897 let view_date = ( self . view_date ) ( ) ;
922898 let new_month = month. nth_prev ( self . offset ) ;
923- let year = view_date. year ( ) - if gt ( month, view_date. month ( ) ) { 1 } else { 0 } ;
899+ let year = view_date. year ( ) - if month > view_date. month ( ) { 1 } else { 0 } ;
924900 Date :: from_calendar_date ( year, new_month, 1 ) . unwrap_or ( date)
925901 }
926902
@@ -1147,6 +1123,10 @@ pub fn CalendarPreviousMonthButton(props: CalendarPreviousMonthButtonProps) -> E
11471123 let ctx: BaseCalendarContext = use_context ( ) ;
11481124 let view_ctx: CalendarViewContext = use_context ( ) ;
11491125
1126+ if view_ctx. offset != 0 {
1127+ return rsx ! { } ;
1128+ }
1129+
11501130 // disable previous button when we reach the limit
11511131 let button_disabled = use_memo ( move || {
11521132 // Get the current view date from context
@@ -1168,7 +1148,7 @@ pub fn CalendarPreviousMonthButton(props: CalendarPreviousMonthButtonProps) -> E
11681148 let handle_prev_month = move |e : Event < MouseData > | {
11691149 e. prevent_default ( ) ;
11701150 let current_view = ( ctx. view_date ) ( ) ;
1171- if let Some ( date) = view_ctx . sub_months ( current_view) {
1151+ if let Some ( date) = previous_month ( current_view) {
11721152 ctx. set_view_date . call ( date)
11731153 }
11741154 } ;
@@ -1248,6 +1228,10 @@ pub fn CalendarNextMonthButton(props: CalendarNextMonthButtonProps) -> Element {
12481228 let ctx: BaseCalendarContext = use_context ( ) ;
12491229 let view_ctx: CalendarViewContext = use_context ( ) ;
12501230
1231+ if view_ctx. offset + 1 != ctx. month_count {
1232+ return rsx ! { } ;
1233+ }
1234+
12511235 // disable next button when we reach the limit
12521236 let button_disabled = use_memo ( move || {
12531237 // Get the current view date from context
@@ -1273,7 +1257,7 @@ pub fn CalendarNextMonthButton(props: CalendarNextMonthButtonProps) -> Element {
12731257 let handle_next_month = move |e : Event < MouseData > | {
12741258 e. prevent_default ( ) ;
12751259 let current_view = ( ctx. view_date ) ( ) ;
1276- if let Some ( date) = view_ctx . add_months ( current_view) {
1260+ if let Some ( date) = next_month ( current_view) {
12771261 ctx. set_view_date . call ( date)
12781262 }
12791263 } ;
@@ -1890,9 +1874,7 @@ fn relative_calendar_month(
18901874 } else if date > base_ctx. enabled_date_range . end {
18911875 RelativeMonth :: Next
18921876 } else {
1893- let lhs = date. month ( ) as u8 ;
1894- let rhs = current_month as u8 ;
1895- match lhs. cmp ( & rhs) {
1877+ match date. month ( ) . cmp ( & current_month) {
18961878 std:: cmp:: Ordering :: Less => RelativeMonth :: Last ,
18971879 std:: cmp:: Ordering :: Equal => RelativeMonth :: Current ,
18981880 std:: cmp:: Ordering :: Greater => RelativeMonth :: Next ,
0 commit comments