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

[TS migration] Migrate 'useKeyboardState.js' hook to TypeScript #29267

Merged
merged 15 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
68 changes: 0 additions & 68 deletions src/components/withKeyboardState.js

This file was deleted.

68 changes: 68 additions & 0 deletions src/components/withKeyboardState.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import PropTypes from 'prop-types';
import React, {ComponentType, createContext, ForwardedRef, forwardRef, ReactElement, RefAttributes, useEffect, useMemo, useState} from 'react';
import {Keyboard} from 'react-native';
import getComponentDisplayName from '@libs/getComponentDisplayName';
import ChildrenProps from '@src/types/utils/ChildrenProps';

type KeyboardStateContextValue = {
/** Whether the keyboard is open */
isKeyboardShown: boolean;
};

// TODO: Remove - left for backwards compatibility with existing components (https://github.com/Expensify/App/issues/25151)
const keyboardStatePropTypes = {
/** Whether the keyboard is open */
isKeyboardShown: PropTypes.bool.isRequired,
};

const KeyboardStateContext = createContext<KeyboardStateContextValue | null>(null);

function KeyboardStateProvider({children}: ChildrenProps): ReactElement | null {
const [isKeyboardShown, setIsKeyboardShown] = useState(false);

useEffect(() => {
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
const keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', () => {
setIsKeyboardShown(true);
});
const keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', () => {
setIsKeyboardShown(false);
});

return () => {
keyboardDidShowListener.remove();
keyboardDidHideListener.remove();
};
}, []);

const contextValue = useMemo(
() => ({
isKeyboardShown,
}),
[isKeyboardShown],
);
return <KeyboardStateContext.Provider value={contextValue}>{children}</KeyboardStateContext.Provider>;
}

export default function withKeyboardState<TProps extends KeyboardStateContextValue, TRef>(
WrappedComponent: ComponentType<TProps & RefAttributes<TRef>>,
): (props: Omit<TProps, keyof KeyboardStateContextValue> & React.RefAttributes<TRef>) => ReactElement | null {
function WithKeyboardState(props: Omit<TProps, keyof KeyboardStateContextValue>, ref: ForwardedRef<TRef>) {
return (
<KeyboardStateContext.Consumer>
{(keyboardStateProps) => (
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...keyboardStateProps}
// eslint-disable-next-line react/jsx-props-no-spreading
{...(props as TProps)}
ref={ref}
/>
)}
</KeyboardStateContext.Consumer>
);
}
WithKeyboardState.displayName = `withKeyboardState(${getComponentDisplayName(WrappedComponent)})`;
return forwardRef(WithKeyboardState);
}

export {KeyboardStateProvider, keyboardStatePropTypes, type KeyboardStateContextValue, KeyboardStateContext};
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
11 changes: 0 additions & 11 deletions src/hooks/useKeyboardState.js

This file was deleted.

10 changes: 10 additions & 0 deletions src/hooks/useKeyboardState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {useContext} from 'react';
import {KeyboardStateContext, KeyboardStateContextValue} from '@components/withKeyboardState';

/**
* Hook for getting current state of keyboard
* whether the keyboard is open
*/
export default function useKeyboardState(): KeyboardStateContextValue | null {
return useContext(KeyboardStateContext);
}
Loading