@@ -150,17 +150,23 @@ const DatePickerRange = ({
150150 }
151151 } , [ internalStartDate , internalEndDate , updatemode ] ) ;
152152
153+ const isDateAllowed = useCallback (
154+ ( date ?: Date ) : date is Date => {
155+ return (
156+ ! ! date && ! isDateDisabled ( date , minDate , maxDate , disabledDates )
157+ ) ;
158+ } ,
159+ [ minDate , maxDate , disabledDates ]
160+ ) ;
161+
153162 const sendStartInputAsDate = useCallback (
154163 ( focusCalendar = false ) => {
155164 if ( startInputValue ) {
156165 setInternalStartDate ( undefined ) ;
157166 }
158167 const parsed = strAsDate ( startInputValue , display_format ) ;
159- const isValid =
160- parsed &&
161- ! isDateDisabled ( parsed , minDate , maxDate , disabledDates ) ;
162168
163- if ( isValid ) {
169+ if ( isDateAllowed ( parsed ) ) {
164170 setInternalStartDate ( parsed ) ;
165171 if ( focusCalendar ) {
166172 calendarRef . current ?. focusDate ( parsed ) ;
@@ -178,14 +184,7 @@ const DatePickerRange = ({
178184 }
179185 }
180186 } ,
181- [
182- startInputValue ,
183- display_format ,
184- start_date ,
185- minDate ,
186- maxDate ,
187- disabledDates ,
188- ]
187+ [ startInputValue , display_format , start_date , isDateAllowed ]
189188 ) ;
190189
191190 const sendEndInputAsDate = useCallback (
@@ -194,11 +193,8 @@ const DatePickerRange = ({
194193 setInternalEndDate ( undefined ) ;
195194 }
196195 const parsed = strAsDate ( endInputValue , display_format ) ;
197- const isValid =
198- parsed &&
199- ! isDateDisabled ( parsed , minDate , maxDate , disabledDates ) ;
200196
201- if ( isValid ) {
197+ if ( isDateAllowed ( parsed ) ) {
202198 setInternalEndDate ( parsed ) ;
203199 if ( focusCalendar ) {
204200 calendarRef . current ?. focusDate ( parsed ) ;
@@ -216,14 +212,7 @@ const DatePickerRange = ({
216212 }
217213 }
218214 } ,
219- [
220- endInputValue ,
221- display_format ,
222- end_date ,
223- minDate ,
224- maxDate ,
225- disabledDates ,
226- ]
215+ [ endInputValue , display_format , end_date , isDateAllowed ]
227216 ) ;
228217
229218 const clearSelection = useCallback (
@@ -242,15 +231,14 @@ const DatePickerRange = ({
242231
243232 const handleStartInputKeyDown = useCallback (
244233 ( e : React . KeyboardEvent < HTMLInputElement > ) => {
245- if ( [ 'ArrowUp' , 'ArrowDown' ] . includes ( e . key ) ) {
234+ if ( [ 'ArrowUp' , 'ArrowDown' , 'Enter' ] . includes ( e . key ) ) {
246235 e . preventDefault ( ) ;
247- sendStartInputAsDate ( true ) ;
248236 if ( ! isCalendarOpen ) {
249- // open the calendar after resolving prop changes, so that
250- // it opens with the correct date showing
251- setTimeout ( ( ) => setIsCalendarOpen ( true ) , 0 ) ;
237+ setIsCalendarOpen ( true ) ;
252238 }
253- } else if ( [ 'Enter' , 'Tab' ] . includes ( e . key ) ) {
239+ // Wait for calendar to mount before focusing
240+ requestAnimationFrame ( ( ) => sendStartInputAsDate ( true ) ) ;
241+ } else if ( e . key === 'Tab' ) {
254242 sendStartInputAsDate ( ) ;
255243 }
256244 } ,
@@ -259,15 +247,14 @@ const DatePickerRange = ({
259247
260248 const handleEndInputKeyDown = useCallback (
261249 ( e : React . KeyboardEvent < HTMLInputElement > ) => {
262- if ( [ 'ArrowUp' , 'ArrowDown' ] . includes ( e . key ) ) {
250+ if ( [ 'ArrowUp' , 'ArrowDown' , 'Enter' ] . includes ( e . key ) ) {
263251 e . preventDefault ( ) ;
264- sendEndInputAsDate ( true ) ;
265252 if ( ! isCalendarOpen ) {
266- // open the calendar after resolving prop changes, so that
267- // it opens with the correct date showing
268- setTimeout ( ( ) => setIsCalendarOpen ( true ) , 0 ) ;
253+ setIsCalendarOpen ( true ) ;
269254 }
270- } else if ( [ 'Enter' , 'Tab' ] . includes ( e . key ) ) {
255+ // Wait for calendar to mount before focusing
256+ requestAnimationFrame ( ( ) => sendEndInputAsDate ( true ) ) ;
257+ } else if ( e . key === 'Tab' ) {
271258 sendEndInputAsDate ( ) ;
272259 }
273260 } ,
@@ -349,18 +336,16 @@ const DatePickerRange = ({
349336 onChange = { e => setStartInputValue ( e . target . value ) }
350337 onKeyDown = { handleStartInputKeyDown }
351338 onFocus = { ( ) => {
352- if ( internalStartDate ) {
353- calendarRef . current ?. setVisibleDate (
354- internalStartDate
355- ) ;
339+ if ( isCalendarOpen ) {
340+ sendStartInputAsDate ( ) ;
356341 }
357342 } }
358343 placeholder = { start_date_placeholder_text }
359344 disabled = { disabled }
360345 dir = { direction }
361346 aria-label = { start_date_placeholder_text }
362347 />
363- < ArrowIcon />
348+ < ArrowIcon className = "dash-datepicker-range-arrow" />
364349 < AutosizeInput
365350 inputRef = { node => {
366351 endInputRef . current = node ;
@@ -372,10 +357,8 @@ const DatePickerRange = ({
372357 onChange = { e => setEndInputValue ( e . target . value ) }
373358 onKeyDown = { handleEndInputKeyDown }
374359 onFocus = { ( ) => {
375- if ( internalEndDate ) {
376- calendarRef . current ?. setVisibleDate (
377- internalEndDate
378- ) ;
360+ if ( isCalendarOpen ) {
361+ sendEndInputAsDate ( ) ;
379362 }
380363 } }
381364 placeholder = { end_date_placeholder_text }
0 commit comments