Skip to content

Commit

Permalink
feat(DatePickerE): Focus open/close button when day picker is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
jpveooys committed Dec 7, 2021
1 parent 4771a5e commit 86d4a65
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,12 @@ describe('DatePickerE', () => {
expect(wrapper.queryByTestId('floating-box')).not.toBeInTheDocument()
})
})

it('focuses the open/close button', () => {
return waitFor(() =>
expect(wrapper.queryByTestId('datepicker-input-button')).toHaveFocus()
)
})
})

describe('when the next month button is clicked', () => {
Expand Down Expand Up @@ -659,6 +665,14 @@ describe('DatePickerE', () => {
})
})

it('focuses the open/close button', () => {
return waitFor(() =>
expect(
wrapper.queryByTestId('datepicker-input-button')
).toHaveFocus()
)
})

describe('when the picker is reopened', () => {
beforeEach(async () => {
await waitForElementToBeRemoved(
Expand Down Expand Up @@ -781,6 +795,14 @@ describe('DatePickerE', () => {
})
})

it('focuses the open/close button', () => {
return waitFor(() =>
expect(
wrapper.queryByTestId('datepicker-input-button')
).toHaveFocus()
)
})

it('set the value of the component to this date', () => {
expect(
wrapper.getByTestId('datepicker-input').getAttribute('value')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ export function useDatePickerEOpenClose(isOpen = false): {
} {
const { open, handleOnClose, handleOnFocus } = useOpenClose(isOpen)
const floatingBoxChildrenRef = useRef()
const inputButtonRef = useRef()
const inputButtonRef = useRef<HTMLButtonElement>()
const inputRef = useRef()

const handleDatePickerOnClose = useCallback(() => {
if (!open) {
return
}

handleOnClose(null)
}, [handleOnClose])
inputButtonRef.current?.focus()
}, [handleOnClose, open])

useDocumentClick(
[floatingBoxChildrenRef, inputButtonRef, inputRef],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ const NODE_CURRENT = {
* to false if the hook needs to be enabled or disabled dynamically
*/
export function useDocumentClick(
nodes:
| React.MutableRefObject<undefined>
| React.MutableRefObject<undefined>[],
nodes: React.MutableRefObject<Element> | React.MutableRefObject<Element>[],
onDocumentClick: (event: Event) => void,
isEnabled = true
) {
Expand All @@ -31,9 +29,9 @@ export function useDocumentClick(

const nonClickableRef = find(
nonClickableRefs,
(node: React.MutableRefObject<undefined>) => {
(node: React.MutableRefObject<Element>) => {
const current = node.current || NODE_CURRENT
return current.contains(event.target)
return current.contains(event.target as Element)
}
)

Expand Down

0 comments on commit 86d4a65

Please sign in to comment.