Skip to content

Commit

Permalink
fix(fields): handle detached click outside for time in DatePicker & D…
Browse files Browse the repository at this point in the history
…ateRangePicker
  • Loading branch information
ivangabriele committed Jan 13, 2023
1 parent 7dcfbc3 commit afa0a13
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 27 deletions.
4 changes: 2 additions & 2 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Related Issues

- Resolve MTES-MCT/monitorenv#123
- Resolve MTES-MCT/monitorfish#123
- MTES-MCT/monitorenv#123
- MTES-MCT/monitorfish#123

## Preview URL

Expand Down
1 change: 1 addition & 0 deletions src/fields/DatePicker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ export function DatePicker({
<Field $isTimeField>
<TimeInput
ref={timeInputRef}
baseContainer={baseContainer}
defaultValue={selectedTimeTupleRef.current}
disabled={disabled}
isCompact={isCompact}
Expand Down
28 changes: 6 additions & 22 deletions src/fields/DateRangePicker/TimeInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState } from 'react'
import { forwardRef, useCallback, useImperativeHandle, useRef, useState } from 'react'
import styled from 'styled-components'

import { useClickOutside } from '../../hooks/useClickOutside'
import { useForceUpdate } from '../../hooks/useForceUpdate'
import { Clock } from '../../icons'
import { NumberInput } from './NumberInput'
Expand All @@ -12,6 +13,7 @@ import type { ForwardedRef, MutableRefObject } from 'react'
import type { Promisable } from 'type-fest'

export type TimeInputProps = Pick<NumberInputProps, 'onBack' | 'onPrevious' | 'onNext'> & {
baseContainer?: Document | HTMLDivElement | null
defaultValue?: TimeTuple
// TODO Check why TS thinks there is no `disabled` prop in `NumberInputProps`.
disabled: boolean
Expand All @@ -28,6 +30,7 @@ export type TimeInputProps = Pick<NumberInputProps, 'onBack' | 'onPrevious' | 'o
}
function TimeInputWithRef(
{
baseContainer,
defaultValue,
// eslint-disable-next-line @typescript-eslint/naming-convention
disabled = false,
Expand Down Expand Up @@ -90,19 +93,6 @@ function TimeInputWithRef(
setIsFocused(false)
}, [])

const handleClickOutside = useCallback(
(event: globalThis.MouseEvent) => {
const target = event.target as Node | null

if (hourInputRef.current.contains(target) || minuteInputRef.current.contains(target)) {
return
}

closeRangedTimePicker()
},
[closeRangedTimePicker]
)

const handleFocus = useCallback(() => {
setIsFocused(true)

Expand Down Expand Up @@ -139,14 +129,6 @@ function TimeInputWithRef(
forceUpdate()
}, [forceUpdate])

useEffect(() => {
window.document.addEventListener('click', handleClickOutside)

return () => {
window.document.removeEventListener('click', handleClickOutside)
}
}, [handleClickOutside])

const submit = useCallback(() => {
setHasValidationError(false)

Expand All @@ -168,6 +150,8 @@ function TimeInputWithRef(
onChange(nextTimeTuple)
}, [closeRangedTimePicker, onChange])

useClickOutside(boxRef, closeRangedTimePicker, baseContainer)

return (
<Box
ref={boxRef}
Expand Down
2 changes: 2 additions & 0 deletions src/fields/DateRangePicker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ export function DateRangePicker({
<Field isTimeField>
<TimeInput
ref={startTimeInputRef}
baseContainer={baseContainer}
defaultValue={selectedStartTimeTupleRef.current}
disabled={disabled}
isCompact={isCompact}
Expand Down Expand Up @@ -335,6 +336,7 @@ export function DateRangePicker({
<Field isTimeField>
<TimeInput
ref={endTimeInputRef}
baseContainer={baseContainer}
defaultValue={selectedEndTimeTupleRef.current}
disabled={disabled}
isCompact={isCompact}
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useClickOutside.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const useClickOutside = (
baseContainer?: Document | HTMLDivElement | null
) => {
const handleClickOutside = useCallback(
(event: Event) => {
(event: MouseEvent) => {
const eventTarget = event.target as Node | null
const zoneRefs = Array.isArray(zoneRefOrzoneRefs) ? zoneRefOrzoneRefs : [zoneRefOrzoneRefs]

Expand All @@ -31,10 +31,10 @@ export const useClickOutside = (
useEffect(() => {
const globalContainer = baseContainer ?? window.document

globalContainer.addEventListener('click', handleClickOutside)
globalContainer.addEventListener('click', handleClickOutside as any)

return () => {
globalContainer.removeEventListener('click', handleClickOutside)
globalContainer.removeEventListener('click', handleClickOutside as any)
}
}, [baseContainer, handleClickOutside])
}

0 comments on commit afa0a13

Please sign in to comment.