diff --git a/src/devtools/views/Element.js b/src/devtools/views/Element.js index f3e0104fe7..7e92ad3013 100644 --- a/src/devtools/views/Element.js +++ b/src/devtools/views/Element.js @@ -4,8 +4,7 @@ import React, { Fragment, useCallback, useContext, useMemo } from 'react'; import { ElementTypeClassOrFunction } from 'src/devtools/types'; import { TreeContext } from './context'; import { createRegExp } from './utils'; -import { SearchContext } from './SearchContext'; -import { SelectedElementContext } from './SelectedElementContext'; +import { SearchAndSelectionContext } from './SearchAndSelectionContext'; import Icon from './Icon'; import styles from './Element.css'; @@ -31,15 +30,15 @@ export default function Element({ index, style }: Props) { const { children, depth, displayName, id, key, type } = element; - const selectedElement = useContext(SelectedElementContext); + const { selectedElementID, selectElementWithID } = useContext( + SearchAndSelectionContext + ); const handleClick = useCallback( - ({ metaKey }) => { - selectedElement.id = metaKey ? null : id; - }, + ({ metaKey }) => selectElementWithID(metaKey ? null : id), [id] ); - const isSelected = selectedElement.id === id; + const isSelected = selectedElementID === id; const showDollarR = isSelected && type === ElementTypeClassOrFunction; return ( @@ -77,16 +76,14 @@ type DisplayNameProps = {| |}; function DisplayName({ displayName, id }: DisplayNameProps) { - const { - currentIndex: currentSearchIndex, - ids: searchIDs, - text: searchText, - } = useContext(SearchContext); + const { searchIndex, searchResults, searchText } = useContext( + SearchAndSelectionContext + ); const isSearchResult = useMemo(() => { - return searchIDs.includes(id); - }, [id, searchIDs]); + return searchResults.includes(id); + }, [id, searchResults]); const isCurrentResult = - currentSearchIndex !== null && id === searchIDs[currentSearchIndex]; + searchIndex !== null && id === searchResults[searchIndex]; if (!isSearchResult || displayName === null) { return displayName; diff --git a/src/devtools/views/Elements.js b/src/devtools/views/Elements.js index 2079dbc62c..116761969a 100644 --- a/src/devtools/views/Elements.js +++ b/src/devtools/views/Elements.js @@ -5,8 +5,7 @@ import Store from '../store'; import Tree from './Tree'; import { BridgeContext, StoreContext, TreeContext } from './context'; import SelectedElement from './SelectedElement'; -import { SearchController } from './SearchContext'; -import { SelectedElementController } from './SelectedElementContext'; +import { SearchAndSelectionController } from './SearchAndSelectionContext'; import styles from './Elements.css'; import './root.css'; @@ -51,18 +50,16 @@ export default function Elements({ bridge, browserName, themeName }: Props) { - - -
-
- -
-
- -
+ +
+
+
- - +
+ +
+
+
diff --git a/src/devtools/views/SearchAndSelectionContext.js b/src/devtools/views/SearchAndSelectionContext.js new file mode 100644 index 0000000000..886c0a16be --- /dev/null +++ b/src/devtools/views/SearchAndSelectionContext.js @@ -0,0 +1,373 @@ +// @flow + +// This context contains both the current search state and the selected element. +// These values are combined into a single context because changes in one often impact the other. +// Combining them enables us to avoid cascading renders. + +import React, { + createContext, + useCallback, + useContext, + useLayoutEffect, + useMemo, + useReducer, +} from 'react'; +import { createRegExp } from './utils'; +import { StoreContext } from './context'; +import Store from '../store'; + +import type { Element } from 'src/devtools/types'; + +type Context = {| + // Read current context values: + searchIndex: number | null, + searchResults: Array, + searchText: string, + selectedElementID: number | null, + selectedElementIndex: number | null, + + // Update current context values: + decrementSearchIndex(): void, + incrementSearchIndex(): void, + selectElementWithID(id: number | null): void, + selectElementAtIndex(index: number | null): void, + updateSearchText(searchText: string): void, +|}; + +const SearchAndSelectionContext = createContext( + ((null: any): Context) +); +// $FlowFixMe displayName is a valid attribute of React$ConsearchText +SearchAndSelectionContext.displayName = 'SearchAndSelectionContext'; + +type State = {| + searchIndex: number | null, + searchResults: Array, + searchText: string, + selectedElementID: number | null, + selectedElementIndex: number | null, +|}; + +type Action = {| + type: + | 'DECREMENT_SEARCH_INDEX' + | 'INCREMENT_SEARCH_INDEX' + | 'REFINE_SEARCH_RESULTS' + | 'SELECT_ELEMENT_WITH_ID' + | 'SELECT_ELEMENT_AT_INDEX' + | 'UPDATE_SEARCH_TEXT', + payload?: any, +|}; + +type Props = {| + children: React$Node, +|}; + +// TODO Remove this wrapper element once global ConsearchText.write API exists. +function SearchAndSelectionController({ children }: Props) { + const store = useContext(StoreContext); + + // This reducer is created inline because it needs access to the Store. + // The store is mutable, but the Store itself is global and lives for the lifetime of the DevTools, + // so it's okay for the reducer to have an empty dependencies array. + const reducer = useMemo( + () => + function reducer(state: State, action: Action): State { + const { searchIndex, searchResults, searchText } = state; + const { payload, type } = action; + + let newSearchIndex, + newSearchResults, + newSearchText, + newSelectedElementID; + + switch (type) { + case 'DECREMENT_SEARCH_INDEX': + if (searchResults.length === 0) return state; + + newSearchIndex = + ((searchIndex: any): number) > 0 + ? ((searchIndex: any): number) - 1 + : searchResults.length - 1; + newSelectedElementID = searchResults[newSearchIndex]; + + return { + ...state, + searchIndex: newSearchIndex, + selectedElementID: newSelectedElementID, + selectedElementIndex: store.getIndexOfElementID( + newSelectedElementID + ), + }; + + case 'INCREMENT_SEARCH_INDEX': + if (searchResults.length === 0) return state; + + newSearchIndex = + ((searchIndex: any): number) + 1 < searchResults.length + ? ((searchIndex: any): number) + 1 + : 0; + newSelectedElementID = searchResults[newSearchIndex]; + + return { + ...state, + searchIndex: newSearchIndex, + selectedElementID: newSelectedElementID, + selectedElementIndex: store.getIndexOfElementID( + newSelectedElementID + ), + }; + + case 'REFINE_SEARCH_RESULTS': + const [ + addedElementIDs, + removedElementIDs, + ] = ((payload: any): Array); + + newSearchResults = searchResults; + newSearchIndex = searchIndex; + + removedElementIDs.forEach(id => { + // Prune this item from the search results. + const index = newSearchResults.indexOf(id); + if (index >= 0) { + newSearchResults = newSearchResults + .slice(0, index) + .concat(newSearchResults.slice(index + 1)); + + // If the results are now empty, also deselect things. + if (newSearchResults.length === 0) { + newSearchIndex = null; + } else if ( + ((newSearchIndex: any): number) >= newSearchResults.length + ) { + newSearchIndex = newSearchResults.length - 1; + } + } + }); + + addedElementIDs.forEach(id => { + const { displayName } = ((store.getElementByID( + id + ): any): Element); + + // Add this item to the search results if it matches. + const regExp = createRegExp(searchText); + if (displayName !== null && regExp.test(displayName)) { + const newElementIndex = ((store.getIndexOfElementID( + id + ): any): number); + + let foundMatch = false; + for (let index = 0; index < newSearchResults.length; index++) { + const id = newSearchResults[index]; + if ( + newElementIndex < + ((store.getIndexOfElementID(id): any): number) + ) { + foundMatch = true; + newSearchResults = newSearchResults + .slice(0, index) + .concat(id) + .concat(newSearchResults.slice(index)); + break; + } + } + if (!foundMatch) { + newSearchResults = newSearchResults.concat(id); + } + + newSearchIndex = newSearchIndex === null ? 0 : newSearchIndex; + } + }); + + newSelectedElementID = + newSearchIndex !== null ? newSearchResults[newSearchIndex] : null; + + return { + ...state, + searchIndex: newSearchIndex, + searchResults: newSearchResults, + selectedElementID: newSelectedElementID, + selectedElementIndex: + newSelectedElementID !== null + ? store.getIndexOfElementID(newSelectedElementID) + : null, + }; + + case 'SELECT_ELEMENT_WITH_ID': + newSelectedElementID = ((payload: any): number | null); + + return { + ...state, + selectedElementID: newSelectedElementID, + selectedElementIndex: + newSelectedElementID !== null + ? store.getIndexOfElementID(newSelectedElementID) + : null, + }; + + case 'SELECT_ELEMENT_AT_INDEX': + newSearchIndex = ((payload: any): number | null); + + const element = + newSearchIndex !== null + ? store.getElementAtIndex(newSearchIndex) + : null; + + return { + ...state, + selectedElementID: element !== null ? element.id : null, + selectedElementIndex: newSearchIndex, + }; + + case 'UPDATE_SEARCH_TEXT': + newSearchIndex = searchIndex; + newSearchResults = []; + newSearchText = ((payload: any): string); + + // Find all matching elements. + if (newSearchText !== '') { + const regExp = createRegExp(newSearchText); + store.roots.forEach(rootID => { + recursivelySearchTree(store, rootID, regExp, newSearchResults); + }); + + // If this is a refinement of a previous search, preserve the current index (unless it's no longer valid). + // If it's a new search, reset the index. + if (newSearchResults.length === 0) { + newSearchIndex = null; + } else if ( + searchIndex !== null && + newSearchText.startsWith(searchText) + ) { + newSearchIndex = Math.min( + searchIndex, + newSearchResults.length - 1 + ); + } else { + newSearchIndex = 0; + } + } else { + newSearchIndex = null; + } + + newSelectedElementID = + newSearchIndex !== null ? newSearchResults[newSearchIndex] : null; + + return { + ...state, + searchIndex: newSearchIndex, + searchResults: newSearchResults, + searchText: newSearchText, + selectedElementID: newSelectedElementID, + selectedElementIndex: + newSelectedElementID !== null + ? store.getIndexOfElementID(newSelectedElementID) + : null, + }; + + default: + throw new Error(`Unrecognized action "${type}"`); + } + }, + [] + ); + + const [state, dispatch] = useReducer(reducer, { + searchIndex: null, + searchResults: [], + searchText: '', + selectedElementID: null, + selectedElementIndex: null, + }); + + const decrementSearchIndex = useCallback( + () => dispatch({ type: 'DECREMENT_SEARCH_INDEX' }), + [dispatch] + ); + const incrementSearchIndex = useCallback( + () => dispatch({ type: 'INCREMENT_SEARCH_INDEX' }), + [dispatch] + ); + const selectElementWithID = useCallback( + (id: number | null) => + dispatch({ type: 'SELECT_ELEMENT_WITH_ID', payload: id }), + [dispatch] + ); + const selectElementAtIndex = useCallback( + (index: number | null) => + dispatch({ type: 'SELECT_ELEMENT_AT_INDEX', payload: index }), + [dispatch] + ); + const updateSearchText = useCallback( + (searchText: string) => + dispatch({ type: 'UPDATE_SEARCH_TEXT', payload: searchText }), + [dispatch] + ); + + const value = useMemo( + () => ({ + searchIndex: state.searchIndex, + searchResults: state.searchResults, + searchText: state.searchText, + selectedElementID: state.selectedElementID, + selectedElementIndex: state.selectedElementIndex, + + decrementSearchIndex, + incrementSearchIndex, + selectElementWithID, + selectElementAtIndex, + updateSearchText, + }), + [state] + ); + + // Listen for changes to the tree and incrementally adjust the search results. + useLayoutEffect(() => { + const handleStoreMutated = ([ + addedElementIDs, + removedElementIDs, + ]: Array) => { + if (!state.searchText) { + return; + } + + dispatch({ + type: 'REFINE_SEARCH_RESULTS', + payload: [addedElementIDs, removedElementIDs], + }); + }; + + store.addListener('mutated', handleStoreMutated); + + return () => store.removeListener('mutated', handleStoreMutated); + }, [state, store]); + + return ( + + {children} + + ); +} + +function recursivelySearchTree( + store: Store, + elementID: number, + regExp: RegExp, + searchResults: Array +): void { + const { children, displayName } = ((store.getElementByID( + elementID + ): any): Element); + if (displayName !== null) { + if (regExp.test(displayName)) { + searchResults.push(elementID); + } + } + children.forEach(childID => + recursivelySearchTree(store, childID, regExp, searchResults) + ); +} + +export { SearchAndSelectionContext, SearchAndSelectionController }; diff --git a/src/devtools/views/SearchContext.js b/src/devtools/views/SearchContext.js deleted file mode 100644 index 2094ce2b57..0000000000 --- a/src/devtools/views/SearchContext.js +++ /dev/null @@ -1,241 +0,0 @@ -// @flow - -import React, { - createContext, - useContext, - useLayoutEffect, - useMemo, - useReducer, -} from 'react'; -import { createRegExp } from './utils'; -import { StoreContext } from './context'; -import { SelectedElementContext } from './SelectedElementContext'; -import Store from '../store'; - -import type { Element } from 'src/devtools/types'; - -export type SearchContextValue = {| - get currentIndex(): number | null, - get ids(): Array, - get text(): string, - updateCurrentIndex(newCurrentIndex: number): void, - updateText(newText: string): void, -|}; - -const SearchContext = createContext( - ((null: any): SearchContextValue) -); -// $FlowFixMe displayName is a valid attribute of React$Context -SearchContext.displayName = 'SearchContext'; - -type State = {| - currentIndex: number | null, - ids: Array, - text: string, -|}; - -type Action = {| - type: 'UPDATE', - payload: $Shape, -|}; - -function reducer(state: State, action: Action) { - switch (action.type) { - case 'UPDATE': - return { - ...state, - ...action.payload, - }; - default: - throw new Error(); - } -} - -type Props = {| - children: React$Node, -|}; - -// TODO Remove this wrapper element once global Context.write API exists. -function SearchController({ children }: Props) { - const store = useContext(StoreContext); - const selectedElement = useContext(SelectedElementContext); - - const [state, dispatch] = useReducer(reducer, { - currentIndex: null, - ids: [], - text: '', - }); - - const value = useMemo( - () => ({ - get currentIndex(): number | null { - return state.currentIndex; - }, - - get ids(): Array { - return state.ids; - }, - - get text(): string { - return state.text; - }, - - updateCurrentIndex(newCurrentIndex: number) { - dispatch({ - type: 'UPDATE', - payload: { - currentIndex: newCurrentIndex, - }, - }); - - // Make sure a new search result is also selected. - selectedElement.id = - newCurrentIndex !== null ? state.ids[newCurrentIndex] : null; - }, - - updateText(newText: string) { - const { currentIndex: oldCurrentIndex, text: oldText } = state; - - let newCurrentIndex = oldCurrentIndex; - let newIDs = []; - - // Find all matching elements. - if (newText !== '') { - let regExp = createRegExp(newText); - store.roots.forEach(rootID => { - searchTree(store, rootID, regExp, newIDs); - }); - - // If this is a refinement of a previous search, preserve the current index (unless it's no longer valid). - // If it's a new search, reset the index. - if (newIDs.length === 0) { - newCurrentIndex = null; - } else if (oldCurrentIndex !== null && newText.startsWith(oldText)) { - newCurrentIndex = Math.min(oldCurrentIndex, newIDs.length - 1); - } else { - newCurrentIndex = 0; - } - } else { - newCurrentIndex = null; - } - - dispatch({ - type: 'UPDATE', - payload: { - currentIndex: newCurrentIndex, - ids: newIDs, - text: newText, - }, - }); - - // Make sure a new search result is also selected. - selectedElement.id = - newCurrentIndex !== null ? newIDs[newCurrentIndex] : null; - }, - }), - [state] - ); - - // Listen for changes to the tree and incrementally adjust the search results. - useLayoutEffect(() => { - const handleStoreMutated = ([ - addedElementIDs, - removedElementIDs, - ]: Array) => { - const { currentIndex, ids, text } = state; - - if (!text) { - return; - } - - let newIDs = ids; - let newCurrentIndex = currentIndex; - - removedElementIDs.forEach(id => { - // Prune this item from the search results. - const index = newIDs.indexOf(id); - if (index >= 0) { - newIDs = newIDs.slice(0, index).concat(newIDs.slice(index + 1)); - - // If the results are now empty, also deselect things. - if (newIDs.length === 0) { - newCurrentIndex = null; - } else if (((newCurrentIndex: any): number) >= newIDs.length) { - newCurrentIndex = newIDs.length - 1; - } - } - }); - - addedElementIDs.forEach(id => { - const { displayName } = ((store.getElementByID(id): any): Element); - - // Add this item to the search results if it matches. - const regExp = createRegExp(text); - if (displayName !== null && regExp.test(displayName)) { - const newElementIndex = ((store.getIndexOfElementID( - id - ): any): number); - - let foundMatch = false; - for (let index = 0; index < newIDs.length; index++) { - const id = newIDs[index]; - if ( - newElementIndex < ((store.getIndexOfElementID(id): any): number) - ) { - foundMatch = true; - newIDs = newIDs - .slice(0, index) - .concat(id) - .concat(newIDs.slice(index)); - break; - } - } - if (!foundMatch) { - newIDs = newIDs.concat(id); - } - - newCurrentIndex = newCurrentIndex === null ? 0 : newCurrentIndex; - } - }); - - dispatch({ - type: 'UPDATE', - payload: { - currentIndex: newCurrentIndex, - ids: newIDs, - }, - }); - - // Make sure a new search result is also selected. - selectedElement.id = - newCurrentIndex !== null ? newIDs[newCurrentIndex] : null; - }; - - store.addListener('mutated', handleStoreMutated); - - return () => store.removeListener('mutated', handleStoreMutated); - }, [state, store]); - - return ( - {children} - ); -} - -function searchTree( - store: Store, - elementID: number, - regExp: RegExp, - ids: Array -): void { - const { children, displayName } = ((store.getElementByID( - elementID - ): any): Element); - if (displayName !== null) { - if (regExp.test(displayName)) { - ids.push(elementID); - } - } - children.forEach(childID => searchTree(store, childID, regExp, ids)); -} - -export { SearchContext, SearchController }; diff --git a/src/devtools/views/SearchInput.js b/src/devtools/views/SearchInput.js index 330f257aba..24d3e1453c 100644 --- a/src/devtools/views/SearchInput.js +++ b/src/devtools/views/SearchInput.js @@ -1,7 +1,7 @@ // @flow import React, { useCallback, useContext, useEffect, useRef } from 'react'; -import { SearchContext } from './SearchContext'; +import { SearchAndSelectionContext } from './SearchAndSelectionContext'; import ButtonIcon from './ButtonIcon'; import styles from './SearchInput.css'; @@ -9,50 +9,33 @@ import styles from './SearchInput.css'; type Props = {||}; export default function SearchInput(props: Props) { - // We use the wrapper searchContext object rather than its individual parts– - // (specifically for the useCallback input arrays below)– - // because a change in any one value (e.g. ids array) impacts other values (e.g. currentIndex). - // It's easier to avoid stale scope issues if we depend on the entire search state. - const searchContext = useContext(SearchContext); + const { + decrementSearchIndex, + incrementSearchIndex, + searchIndex, + searchResults, + searchText, + updateSearchText, + } = useContext(SearchAndSelectionContext); const inputRef = useRef(); - const handleTextChange = useCallback(({ currentTarget }) => { - searchContext.updateText(currentTarget.value); - }); - - const selectNext = useCallback(() => { - const { currentIndex } = searchContext; - if (currentIndex !== null) { - searchContext.updateCurrentIndex(currentIndex + 1); - } - }, [searchContext]); - - const selectPrevious = useCallback(() => { - const { currentIndex } = searchContext; - if (currentIndex !== null) { - searchContext.updateCurrentIndex(currentIndex - 1); - } - }, [searchContext]); + const handleTextChange = useCallback( + ({ currentTarget }) => updateSearchText(currentTarget.value), + [updateSearchText] + ); const resetSearch = useCallback(() => { - searchContext.updateText(''); - }, [searchContext]); + updateSearchText(''); + }, [updateSearchText]); const handleInputKeyPress = useCallback( ({ key }) => { if (key === 'Enter') { - const { currentIndex, ids } = searchContext; - if (currentIndex !== null) { - if (currentIndex + 1 < ids.length) { - searchContext.updateCurrentIndex(currentIndex + 1); - } else { - searchContext.updateCurrentIndex(0); - } - } + incrementSearchIndex(); } }, - [searchContext] + [incrementSearchIndex] ); // Auto-focus search input @@ -72,8 +55,6 @@ export default function SearchInput(props: Props) { return () => window.removeEventListener('keydown', handleWindowKeyDown); }, [inputRef]); - const { currentIndex, ids, text } = searchContext; - return (
- {!!text && ( + {!!searchText && ( - {Math.min(currentIndex + 1, ids.length)} | {ids.length} + {Math.min(searchIndex + 1, searchResults.length)} |{' '} + {searchResults.length} )}
); @@ -76,13 +72,11 @@ export default function SelectedElement(_: Props) { type InspectedElementViewProps = {| inspectedElement: InspectedElement, - selectedElement: SelectedElementContextValue, |}; -function InspectedElementView({ - inspectedElement, - selectedElement, -}: InspectedElementViewProps) { +function InspectedElementView({ inspectedElement }: InspectedElementViewProps) { + const { selectElementWithID } = useContext(SearchAndSelectionContext); + let { context, hooks, owners, props, state } = inspectedElement; return ( @@ -99,9 +93,7 @@ function InspectedElementView({
{ - selectedElement.id = owner.id; - }} + onClick={() => selectElementWithID(owner.id)} title={owner.displayName} > {owner.displayName} diff --git a/src/devtools/views/SelectedElementContext.js b/src/devtools/views/SelectedElementContext.js deleted file mode 100644 index a930cd5dd9..0000000000 --- a/src/devtools/views/SelectedElementContext.js +++ /dev/null @@ -1,56 +0,0 @@ -// @flow - -import React, { createContext, useContext, useMemo, useState } from 'react'; -import { StoreContext } from './context'; - -export type SelectedElementContextValue = {| - id: number | null, - index: number | null, -|}; - -const SelectedElementContext = createContext( - ((null: any): SelectedElementContextValue) -); -// $FlowFixMe displayName is a valid attribute of React$Context -SelectedElementContext.displayName = 'SelectedElementContext'; - -type Props = {| - children: React$Node, -|}; - -// TODO Remove this wrapper element once global Context.write API exists. -function SelectedElementController({ children }: Props) { - const store = useContext(StoreContext); - - const [id, setID] = useState(null); - const [index, setIndex] = useState(null); - - const value = useMemo( - () => ({ - get id() { - return id; - }, - set id(id: number | null) { - setID(id); - setIndex(id !== null ? store.getIndexOfElementID(id) : null); - }, - get index() { - return index; - }, - set index(index: number | null) { - const element = index !== null ? store.getElementAtIndex(index) : null; - setID(element !== null ? element.id : null); - setIndex(index); - }, - }), - [id] - ); - - return ( - - {children} - - ); -} - -export { SelectedElementContext, SelectedElementController }; diff --git a/src/devtools/views/Tree.js b/src/devtools/views/Tree.js index fb97b5f0de..5464701ad4 100644 --- a/src/devtools/views/Tree.js +++ b/src/devtools/views/Tree.js @@ -3,7 +3,7 @@ import React, { useContext, useEffect, useLayoutEffect, useRef } from 'react'; import AutoSizer from 'react-virtualized-auto-sizer'; import { FixedSizeList } from 'react-window'; -import { SelectedElementContext } from './SelectedElementContext'; +import { SearchAndSelectionContext } from './SearchAndSelectionContext'; import ButtonIcon from './ButtonIcon'; import Element from './Element'; import SearchInput from './SearchInput'; @@ -14,37 +14,37 @@ import styles from './Tree.css'; type Props = {||}; export default function Tree(props: Props) { - const selectedElementContext = useContext(SelectedElementContext); + const { selectedElementIndex, selectElementAtIndex } = useContext( + SearchAndSelectionContext + ); const treeContext = useContext(TreeContext); const listRef = useRef>(); // Make sure a newly selected element is visible in the list. // This is helpful for things like the owners list. useLayoutEffect(() => { - const { index } = selectedElementContext; - if (index !== null && listRef.current != null) { - listRef.current.scrollToItem(index); + if (selectedElementIndex !== null && listRef.current != null) { + listRef.current.scrollToItem(selectedElementIndex); } - }, [listRef, selectedElementContext]); + }, [listRef, selectedElementIndex]); // Navigate the tree with up/down arrow keys. useEffect(() => { const handleKeyDown = event => { - let index; - // eslint-disable-next-line default-case switch (event.key) { case 'ArrowDown': - index = selectedElementContext.index; - if (index !== null && index + 1 < treeContext.size) { - selectedElementContext.index = ((index: any): number) + 1; + if ( + selectedElementIndex !== null && + selectedElementIndex + 1 < treeContext.size + ) { + selectElementAtIndex(((selectedElementIndex: any): number) + 1); } event.preventDefault(); break; case 'ArrowUp': - index = selectedElementContext.index; - if (index !== null && index > 0) { - selectedElementContext.index = ((index: any): number) - 1; + if (selectedElementIndex !== null && selectedElementIndex > 0) { + selectElementAtIndex(((selectedElementIndex: any): number) - 1); } event.preventDefault(); break; @@ -56,7 +56,7 @@ export default function Tree(props: Props) { return () => { window.removeEventListener('keydown', handleKeyDown); }; - }, [selectedElementContext, treeContext]); + }, [selectedElementIndex, selectElementAtIndex, treeContext]); return (