@@ -9,8 +9,8 @@ import { Card } from '../Card'
9
9
import { Stack } from '../Stack'
10
10
import { Text } from '../Text'
11
11
import { TextInput } from '../TextInput'
12
- import type { ContextProps } from './Context'
13
12
import { DateInputContext } from './Context'
13
+ import type { ContextProps } from './Context'
14
14
import { CalendarContent } from './components/CalendarContent'
15
15
import { CalendarPopup } from './components/Popup'
16
16
import {
@@ -227,8 +227,16 @@ export const DateInput = <IsRange extends undefined | boolean>({
227
227
end : endDate ?? computedRange . end ,
228
228
} )
229
229
}
230
- // oxlint-disable-next-line react/exhaustive-deps
231
- } , [ endDate , startDate , value ] )
230
+ } , [
231
+ endDate ,
232
+ startDate ,
233
+ value ,
234
+ computedRange . start ,
235
+ computedRange . end ,
236
+ selectsRange ,
237
+ format ,
238
+ showMonthYearPicker ,
239
+ ] )
232
240
233
241
const manageOnChange = ( event : ChangeEvent < HTMLInputElement > ) => {
234
242
const newValue = event . currentTarget . value
@@ -245,24 +253,38 @@ export const DateInput = <IsRange extends undefined | boolean>({
245
253
setMonthToShow ( computedNewRange [ 0 ] . getMonth ( ) + 1 )
246
254
setYearToShow ( computedNewRange [ 0 ] . getFullYear ( ) )
247
255
}
248
- // TypeScript fails to automatically get the correct type of onChange here
249
- ; (
250
- onChange as (
251
- date : Date [ ] | [ Date | null , Date | null ] ,
252
- event : React . SyntheticEvent | undefined ,
253
- ) => void
254
- ) ?.( computedNewRange , event )
255
256
} else {
256
257
const computedDate = createDate ( newValue , showMonthYearPicker )
257
258
258
259
if ( computedDate ) {
259
260
setMonthToShow ( computedDate . getMonth ( ) + 1 )
260
261
setYearToShow ( computedDate . getFullYear ( ) )
262
+ setValue ( computedDate )
263
+ }
264
+ }
265
+ setInputValue ( newValue )
266
+ }
267
+
268
+ const onBlurInput = ( ) => {
269
+ // Only call onChange when there is a date typed in the input and the user did not click on the calendar (which triggers onChange itself)
270
+ if ( inputValue ) {
271
+ if ( selectsRange ) {
272
+ const computedNewRange = createDateRange (
273
+ inputValue ,
274
+ showMonthYearPicker ,
275
+ )
276
+ ; (
277
+ onChange as (
278
+ date : Date [ ] | [ Date | null , Date | null ] ,
279
+ event : React . SyntheticEvent | undefined ,
280
+ ) => void
281
+ ) ?.( computedNewRange , undefined )
282
+ } else {
283
+ const computedDate = createDate ( inputValue , showMonthYearPicker )
284
+ ; (
285
+ onChange as ( date : Date | null , event ?: React . SyntheticEvent ) => void
286
+ ) ?.( computedDate , undefined )
261
287
}
262
- // TypeScript fails to automatically get the correct type of onChange here
263
- ; (
264
- onChange as ( date : Date | null , event ?: React . SyntheticEvent ) => void
265
- ) ?.( computedDate , event )
266
288
}
267
289
}
268
290
@@ -311,7 +333,16 @@ export const DateInput = <IsRange extends undefined | boolean>({
311
333
tooltip = { tooltip }
312
334
autoComplete = "false"
313
335
onChange = { manageOnChange }
314
- onBlur = { onBlurInput }
336
+ onBlur = { event => {
337
+ if ( ! popupRef . current ?. contains ( event . relatedTarget ) )
338
+ onBlurInput ( )
339
+ } }
340
+ onKeyDown = { event => {
341
+ if ( event . key === 'Enter' ) {
342
+ setVisible ( ! isPopupVisible )
343
+ onBlurInput ( )
344
+ }
345
+ } }
315
346
clearable = { clearable }
316
347
/>
317
348
</ CalendarPopup >
0 commit comments