Skip to content

Commit

Permalink
chore: the use of the keyboard to navigate in the search is being tested
Browse files Browse the repository at this point in the history
  • Loading branch information
Franqsanz committed Oct 19, 2023
1 parent c85fddb commit b400af3
Showing 1 changed file with 45 additions and 6 deletions.
51 changes: 45 additions & 6 deletions src/components/forms/filters/InputSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function InputSearch({
top,
onResultClick,
}: BookSearchResultsType) {
// const containerRef = useRef<HTMLDivElement | HTMLInputElement>(null);
const containerScrollRef = useRef<HTMLUListElement>(null);
const containerRef = useRef(null);
const colorIcons = useColorModeValue('gray.700', 'gray.300');
const bgInput = useColorModeValue('white', 'black');
Expand All @@ -54,9 +54,10 @@ export function InputSearch({
const colorListBgHover = useColorModeValue('gray.300', 'gray.600');
const colorInputNotResult = useColorModeValue('gray.600', 'gray.400');
const [search, setSearch] = useState({ query: '' });
const [focusedIndex, setFocusedIndex] = useState(0);
const { query } = search;
const debouncedQuery = useDebounce(query, 500);
// const navigate = useNavigate();
const navigate = useNavigate();
let alertMessage;
let loading;

Expand All @@ -69,6 +70,31 @@ export function InputSearch({
// }
// }

function handleKeyDown(e) {
if (e.key === 'ArrowDown') {
e.preventDefault();
setFocusedIndex((prevIndex) =>
prevIndex < data.length - 1 ? prevIndex + 1 : prevIndex,
);
} else if (e.key === 'ArrowUp') {
e.preventDefault();
setFocusedIndex((prevIndex) =>
prevIndex > 0 ? prevIndex - 1 : prevIndex,
);
} else if (e.key === 'Enter') {
if (
focusedIndex !== undefined &&
focusedIndex >= 0 &&
focusedIndex < data.length
) {
// Realizar alguna acción con el elemento seleccionado
const selectedItem = data[focusedIndex];
console.log(selectedItem.pathUrl);
navigate(`/book/view/${selectedItem.pathUrl}`);
}
}
}

useOutsideClick({
ref: containerRef,
handler: () => {
Expand All @@ -89,12 +115,24 @@ export function InputSearch({
}
}

if (containerScrollRef.current) {
const focusedElement = containerScrollRef.current.children[focusedIndex];
if (focusedElement) {
focusedElement.scrollIntoView({
behavior: 'smooth',
block: 'nearest',
});
}
}

document.addEventListener('keydown', handleKeyPress);
document.addEventListener('keydown', handleKeyDown);

return () => {
document.removeEventListener('keydown', handleKeyPress);
document.removeEventListener('keydown', handleKeyDown);
};
}, [debouncedQuery, refetch]);
}, [debouncedQuery, refetch, focusedIndex]);

if (isLoading) {
loading = (
Expand Down Expand Up @@ -190,17 +228,18 @@ export function InputSearch({
top={top}
>
{loading}
<List fontSize='md'>
<List fontSize='md' ref={containerScrollRef}>
{data &&
data.map((book) => (
data.map((book, index) => (
<ListItem
key={book.id}
tabIndex={0}
textAlign='left'
mb='3'
rounded='lg'
bg={colorListBg}
// onKeyDown={handleKeyPress}
// outline={index === focusedIndex ? 'green.700' : 'black'}
// onClick={data && data[index]}
_hover={{ bg: `${colorListBgHover}` }}
>
<Link
Expand Down

1 comment on commit b400af3

@vercel
Copy link

@vercel vercel bot commented on b400af3 Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

xbu – ./

xbu-franqsanz.vercel.app
xbu.vercel.app
xbu-git-main-franqsanz.vercel.app

Please sign in to comment.