Skip to content

Commit 65e5ce3

Browse files
committed
fix: on blur
1 parent fcbdf9a commit 65e5ce3

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

packages/ui/src/components/DateInput/__stories__/Range.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const Range: StoryFn<ComponentProps<typeof DateInput>> = args => {
2323
const [endDate, setEndDate] = useState<Date | null>(null)
2424

2525
const [startMonth, setStartMonth] = useState<Date | null>(
26-
new Date('March 2024'),
26+
new Date('01/03/2024'),
2727
)
2828
const [endMonth, setEndMonth] = useState<Date | null>(null)
2929

packages/ui/src/components/DateInput/components/CalendarDaily.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ const Day = styled(Button)`
2929
3030
&[aria-label="in-range"]:hover {
3131
color: ${({ theme }) => theme.colors.neutral.textStronger};
32-
background-color: ${({ theme }) => theme.colors.primary.backgroundStrongHover};
32+
background-color: ${({ theme }) =>
33+
theme.colors.primary.backgroundStrongHover};
3334
}
3435
3536
&[aria-label="not-current"], :disabled {

packages/ui/src/components/DateInput/index.tsx

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { Card } from '../Card'
99
import { Stack } from '../Stack'
1010
import { Text } from '../Text'
1111
import { TextInput } from '../TextInput'
12-
import type { ContextProps } from './Context'
1312
import { DateInputContext } from './Context'
13+
import type { ContextProps } from './Context'
1414
import { CalendarContent } from './components/CalendarContent'
1515
import { CalendarPopup } from './components/Popup'
1616
import {
@@ -227,8 +227,16 @@ export const DateInput = <IsRange extends undefined | boolean>({
227227
end: endDate ?? computedRange.end,
228228
})
229229
}
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+
])
232240

233241
const manageOnChange = (event: ChangeEvent<HTMLInputElement>) => {
234242
const newValue = event.currentTarget.value
@@ -245,24 +253,38 @@ export const DateInput = <IsRange extends undefined | boolean>({
245253
setMonthToShow(computedNewRange[0].getMonth() + 1)
246254
setYearToShow(computedNewRange[0].getFullYear())
247255
}
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)
255256
} else {
256257
const computedDate = createDate(newValue, showMonthYearPicker)
257258

258259
if (computedDate) {
259260
setMonthToShow(computedDate.getMonth() + 1)
260261
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)
261287
}
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)
266288
}
267289
}
268290

@@ -311,7 +333,16 @@ export const DateInput = <IsRange extends undefined | boolean>({
311333
tooltip={tooltip}
312334
autoComplete="false"
313335
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+
}}
315346
clearable={clearable}
316347
/>
317348
</CalendarPopup>

0 commit comments

Comments
 (0)