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 2 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
@@ -1,20 +1,27 @@
import React, {forwardRef, createContext, useEffect, useState} from 'react';
import React, {ComponentType, createContext, ForwardedRef, forwardRef, ReactNode, useEffect, useState} from 'react';
import {Keyboard} from 'react-native';
import PropTypes from 'prop-types';
import getComponentDisplayName from '../libs/getComponentDisplayName';
import getComponentDisplayName from "../libs/getComponentDisplayName";

const KeyboardStateContext = createContext(null);
const keyboardStatePropTypes = {
/** Whether or not the keyboard is open */
isKeyboardShown: PropTypes.bool.isRequired,
type KeyboardStateContextValue = {
/** Whether the keyboard is open */
isKeyboardShown : boolean
};

const keyboardStateProviderPropTypes = {
type KeyboardStateProviderProps = {
/* Actual content wrapped by this component */
children: PropTypes.node.isRequired,
children: ReactNode
};

// TODO: Remove - left for backwards compatibility with existing components.
const keyboardStatePropTypes = {
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?

/** Whether the keyboard is open */
isKeyboardShown: PropTypes.bool.isRequired,
};

function KeyboardStateProvider(props) {
const KeyboardStateContext = createContext<KeyboardStateContextValue | null>(null);

function KeyboardStateProvider(props: KeyboardStateProviderProps) {
const {children} = props;
const [isKeyboardShown, setIsKeyboardShown] = useState(false);
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
useEffect(() => {
Expand All @@ -34,14 +41,10 @@ function KeyboardStateProvider(props) {
return <KeyboardStateContext.Provider value={{isKeyboardShown}}>{children}</KeyboardStateContext.Provider>;
}

KeyboardStateProvider.propTypes = keyboardStateProviderPropTypes;

/**
* @param {React.Component} WrappedComponent
* @returns {React.Component}
*/
export default function withKeyboardState(WrappedComponent) {
const WithKeyboardState = forwardRef((props, ref) => (
// eslint-disable-next-line @typescript-eslint/naming-convention
export default function withKeyboardState(WrappedComponent: ComponentType<{ref: ForwardedRef<unknown>}>) {
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
const WithKeyboardState = forwardRef((props: Record<string, unknown>, ref: React.ForwardedRef<unknown>) => (
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
<KeyboardStateContext.Consumer>
{(keyboardStateProps) => (
<WrappedComponent
Expand All @@ -52,11 +55,9 @@ export default function withKeyboardState(WrappedComponent) {
ref={ref}
/>
)}
</KeyboardStateContext.Consumer>
));

WithKeyboardState.displayName = `withKeyboardState(${getComponentDisplayName(WrappedComponent)})`;
</KeyboardStateContext.Consumer>));
(WithKeyboardState as unknown as {displayName: string}).displayName = `withKeyboardState(${getComponentDisplayName(WrappedComponent as ComponentType)})`;
return WithKeyboardState;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
}

export {KeyboardStateProvider, keyboardStatePropTypes, KeyboardStateContext};
export {KeyboardStateProvider, keyboardStatePropTypes, type KeyboardStateContextValue, KeyboardStateContext};
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