Skip to content
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

[No QA] [TS migration] Migrate 'useArrowKeyFocusManager.js' hook to TypeScript #29328

Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@ import {useState, useEffect, useCallback, useMemo} from 'react';
import useKeyboardShortcut from './useKeyboardShortcut';
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe migrate useKeyboardShortcut hook in this PR as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@blazejkustra useKeyboardShortcut is migrated in this PR but its blocked by this PR

import CONST from '../CONST';

type Config = {
maxIndex: number;
onFocusedIndexChange?: (index: number) => void;
initialFocusedIndex?: number;
disabledIndexes?: readonly number[];
shouldExcludeTextAreaNodes?: boolean;
isActive?: boolean;
};

type UseArrowKeyFocusManager = [number, (index: number) => void];

/**
* A hook that makes it easy to use the arrow keys to manage focus of items in a list
*
* Recommendation: To ensure stability, wrap the `onFocusedIndexChange` function with the useCallback hook before using it with this hook.
*
* @param {Object} config
* @param {Number} config.maxIndex – typically the number of items in your list
* @param {Function} [config.onFocusedIndexChange] – optional callback to execute when focusedIndex changes
* @param {Number} [config.initialFocusedIndex] – where to start in the list
* @param {Array} [config.disabledIndexes] – An array of indexes to disable + skip over
* @param {Boolean} [config.shouldExcludeTextAreaNodes] – Whether arrow keys should have any effect when a TextArea node is focused
* @param {Boolean} [config.isActive] – Whether the component is ready and should subscribe to KeyboardShortcut
* @returns {Array}
* @param config.maxIndex – typically the number of items in your list
* @param [config.onFocusedIndexChange] – optional callback to execute when focusedIndex changes
* @param [config.initialFocusedIndex] – where to start in the list
* @param [config.disabledIndexes] – An array of indexes to disable + skip over
* @param [config.shouldExcludeTextAreaNodes] – Whether arrow keys should have any effect when a TextArea node is focused
* @param [config.isActive] – Whether the component is ready and should subscribe to KeyboardShortcut
*/
export default function useArrowKeyFocusManager({
maxIndex,
Expand All @@ -26,7 +35,7 @@ export default function useArrowKeyFocusManager({
disabledIndexes = CONST.EMPTY_ARRAY,
shouldExcludeTextAreaNodes = true,
isActive,
}) {
}: Config): UseArrowKeyFocusManager {
const [focusedIndex, setFocusedIndex] = useState(initialFocusedIndex);
const arrowConfig = useMemo(
() => ({
Expand Down
Loading