From 0556ba6957c9477d4cc87c333c3cb2c2c729cac6 Mon Sep 17 00:00:00 2001 From: Dimitar Nestorov <8790386+DimitarNestorov@users.noreply.github.com> Date: Mon, 6 Feb 2023 10:19:50 +0200 Subject: [PATCH] fix: style types (#3623) --- package.json | 8 +- src/components/Appbar/Appbar.tsx | 23 +- src/components/Appbar/AppbarAction.tsx | 4 +- src/components/Appbar/AppbarBackAction.tsx | 3 +- src/components/Appbar/AppbarHeader.tsx | 42 +-- src/components/Appbar/utils.ts | 4 +- src/components/Banner.tsx | 4 +- .../BottomNavigation/BottomNavigation.tsx | 58 +++-- src/components/Button/Button.tsx | 3 +- src/components/Card/Card.tsx | 3 +- src/components/Chip/Chip.tsx | 37 ++- src/components/Dialog/Dialog.tsx | 10 +- src/components/FAB/AnimatedFAB.tsx | 36 ++- src/components/FAB/FAB.tsx | 36 ++- src/components/FAB/FABGroup.tsx | 56 ++-- src/components/IconButton/IconButton.tsx | 30 ++- src/components/Menu/Menu.tsx | 27 +- src/components/Modal.tsx | 10 +- src/components/Searchbar.tsx | 3 +- src/components/Snackbar.tsx | 48 ++-- src/components/ToggleButton/ToggleButton.tsx | 7 +- ...imatedFAB.test.js => AnimatedFAB.test.tsx} | 37 ++- .../{Appbar.test.js => Appbar.test.tsx} | 133 +++++++++- ...pbar.test.js.snap => Appbar.test.tsx.snap} | 10 + ...ct-native-safe-area-context-jest-mock.d.ts | 5 + .../{Banner.test.js => Banner.test.tsx} | 44 +++- ...tion.test.js => BottomNavigation.test.tsx} | 68 +++-- .../{Button.test.js => Button.test.tsx} | 242 +++++++++++------- .../Card/{Card.test.js => Card.test.tsx} | 52 +++- .../{Card.test.js.snap => Card.test.tsx.snap} | 1 + .../__tests__/{Chip.test.js => Chip.test.tsx} | 68 ++++- .../__tests__/{FAB.test.js => FAB.test.tsx} | 32 ++- .../{FABGroup.test.js => FABGroup.test.tsx} | 50 ++++ ...IconButton.test.js => IconButton.test.tsx} | 32 ++- .../__tests__/{Menu.test.js => Menu.test.tsx} | 48 +++- .../{Modal.test.js => Modal.test.tsx} | 165 ++++++++++-- .../{Searchbar.test.js => Searchbar.test.tsx} | 39 ++- .../{Snackbar.test.js => Snackbar.test.tsx} | 49 +++- ...leButton.test.js => ToggleButton.test.tsx} | 47 ++-- ...test.js.snap => AnimatedFAB.test.tsx.snap} | 1 + ...nner.test.js.snap => Banner.test.tsx.snap} | 50 +++- ...js.snap => BottomNavigation.test.tsx.snap} | 56 +++- ...tton.test.js.snap => Button.test.tsx.snap} | 13 + .../{Chip.test.js.snap => Chip.test.tsx.snap} | 12 + .../__snapshots__/DataTable.test.js.snap | 25 ++ .../{FAB.test.js.snap => FAB.test.tsx.snap} | 0 ....test.js.snap => IconButton.test.tsx.snap} | 10 + .../__snapshots__/ListItem.test.js.snap | 2 + .../{Menu.test.js.snap => Menu.test.tsx.snap} | 5 + ...r.test.js.snap => Searchbar.test.tsx.snap} | 15 ++ ...ar.test.js.snap => Snackbar.test.tsx.snap} | 1 + .../__snapshots__/TextInput.test.js.snap | 2 + ...est.js.snap => ToggleButton.test.tsx.snap} | 8 +- src/core/theming.tsx | 10 +- tsconfig.build.json | 2 +- yarn.lock | 7 + 56 files changed, 1369 insertions(+), 424 deletions(-) rename src/components/__tests__/{AnimatedFAB.test.js => AnimatedFAB.test.tsx} (61%) rename src/components/__tests__/Appbar/{Appbar.test.js => Appbar.test.tsx} (74%) rename src/components/__tests__/Appbar/__snapshots__/{Appbar.test.js.snap => Appbar.test.tsx.snap} (98%) create mode 100644 src/components/__tests__/Appbar/react-native-safe-area-context-jest-mock.d.ts rename src/components/__tests__/{Banner.test.js => Banner.test.tsx} (89%) rename src/components/__tests__/{BottomNavigation.test.js => BottomNavigation.test.tsx} (89%) rename src/components/__tests__/{Button.test.js => Button.test.tsx} (79%) rename src/components/__tests__/Card/{Card.test.js => Card.test.tsx} (83%) rename src/components/__tests__/Card/__snapshots__/{Card.test.js.snap => Card.test.tsx.snap} (98%) rename src/components/__tests__/{Chip.test.js => Chip.test.tsx} (91%) rename src/components/__tests__/{FAB.test.js => FAB.test.tsx} (92%) rename src/components/__tests__/{FABGroup.test.js => FABGroup.test.tsx} (78%) rename src/components/__tests__/{IconButton.test.js => IconButton.test.tsx} (91%) rename src/components/__tests__/{Menu.test.js => Menu.test.tsx} (78%) rename src/components/__tests__/{Modal.test.js => Modal.test.tsx} (77%) rename src/components/__tests__/{Searchbar.test.js => Searchbar.test.tsx} (61%) rename src/components/__tests__/{Snackbar.test.js => Snackbar.test.tsx} (67%) rename src/components/__tests__/{ToggleButton.test.js => ToggleButton.test.tsx} (69%) rename src/components/__tests__/__snapshots__/{AnimatedFAB.test.js.snap => AnimatedFAB.test.tsx.snap} (99%) rename src/components/__tests__/__snapshots__/{Banner.test.js.snap => Banner.test.tsx.snap} (96%) rename src/components/__tests__/__snapshots__/{BottomNavigation.test.js.snap => BottomNavigation.test.tsx.snap} (99%) rename src/components/__tests__/__snapshots__/{Button.test.js.snap => Button.test.tsx.snap} (99%) rename src/components/__tests__/__snapshots__/{Chip.test.js.snap => Chip.test.tsx.snap} (99%) rename src/components/__tests__/__snapshots__/{FAB.test.js.snap => FAB.test.tsx.snap} (100%) rename src/components/__tests__/__snapshots__/{IconButton.test.js.snap => IconButton.test.tsx.snap} (98%) rename src/components/__tests__/__snapshots__/{Menu.test.js.snap => Menu.test.tsx.snap} (99%) rename src/components/__tests__/__snapshots__/{Searchbar.test.js.snap => Searchbar.test.tsx.snap} (98%) rename src/components/__tests__/__snapshots__/{Snackbar.test.js.snap => Snackbar.test.tsx.snap} (99%) rename src/components/__tests__/__snapshots__/{ToggleButton.test.js.snap => ToggleButton.test.tsx.snap} (98%) diff --git a/package.json b/package.json index 240e73f8e8..8fc31ebda4 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "@types/react-dom": "^18.0.8", "@types/react-native": "^0.70.6", "@types/react-native-vector-icons": "^6.4.1", + "@types/react-test-renderer": "^18.0.0", "@typescript-eslint/eslint-plugin": "^5.41.0", "@typescript-eslint/parser": "^5.41.0", "all-contributors-cli": "^6.24.0", @@ -99,8 +100,8 @@ "peerDependencies": { "react": "*", "react-native": "*", - "react-native-vector-icons": "*", - "react-native-safe-area-context": "*" + "react-native-safe-area-context": "*", + "react-native-vector-icons": "*" }, "husky": { "hooks": { @@ -117,6 +118,9 @@ "@testing-library/jest-native/extend-expect" ], "cacheDirectory": "./cache/jest", + "testPathIgnorePatterns": [ + "\\.d\\.ts$" + ], "modulePathIgnorePatterns": [ "/example/node_modules", "/lib/" diff --git a/src/components/Appbar/Appbar.tsx b/src/components/Appbar/Appbar.tsx index db6f7383ca..10ea23b898 100644 --- a/src/components/Appbar/Appbar.tsx +++ b/src/components/Appbar/Appbar.tsx @@ -1,5 +1,13 @@ import * as React from 'react'; -import { Platform, StyleProp, StyleSheet, View, ViewStyle } from 'react-native'; +import { + Animated, + Platform, + StyleProp, + StyleSheet, + View, + ViewStyle, + ColorValue, +} from 'react-native'; import color from 'color'; @@ -18,7 +26,10 @@ import { renderAppbarContent, } from './utils'; -export type Props = Partial> & { +export type Props = Omit< + Partial>, + 'style' +> & { /** * Whether the background color is a dark color. A dark appbar will render light text and vice-versa. */ @@ -55,7 +66,7 @@ export type Props = Partial> & { * @optional */ theme?: ThemeProp; - style?: StyleProp; + style?: Animated.WithAnimatedValue>; }; /** @@ -160,11 +171,15 @@ const Appbar = ({ }: Props) => { const theme = useInternalTheme(themeOverrides); const { isV3 } = theme; + const flattenedStyle = StyleSheet.flatten(style); const { backgroundColor: customBackground, elevation = isV3 ? (elevated ? 2 : 0) : 4, ...restStyle - }: ViewStyle = StyleSheet.flatten(style) || {}; + } = (flattenedStyle || {}) as Exclude & { + elevation?: number; + backgroundColor?: ColorValue; + }; let isDark: boolean; diff --git a/src/components/Appbar/AppbarAction.tsx b/src/components/Appbar/AppbarAction.tsx index f3e55f959d..9f8b718354 100644 --- a/src/components/Appbar/AppbarAction.tsx +++ b/src/components/Appbar/AppbarAction.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import type { StyleProp, ViewStyle, View } from 'react-native'; +import type { StyleProp, ViewStyle, View, Animated } from 'react-native'; import color from 'color'; import type { ThemeProp } from 'src/types'; @@ -41,7 +41,7 @@ export type Props = React.ComponentPropsWithoutRef & { * Whether it's the leading button. */ isLeading?: boolean; - style?: StyleProp; + style?: Animated.WithAnimatedValue>; ref?: React.RefObject; /** * @optional diff --git a/src/components/Appbar/AppbarBackAction.tsx b/src/components/Appbar/AppbarBackAction.tsx index c8b8bd009c..e5c6427fb7 100644 --- a/src/components/Appbar/AppbarBackAction.tsx +++ b/src/components/Appbar/AppbarBackAction.tsx @@ -4,6 +4,7 @@ import type { StyleProp, ViewStyle, View, + Animated, } from 'react-native'; import { forwardRef } from '../../utils/forwardRef'; @@ -35,7 +36,7 @@ export type Props = $Omit< * Function to execute on press. */ onPress?: (e: GestureResponderEvent) => void; - style?: StyleProp; + style?: Animated.WithAnimatedValue>; ref?: React.RefObject; }; diff --git a/src/components/Appbar/AppbarHeader.tsx b/src/components/Appbar/AppbarHeader.tsx index 3c4ceae5a5..9adf1ba23b 100644 --- a/src/components/Appbar/AppbarHeader.tsx +++ b/src/components/Appbar/AppbarHeader.tsx @@ -1,5 +1,13 @@ import * as React from 'react'; -import { Platform, StyleProp, StyleSheet, View, ViewStyle } from 'react-native'; +import { + Animated, + ColorValue, + Platform, + StyleProp, + StyleSheet, + View, + ViewStyle, +} from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; @@ -48,7 +56,7 @@ export type Props = React.ComponentProps & { * @optional */ theme?: ThemeProp; - style?: StyleProp; + style?: Animated.WithAnimatedValue>; }; /** @@ -112,13 +120,19 @@ const AppbarHeader = ({ const theme = useInternalTheme(themeOverrides); const { isV3 } = theme; + const flattenedStyle = StyleSheet.flatten(style); const { height = isV3 ? modeAppbarHeight[mode] : DEFAULT_APPBAR_HEIGHT, elevation = isV3 ? (elevated ? 2 : 0) : 4, backgroundColor: customBackground, zIndex = isV3 && elevated ? 1 : 0, ...restStyle - }: ViewStyle = StyleSheet.flatten(style) || {}; + } = (flattenedStyle || {}) as Exclude & { + height?: number; + elevation?: number; + backgroundColor?: ColorValue; + zIndex?: number; + }; const backgroundColor = getAppbarColor( theme, @@ -131,18 +145,16 @@ const AppbarHeader = ({ return ( - } + style={[ + { + backgroundColor, + zIndex, + elevation, + paddingTop: statusBarHeight ?? top, + paddingHorizontal: Math.max(left, right), + }, + shadow(elevation) as ViewStyle, + ]} > []; + renderOnly?: (React.ComponentType | false)[]; renderExcept?: React.ComponentType[]; mode?: AppbarModes; theme?: ThemeProp; @@ -65,7 +65,7 @@ export const modeTextVariant = { medium: 'headlineSmall', large: 'headlineMedium', 'center-aligned': 'titleLarge', -}; +} as const; export const renderAppbarContent = ({ children, diff --git a/src/components/Banner.tsx b/src/components/Banner.tsx index b23372c8b6..65291d89a4 100644 --- a/src/components/Banner.tsx +++ b/src/components/Banner.tsx @@ -37,7 +37,7 @@ export type Props = $RemoveChildren & { actions?: Array< { label: string; - } & Omit, 'children'> + } & $RemoveChildren >; /** * Style of banner's inner content. @@ -49,7 +49,7 @@ export type Props = $RemoveChildren & { * Changes Banner shadow and background on iOS and Android. */ elevation?: 0 | 1 | 2 | 3 | 4 | 5 | Animated.Value; - style?: StyleProp; + style?: Animated.WithAnimatedValue>; ref?: React.RefObject; /** * @optional diff --git a/src/components/BottomNavigation/BottomNavigation.tsx b/src/components/BottomNavigation/BottomNavigation.tsx index aaa0602077..22641b2940 100644 --- a/src/components/BottomNavigation/BottomNavigation.tsx +++ b/src/components/BottomNavigation/BottomNavigation.tsx @@ -1,6 +1,7 @@ import * as React from 'react'; import { Animated, + ColorValue, EasingFunction, Platform, StyleProp, @@ -255,7 +256,7 @@ export type Props = { * barStyle={{ backgroundColor: '#694fad' }} * ``` */ - barStyle?: StyleProp; + barStyle?: Animated.WithAnimatedValue>; /** * Specifies the largest possible scale a label font can reach. */ @@ -620,8 +621,11 @@ const BottomNavigation = ({ const { routes } = navigationState; const { colors, dark: isDarkTheme, mode, isV3 } = theme; - const { backgroundColor: customBackground, elevation = 4 }: ViewStyle = - StyleSheet.flatten(barStyle) || {}; + const { backgroundColor: customBackground, elevation = 4 } = + (StyleSheet.flatten(barStyle) || {}) as { + elevation?: number; + backgroundColor?: ColorValue; + }; const approxBackgroundColor = customBackground ? customBackground @@ -758,29 +762,28 @@ const BottomNavigation = ({