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 8 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.

65 changes: 65 additions & 0 deletions src/components/withKeyboardState.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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.
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this still required, if so, can you link an issue/ PR which is tied to this?

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, TRef>(WrappedComponent: ComponentType<TProps & RefAttributes<TRef>>): (props: TProps & React.RefAttributes<TRef>) => ReactElement | null {
function WithKeyboardState(props: TProps, 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}
ref={ref}
/>
)}
</KeyboardStateContext.Consumer>
);
}
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
WithKeyboardState.displayName = `withKeyboardState(${getComponentDisplayName(WrappedComponent as ComponentType)})`;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
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