@@ -2891,6 +2891,56 @@ describe("DatePicker", () => {
28912891 formatDate ( future , "yyyy-MM-dd" ) ,
28922892 ) ;
28932893 } ) ;
2894+ it ( "should update calendar view when selected prop changes to a different month (issue #3367)" , ( ) => {
2895+ // This test verifies that when a user programmatically sets selected date
2896+ // (e.g., via a "Today" button), the calendar view updates to show
2897+ // the month containing the new date, even if user was viewing a different month.
2898+ const initialDate = new Date ( "2024-01-15" ) ;
2899+ let instance : DatePicker | null = null ;
2900+
2901+ const { container, rerender } = render (
2902+ < DatePicker
2903+ ref = { ( node ) => {
2904+ instance = node ;
2905+ } }
2906+ selected = { initialDate }
2907+ onChange = { ( ) => { } }
2908+ /> ,
2909+ ) ;
2910+
2911+ expect ( instance ) . toBeTruthy ( ) ;
2912+
2913+ // Open the calendar
2914+ const input = safeQuerySelector ( container , "input" ) ;
2915+ fireEvent . click ( input ) ;
2916+
2917+ // Verify initial preSelection is set to January 2024
2918+ expect ( instance ! . state . preSelection ?. getMonth ( ) ) . toBe ( 0 ) ; // January
2919+ expect ( instance ! . state . preSelection ?. getFullYear ( ) ) . toBe ( 2024 ) ;
2920+
2921+ // Close calendar
2922+ fireEvent . click ( input ) ;
2923+
2924+ // Simulate programmatic update to a different month (e.g., user clicks "Jump to March" button)
2925+ const newDate = new Date ( "2024-03-10" ) ;
2926+
2927+ rerender (
2928+ < DatePicker
2929+ ref = { ( node ) => {
2930+ instance = node ;
2931+ } }
2932+ selected = { newDate }
2933+ onChange = { ( ) => { } }
2934+ /> ,
2935+ ) ;
2936+
2937+ // Open the calendar again
2938+ fireEvent . click ( input ) ;
2939+
2940+ // The calendar should now show March 2024, not January 2024
2941+ expect ( instance ! . state . preSelection ?. getMonth ( ) ) . toBe ( 2 ) ; // March
2942+ expect ( instance ! . state . preSelection ?. getFullYear ( ) ) . toBe ( 2024 ) ;
2943+ } ) ;
28942944 it ( "should not set open state when focusing on the date input and the preventOpenOnFocus prop is set" , ( ) => {
28952945 const { container } = render ( < DatePicker preventOpenOnFocus /> ) ;
28962946 const input = safeQuerySelector ( container , "input" ) ;
@@ -3855,6 +3905,63 @@ describe("DatePicker", () => {
38553905 const input = safeQuerySelector < HTMLInputElement > ( container , "input" ) ;
38563906 expect ( input . value ) . toBe ( "2025/01/01 to 2025/01/03" ) ;
38573907 } ) ;
3908+
3909+ it ( "should update calendar view when startDate prop changes to a different month (issue #3367)" , ( ) => {
3910+ // This test verifies that when a user programmatically sets startDate
3911+ // (e.g., via a "This Week" button), the calendar view updates to show
3912+ // the month containing the new startDate, even if user was viewing a different month.
3913+ const initialStartDate = new Date ( "2024-01-15" ) ;
3914+ const initialEndDate = new Date ( "2024-01-20" ) ;
3915+ let instance : DatePicker | null = null ;
3916+
3917+ const { container, rerender } = render (
3918+ < DatePicker
3919+ ref = { ( node ) => {
3920+ instance = node ;
3921+ } }
3922+ selectsRange
3923+ startDate = { initialStartDate }
3924+ endDate = { initialEndDate }
3925+ onChange = { ( ) => { } }
3926+ /> ,
3927+ ) ;
3928+
3929+ expect ( instance ) . toBeTruthy ( ) ;
3930+
3931+ // Open the calendar
3932+ const input = safeQuerySelector ( container , "input" ) ;
3933+ fireEvent . click ( input ) ;
3934+
3935+ // Verify initial preSelection is set to January 2024
3936+ expect ( instance ! . state . preSelection ?. getMonth ( ) ) . toBe ( 0 ) ; // January
3937+ expect ( instance ! . state . preSelection ?. getFullYear ( ) ) . toBe ( 2024 ) ;
3938+
3939+ // Close calendar
3940+ fireEvent . click ( input ) ;
3941+
3942+ // Simulate programmatic update to a different month (e.g., user clicks "This Week" button in March)
3943+ const newStartDate = new Date ( "2024-03-10" ) ;
3944+ const newEndDate = new Date ( "2024-03-15" ) ;
3945+
3946+ rerender (
3947+ < DatePicker
3948+ ref = { ( node ) => {
3949+ instance = node ;
3950+ } }
3951+ selectsRange
3952+ startDate = { newStartDate }
3953+ endDate = { newEndDate }
3954+ onChange = { ( ) => { } }
3955+ /> ,
3956+ ) ;
3957+
3958+ // Open the calendar again
3959+ fireEvent . click ( input ) ;
3960+
3961+ // The calendar should now show March 2024, not January 2024
3962+ expect ( instance ! . state . preSelection ?. getMonth ( ) ) . toBe ( 2 ) ; // March
3963+ expect ( instance ! . state . preSelection ?. getFullYear ( ) ) . toBe ( 2024 ) ;
3964+ } ) ;
38583965 } ) ;
38593966
38603967 describe ( "duplicate dates when multiple months" , ( ) => {
0 commit comments