-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35884 from kubabutkiewicz/ts-migration/react-nati…
…ve-safe-area-context-mock [No QA][TS migration] Migrate 'react-native-safe-area-context.js' mock to TypeScript
- Loading branch information
Showing
2 changed files
with
61 additions
and
52 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type {ForwardedRef, ReactNode} from 'react'; | ||
import React, {forwardRef} from 'react'; | ||
import {View} from 'react-native'; | ||
import type {EdgeInsets, useSafeAreaFrame as LibUseSafeAreaFrame, WithSafeAreaInsetsProps} from 'react-native-safe-area-context'; | ||
import type ChildrenProps from '@src/types/utils/ChildrenProps'; | ||
|
||
type SafeAreaProviderProps = ChildrenProps; | ||
type SafeAreaConsumerProps = { | ||
children?: (insets: EdgeInsets) => ReactNode; | ||
}; | ||
type SafeAreaInsetsContextValue = { | ||
Consumer: (props: SafeAreaConsumerProps) => ReactNode; | ||
}; | ||
|
||
const insets: EdgeInsets = { | ||
top: 0, | ||
right: 0, | ||
bottom: 0, | ||
left: 0, | ||
}; | ||
|
||
function withSafeAreaInsets(WrappedComponent: React.ComponentType<WithSafeAreaInsetsProps & {ref: ForwardedRef<unknown>}>) { | ||
function WithSafeAreaInsets(props: WithSafeAreaInsetsProps & {forwardedRef: React.ForwardedRef<unknown>}) { | ||
return ( | ||
<WrappedComponent | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
// eslint-disable-next-line react/prop-types | ||
ref={props.forwardedRef} | ||
insets={insets} | ||
/> | ||
); | ||
} | ||
|
||
const WithSafeAreaInsetsWithRef = forwardRef((props: WithSafeAreaInsetsProps, ref: ForwardedRef<unknown>) => ( | ||
<WithSafeAreaInsets | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
forwardedRef={ref} | ||
/> | ||
)); | ||
|
||
return WithSafeAreaInsetsWithRef; | ||
} | ||
|
||
const SafeAreaView = View; | ||
const SafeAreaProvider = (props: SafeAreaProviderProps) => props.children; | ||
const SafeAreaConsumer = (props: SafeAreaConsumerProps) => props.children?.(insets); | ||
const SafeAreaInsetsContext: SafeAreaInsetsContextValue = { | ||
Consumer: SafeAreaConsumer, | ||
}; | ||
|
||
const useSafeAreaFrame: jest.Mock<ReturnType<typeof LibUseSafeAreaFrame>> = jest.fn(() => ({ | ||
x: 0, | ||
y: 0, | ||
width: 390, | ||
height: 844, | ||
})); | ||
const useSafeAreaInsets: jest.Mock<EdgeInsets> = jest.fn(() => insets); | ||
|
||
export {SafeAreaProvider, SafeAreaConsumer, SafeAreaInsetsContext, withSafeAreaInsets, SafeAreaView, useSafeAreaFrame, useSafeAreaInsets}; |