Skip to content

Commit

Permalink
fix(DatePicker): Fix compatibility with strict mode
Browse files Browse the repository at this point in the history
This stops using the onDeactivate callback in focus-trap due to its incompatibility with strict mode: https://github.com/focus-trap/focus-trap-react?tab=readme-ov-file#%EF%B8%8F%EF%B8%8F-react-18-strict-mode-%EF%B8%8F%EF%B8%8F

Instead, the click and escape callbacks are used.

Additionally, the DATEPICKER_ACTION.TOGGLE_OPEN reducer action was removed as the reduce functions are called twice when strict mode is enabled, so that action doesn't work as expected.

Strict mode hasn't been enabled in Storybook as there seemed to be some unrelated end-to-end tests still failing with it enabled.
  • Loading branch information
jpveooys committed Nov 22, 2024
1 parent 8adb5ac commit effb893
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,15 @@ export const Input = memo(

const handleInputClick = useCallback(() => {
if (isRange) {
dispatch({ type: DATEPICKER_ACTION.TOGGLE_OPEN })
dispatch({
type: DATEPICKER_ACTION.UPDATE,
data: {
calendarTableVariant: CALENDAR_TABLE_VARIANT.HIDE,
isOpen: !isOpen,
},
})
}
}, [isRange, dispatch])
}, [isOpen, isRange, dispatch])

const handleShowHideClick = useCallback(() => {
dispatch({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export const DATEPICKER_ACTION = {
UPDATE: 'UPDATE',
REFRESH_HAS_ERROR: 'REFRESH_HAS_ERROR',
REFRESH_INPUT_VALUE: 'REFRESH_INPUT_VALUE',
TOGGLE_OPEN: 'TOGGLE_OPEN',
} as const

interface ResetAction {
Expand All @@ -39,11 +38,6 @@ interface RefreshInputValueAction {
data?: never
}

interface ToggleOpenAction {
type: typeof DATEPICKER_ACTION.TOGGLE_OPEN
data?: never
}

export type DatePickerAction =
| ResetAction
| UpdateAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ function reducer(
state.datePickerFormat
),
}
case DATEPICKER_ACTION.TOGGLE_OPEN:
return {
...state,
isOpen: !state.isOpen,
}
default:
throw new Error('Unknown reducer action type')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,21 @@ export function useFocusTrapOptions(
() => ({
allowOutsideClick: (event) =>
isEventTargetDescendantOf(event, clickAllowedElementRefs),
clickOutsideDeactivates: (event) =>
!isEventTargetDescendantOf(event, clickAllowedElementRefs),
clickOutsideDeactivates: (event) => {
const shouldDeactivate = !isEventTargetDescendantOf(
event,
clickAllowedElementRefs
)
if (shouldDeactivate) {
close()
}
return shouldDeactivate
},
escapeDeactivates: () => {
close()
return true
},
initialFocus: false,
onDeactivate: close,
}),
[clickAllowedElementRefs, close]
)
Expand Down

0 comments on commit effb893

Please sign in to comment.