-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add typings for hooks #3125
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import { render, screen } from '@testing-library/react'; | |
import userEvent from '@testing-library/user-event'; | ||
|
||
import { useToggle } from '../..'; | ||
import { ToggleHandlers } from '../useToggle'; | ||
|
||
const TOGGLE_IS_ON = 'on'; | ||
const TOGGLE_IS_OFF = 'off'; | ||
|
@@ -19,7 +20,7 @@ const resetHandlerMocks = () => { | |
}; | ||
|
||
// eslint-disable-next-line react/prop-types | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit/unrelated suggestion: could opt to disable prop-types rule for |
||
function FakeComponent({ defaultIsOn, handlers }) { | ||
function FakeComponent({ defaultIsOn, handlers }: { defaultIsOn: boolean, handlers: ToggleHandlers }) { | ||
const [isOn, setOn, setOff, toggle] = useToggle(defaultIsOn, handlers); | ||
|
||
return ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,21 @@ import { useRef, useEffect } from 'react'; | |
* A React hook to enable arrow key navigation on a component. | ||
*/ | ||
|
||
function handleEnter({ event, currentIndex, activeElement }) { | ||
function handleEnter( | ||
{ event, currentIndex, activeElement }: { event: KeyboardEvent, currentIndex: number, activeElement: HTMLElement }, | ||
) { | ||
Comment on lines
+7
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. curious/suggestion: would it improve readability/understandability of these types if these function args and the return data structure were abstracted into an interface HandleEnterOptions {
event: KeyboardEvent;
currentIndex: number;
activeElement: HTMLElement;
}
function handleEnter({ event, currentIndex, activeElement }: HandleEnterOptions) { ... } Similar question for |
||
if (currentIndex === -1) { return; } | ||
activeElement.click(); | ||
event.preventDefault(); | ||
} | ||
|
||
function handleArrowKey({ event, currentIndex, availableElements }) { | ||
function handleArrowKey( | ||
{ event, currentIndex, availableElements }: { | ||
event: KeyboardEvent, | ||
currentIndex: number, | ||
availableElements: NodeListOf<HTMLElement>, | ||
}, | ||
) { | ||
// If the focus isn't in the container, focus on the first thing | ||
if (currentIndex === -1) { availableElements[0].focus(); } | ||
|
||
|
@@ -32,8 +40,7 @@ function handleArrowKey({ event, currentIndex, availableElements }) { | |
[nextElement] = availableElements; | ||
} | ||
|
||
// eslint-disable-next-line no-unused-expressions | ||
nextElement && nextElement.focus(); | ||
nextElement?.focus(); | ||
event.preventDefault(); | ||
} | ||
|
||
|
@@ -45,7 +52,7 @@ function handleEvents({ | |
ignoredKeys = [], | ||
parentNode, | ||
selectors = 'a,button,input', | ||
}) { | ||
}: { event: KeyboardEvent, ignoredKeys?: string[], parentNode: HTMLElement | undefined, selectors?: string }) { | ||
if (!parentNode) { return; } | ||
|
||
const { key } = event; | ||
|
@@ -61,7 +68,7 @@ function handleEvents({ | |
if (!parentNode.contains(activeElement)) { return; } | ||
|
||
// Get the list of elements we're allowed to scroll through | ||
const availableElements = parentNode.querySelectorAll(selectors); | ||
const availableElements = parentNode.querySelectorAll<HTMLElement>(selectors); | ||
|
||
// No elements are available to loop through. | ||
if (!availableElements.length) { return; } | ||
|
@@ -71,18 +78,24 @@ function handleEvents({ | |
(availableElement) => availableElement === activeElement, | ||
); | ||
|
||
if (key === 'Enter') { | ||
handleEnter({ event, currentIndex, activeElement }); | ||
if (key === 'Enter' && activeElement) { | ||
handleEnter({ event, currentIndex, activeElement: activeElement as HTMLElement }); | ||
} | ||
handleArrowKey({ event, currentIndex, availableElements }); | ||
} | ||
|
||
export default function useArrowKeyNavigation(props) { | ||
const { selectors, ignoredKeys } = props || {}; | ||
const parentNode = useRef(); | ||
export interface ArrowKeyNavProps { | ||
/** e.g. 'a,button,input' */ | ||
selectors?: string; | ||
ignoredKeys?: string[]; | ||
} | ||
|
||
export default function useArrowKeyNavigation(props: ArrowKeyNavProps = {}) { | ||
const { selectors, ignoredKeys } = props; | ||
const parentNode = useRef<HTMLElement>(); | ||
|
||
useEffect(() => { | ||
const eventHandler = (event) => { | ||
const eventHandler = (event: KeyboardEvent) => { | ||
handleEvents({ | ||
event, ignoredKeys, parentNode: parentNode.current, selectors, | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,25 +5,32 @@ import { useLayoutEffect, useState } from 'react'; | |
* that fits within its bounding rectangle. This is done by summing the widths | ||
* of the children until they exceed the width of the container. | ||
* | ||
* @param {Element} containerElementRef - container element | ||
* @param {Element} overflowElementRef - overflow element | ||
* | ||
* The hook returns the index of the last visible child. | ||
* | ||
* @param containerElementRef - container element | ||
* @param overflowElementRef - overflow element | ||
*/ | ||
const useIndexOfLastVisibleChild = (containerElementRef, overflowElementRef) => { | ||
const useIndexOfLastVisibleChild = ( | ||
containerElementRef: Element | null, | ||
overflowElementRef: Element | null, | ||
Comment on lines
+14
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit/clarification: should these |
||
): number => { | ||
const [indexOfLastVisibleChild, setIndexOfLastVisibleChild] = useState(-1); | ||
|
||
useLayoutEffect(() => { | ||
if (!containerElementRef) { | ||
return undefined; | ||
} | ||
|
||
function updateLastVisibleChildIndex() { | ||
// Get array of child nodes from NodeList form | ||
const childNodesArr = Array.prototype.slice.call(containerElementRef.children); | ||
const childNodesArr = Array.prototype.slice.call(containerElementRef!.children); | ||
const { nextIndexOfLastVisibleChild } = childNodesArr | ||
// filter out the overflow element | ||
.filter(childNode => childNode !== overflowElementRef) | ||
// sum the widths to find the last visible element's index | ||
.reduce((acc, childNode, index) => { | ||
acc.sumWidth += childNode.getBoundingClientRect().width; | ||
if (acc.sumWidth <= containerElementRef.getBoundingClientRect().width) { | ||
if (acc.sumWidth <= containerElementRef!.getBoundingClientRect().width) { | ||
acc.nextIndexOfLastVisibleChild = index; | ||
} | ||
return acc; | ||
|
@@ -32,23 +39,18 @@ const useIndexOfLastVisibleChild = (containerElementRef, overflowElementRef) => | |
// sometimes we'll show a dropdown with one item in it when it would fit, | ||
// but allowing this case dramatically simplifies the calculations we need | ||
// to do above. | ||
sumWidth: overflowElementRef ? overflowElementRef.getBoundingClientRect().width : 0, | ||
sumWidth: overflowElementRef?.getBoundingClientRect().width ?? 0, | ||
nextIndexOfLastVisibleChild: -1, | ||
}); | ||
|
||
setIndexOfLastVisibleChild(nextIndexOfLastVisibleChild); | ||
} | ||
|
||
if (containerElementRef) { | ||
updateLastVisibleChildIndex(); | ||
|
||
const resizeObserver = new ResizeObserver(() => updateLastVisibleChildIndex()); | ||
resizeObserver.observe(containerElementRef); | ||
|
||
return () => resizeObserver.disconnect(); | ||
} | ||
updateLastVisibleChildIndex(); | ||
|
||
return undefined; | ||
const resizeObserver = new ResizeObserver(() => updateLastVisibleChildIndex()); | ||
resizeObserver.observe(containerElementRef); | ||
return () => resizeObserver.disconnect(); | ||
}, [containerElementRef, overflowElementRef]); | ||
|
||
return indexOfLastVisibleChild; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import { useRef, useState, useEffect } from 'react'; | ||
import React, { useRef, useState, useEffect } from 'react'; | ||
|
||
const useIsVisible = (defaultIsVisible = true) => { | ||
const sentinelRef = useRef(); | ||
const useIsVisible = (defaultIsVisible = true): [ | ||
isVisible: boolean, | ||
sentinelRef: React.MutableRefObject<HTMLDivElement | null>, | ||
] => { | ||
const sentinelRef = useRef<HTMLDivElement | null>(null); | ||
const [isVisible, setIsVisible] = useState(defaultIsVisible); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [clarification] Will this |
||
|
||
useEffect(() => { | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useState, useCallback } from 'react'; | ||
|
||
export type Toggler = [ | ||
isOn: boolean, | ||
setOn: () => void, | ||
setOff: () => void, | ||
toggle: () => void, | ||
]; | ||
|
||
export interface ToggleHandlers { | ||
handleToggleOn?: () => void; | ||
handleToggleOff?: () => void; | ||
handleToggle?: (newStatus: boolean) => void; | ||
} | ||
|
||
export default function useToggle(defaultIsOn = false, handlers: ToggleHandlers = {}): Toggler { | ||
const { handleToggleOn, handleToggleOff, handleToggle } = handlers; | ||
const [isOn, setIsOn] = useState(defaultIsOn); | ||
|
||
const setOn = useCallback(() => { | ||
setIsOn(true); | ||
handleToggleOn?.(); | ||
handleToggle?.(true); | ||
}, [handleToggleOn, handleToggle]); | ||
|
||
const setOff = useCallback(() => { | ||
setIsOn(false); | ||
handleToggleOff?.(); | ||
handleToggle?.(false); | ||
}, [handleToggleOff, handleToggle]); | ||
|
||
const toggle = useCallback(() => { | ||
const doToggle = isOn ? setOff : setOn; | ||
doToggle(); | ||
}, [isOn, setOn, setOff]); | ||
|
||
return [isOn, setOn, setOff, toggle]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit/question: should both refs get the same type with
HTMLDivElement
, given both refs are applied todivs
below?