-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b76f09e
commit 3dee2dc
Showing
3 changed files
with
71 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,30 @@ | ||
import { prefix } from "../../constants"; | ||
import Row from "../Row"; | ||
import useFocus from "../../useFocus"; | ||
|
||
const List = ({ | ||
entries, | ||
data, | ||
...rest | ||
}) => ( | ||
<div className={`${prefix}__list`}> | ||
{entries.map(guid => ( | ||
<Row | ||
key={guid} | ||
{...data[guid]} | ||
{...rest} | ||
/> | ||
))} | ||
</div> | ||
); | ||
}) => { | ||
const [focus, setFocus] = useFocus(entries.length); | ||
return ( | ||
<div className={`${prefix}__list`}> | ||
{entries.map((guid, index) => { | ||
const entry = data[guid]; | ||
return ( | ||
<Row | ||
key={guid} | ||
setFocus={setFocus} | ||
index={index} | ||
focus={focus === index} | ||
{...entry} | ||
{...rest} | ||
/> | ||
) | ||
})} | ||
</div> | ||
); | ||
}; | ||
|
||
export default List; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useCallback, useState, useEffect } from "react"; | ||
|
||
const useFocus = (size) => { | ||
const [currentFocus, setCurrentFocus] = useState(0); | ||
|
||
const handleKeyDown = useCallback( | ||
e => { | ||
if (e.keyCode === 40) { // Down arrow | ||
e.preventDefault(); | ||
setCurrentFocus(currentFocus === size - 1 ? 0 : currentFocus + 1); | ||
} else if (e.keyCode === 38) { // Up arrow | ||
e.preventDefault(); | ||
setCurrentFocus(currentFocus === 0 ? size - 1 : currentFocus - 1); | ||
} | ||
}, | ||
[size, currentFocus, setCurrentFocus] | ||
); | ||
|
||
useEffect(() => { | ||
document.addEventListener("keydown", handleKeyDown, false); | ||
return () => { | ||
document.removeEventListener("keydown", handleKeyDown, false); | ||
}; | ||
}, [handleKeyDown]); | ||
|
||
return [currentFocus, setCurrentFocus]; | ||
} | ||
|
||
export default useFocus; |