@@ -2947,6 +2947,56 @@ describe("DatePicker", () => {
29472947 formatDate ( future , "yyyy-MM-dd" ) ,
29482948 ) ;
29492949 } ) ;
2950+ it ( "should update calendar view when selected prop changes to a different month (issue #3367)" , ( ) => {
2951+ // This test verifies that when a user programmatically sets selected date
2952+ // (e.g., via a "Today" button), the calendar view updates to show
2953+ // the month containing the new date, even if user was viewing a different month.
2954+ const initialDate = new Date ( "2024-01-15" ) ;
2955+ let instance : DatePicker | null = null ;
2956+
2957+ const { container, rerender } = render (
2958+ < DatePicker
2959+ ref = { ( node ) => {
2960+ instance = node ;
2961+ } }
2962+ selected = { initialDate }
2963+ onChange = { ( ) => { } }
2964+ /> ,
2965+ ) ;
2966+
2967+ expect ( instance ) . toBeTruthy ( ) ;
2968+
2969+ // Open the calendar
2970+ const input = safeQuerySelector ( container , "input" ) ;
2971+ fireEvent . click ( input ) ;
2972+
2973+ // Verify initial preSelection is set to January 2024
2974+ expect ( instance ! . state . preSelection ?. getMonth ( ) ) . toBe ( 0 ) ; // January
2975+ expect ( instance ! . state . preSelection ?. getFullYear ( ) ) . toBe ( 2024 ) ;
2976+
2977+ // Close calendar
2978+ fireEvent . click ( input ) ;
2979+
2980+ // Simulate programmatic update to a different month (e.g., user clicks "Jump to March" button)
2981+ const newDate = new Date ( "2024-03-10" ) ;
2982+
2983+ rerender (
2984+ < DatePicker
2985+ ref = { ( node ) => {
2986+ instance = node ;
2987+ } }
2988+ selected = { newDate }
2989+ onChange = { ( ) => { } }
2990+ /> ,
2991+ ) ;
2992+
2993+ // Open the calendar again
2994+ fireEvent . click ( input ) ;
2995+
2996+ // The calendar should now show March 2024, not January 2024
2997+ expect ( instance ! . state . preSelection ?. getMonth ( ) ) . toBe ( 2 ) ; // March
2998+ expect ( instance ! . state . preSelection ?. getFullYear ( ) ) . toBe ( 2024 ) ;
2999+ } ) ;
29503000 it ( "should not set open state when focusing on the date input and the preventOpenOnFocus prop is set" , ( ) => {
29513001 const { container } = render ( < DatePicker preventOpenOnFocus /> ) ;
29523002 const input = safeQuerySelector ( container , "input" ) ;
@@ -3911,6 +3961,63 @@ describe("DatePicker", () => {
39113961 const input = safeQuerySelector < HTMLInputElement > ( container , "input" ) ;
39123962 expect ( input . value ) . toBe ( "2025/01/01 to 2025/01/03" ) ;
39133963 } ) ;
3964+
3965+ it ( "should update calendar view when startDate prop changes to a different month (issue #3367)" , ( ) => {
3966+ // This test verifies that when a user programmatically sets startDate
3967+ // (e.g., via a "This Week" button), the calendar view updates to show
3968+ // the month containing the new startDate, even if user was viewing a different month.
3969+ const initialStartDate = new Date ( "2024-01-15" ) ;
3970+ const initialEndDate = new Date ( "2024-01-20" ) ;
3971+ let instance : DatePicker | null = null ;
3972+
3973+ const { container, rerender } = render (
3974+ < DatePicker
3975+ ref = { ( node ) => {
3976+ instance = node ;
3977+ } }
3978+ selectsRange
3979+ startDate = { initialStartDate }
3980+ endDate = { initialEndDate }
3981+ onChange = { ( ) => { } }
3982+ /> ,
3983+ ) ;
3984+
3985+ expect ( instance ) . toBeTruthy ( ) ;
3986+
3987+ // Open the calendar
3988+ const input = safeQuerySelector ( container , "input" ) ;
3989+ fireEvent . click ( input ) ;
3990+
3991+ // Verify initial preSelection is set to January 2024
3992+ expect ( instance ! . state . preSelection ?. getMonth ( ) ) . toBe ( 0 ) ; // January
3993+ expect ( instance ! . state . preSelection ?. getFullYear ( ) ) . toBe ( 2024 ) ;
3994+
3995+ // Close calendar
3996+ fireEvent . click ( input ) ;
3997+
3998+ // Simulate programmatic update to a different month (e.g., user clicks "This Week" button in March)
3999+ const newStartDate = new Date ( "2024-03-10" ) ;
4000+ const newEndDate = new Date ( "2024-03-15" ) ;
4001+
4002+ rerender (
4003+ < DatePicker
4004+ ref = { ( node ) => {
4005+ instance = node ;
4006+ } }
4007+ selectsRange
4008+ startDate = { newStartDate }
4009+ endDate = { newEndDate }
4010+ onChange = { ( ) => { } }
4011+ /> ,
4012+ ) ;
4013+
4014+ // Open the calendar again
4015+ fireEvent . click ( input ) ;
4016+
4017+ // The calendar should now show March 2024, not January 2024
4018+ expect ( instance ! . state . preSelection ?. getMonth ( ) ) . toBe ( 2 ) ; // March
4019+ expect ( instance ! . state . preSelection ?. getFullYear ( ) ) . toBe ( 2024 ) ;
4020+ } ) ;
39144021 } ) ;
39154022
39164023 describe ( "duplicate dates when multiple months" , ( ) => {
0 commit comments