Skip to content

Commit 46417c4

Browse files
authored
Infra/typescript page control (#993)
* Rename pageControl from .js to .tsx * Move PageControl to typescript - initial commit * Fix forwardRef * Rename PageControlScreen .js to .tsx * PageControl - export types properly and fix screen
1 parent 454e1c4 commit 46417c4

File tree

18 files changed

+142
-83
lines changed

18 files changed

+142
-83
lines changed

demo/src/screens/componentScreens/PageControlScreen.js renamed to demo/src/screens/componentScreens/PageControlScreen.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default class PageControlScreen extends Component {
2222
this.setState({currentPage: this.state.currentPage + 1});
2323
}
2424

25-
onPagePress = (index) => {
25+
onPagePress = (index: number) => {
2626
this.setState({currentPage: index});
2727
}
2828

generatedTypes/commons/forwardRef.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ export interface ForwardRefInjectedProps {
55
*/
66
forwardedRef: any;
77
}
8-
export default function forwardRef(WrappedComponent: React.ComponentType<any>): React.ComponentType<any>;
8+
export default function forwardRef<P = any>(WrappedComponent: React.ComponentType<P>): React.ComponentType<P>;

generatedTypes/commons/withScrollEnabler.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import { LayoutChangeEvent } from 'react-native';
3-
export declare type ScrollEnablerProps = {
3+
declare type ScrollEnablerProps = {
44
onContentSizeChange: (contentWidth: number, contentHeight: number) => void;
55
onLayout: (event: LayoutChangeEvent) => void;
66
scrollEnabled: boolean;

generatedTypes/commons/withScrollReached.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import { NativeSyntheticEvent, NativeScrollEvent } from 'react-native';
3-
export declare type ScrollReachedProps = {
3+
declare type ScrollReachedProps = {
44
onScroll: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
55
/**
66
* Is the scroll at the start (or equal\smaller than the threshold if one was given)

generatedTypes/components/button/index.d.ts

Lines changed: 8 additions & 4 deletions
Large diffs are not rendered by default.

generatedTypes/components/card/index.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export declare type CardPropTypes = ViewPropTypes & TouchableOpacityProps & {
6262
hideIndicator?: boolean;
6363
};
6464
};
65-
declare const _default: React.ComponentClass<ViewPropTypes & Pick<import("react-native").TouchableOpacityProps, "testID" | "disabled" | "hitSlop" | "onLayout" | "hasTVPreferredFocus" | "tvParallaxProperties" | "accessible" | "accessibilityActions" | "accessibilityLabel" | "accessibilityRole" | "accessibilityStates" | "accessibilityState" | "accessibilityHint" | "accessibilityValue" | "onAccessibilityAction" | "accessibilityComponentType" | "accessibilityLiveRegion" | "importantForAccessibility" | "accessibilityElementsHidden" | "accessibilityTraits" | "accessibilityViewIsModal" | "onAccessibilityEscape" | "onAccessibilityTap" | "onMagicTap" | "accessibilityIgnoresInvertColors" | "activeOpacity" | "onPress" | "onLongPress" | "delayLongPress" | "delayPressIn" | "delayPressOut" | "onBlur" | "onFocus" | "onPressIn" | "onPressOut" | "pressRetentionOffset"> & Partial<Record<import("../../commons/modifiers").AlignmentLiterals, boolean>> & Partial<Record<import("../../commons/modifiers").PositionLiterals, boolean>> & Partial<Record<"padding" | "paddingL" | "paddingT" | "paddingR" | "paddingB" | "paddingH" | "paddingV", boolean>> & Partial<Record<"margin" | "marginL" | "marginT" | "marginR" | "marginB" | "marginH" | "marginV", boolean>> & Partial<Record<"flex" | "flexG" | "flexS", boolean>> & Partial<Record<"br0" | "br10" | "br20" | "br30" | "br40" | "br50" | "br60" | "br100", boolean>> & Partial<Record<"bg", boolean>> & {
65+
declare const _default: React.ComponentClass<ViewPropTypes & Pick<import("react-native").TouchableOpacityProps, "testID" | "onLayout" | "hitSlop" | "hasTVPreferredFocus" | "tvParallaxProperties" | "accessible" | "accessibilityActions" | "accessibilityLabel" | "accessibilityRole" | "accessibilityStates" | "accessibilityState" | "accessibilityHint" | "accessibilityValue" | "onAccessibilityAction" | "accessibilityComponentType" | "accessibilityLiveRegion" | "importantForAccessibility" | "accessibilityElementsHidden" | "accessibilityTraits" | "accessibilityViewIsModal" | "onAccessibilityEscape" | "onAccessibilityTap" | "onMagicTap" | "accessibilityIgnoresInvertColors" | "disabled" | "activeOpacity" | "onLongPress" | "delayLongPress" | "delayPressIn" | "delayPressOut" | "onBlur" | "onFocus" | "onPressIn" | "onPressOut" | "pressRetentionOffset"> & Partial<Record<import("../../commons/modifiers").AlignmentLiterals, boolean>> & Partial<Record<import("../../commons/modifiers").PositionLiterals, boolean>> & Partial<Record<"padding" | "paddingL" | "paddingT" | "paddingR" | "paddingB" | "paddingH" | "paddingV", boolean>> & Partial<Record<"margin" | "marginL" | "marginT" | "marginR" | "marginB" | "marginH" | "marginV", boolean>> & Partial<Record<"flex" | "flexG" | "flexS", boolean>> & Partial<Record<"br0" | "br10" | "br20" | "br30" | "br40" | "br50" | "br60" | "br100", boolean>> & Partial<Record<"bg", boolean>> & {
6666
backgroundColor?: string | undefined;
6767
throttleTime?: number | undefined;
6868
throttleOptions?: {
@@ -71,8 +71,11 @@ declare const _default: React.ComponentClass<ViewPropTypes & Pick<import("react-
7171
} | undefined;
7272
activeBackgroundColor?: string | undefined;
7373
useNative?: boolean | undefined;
74-
customValue?: any;
74+
customValue?: any; /**
75+
* card custom height
76+
*/
7577
style?: false | ViewStyle | import("react-native").RegisteredStyle<ViewStyle> | import("react-native").RecursiveArray<false | ViewStyle | import("react-native").RegisteredStyle<ViewStyle> | null | undefined> | Animated.AnimatedProps<ViewStyle> | Animated.AnimatedProps<import("react-native").RegisteredStyle<ViewStyle>> | Animated.AnimatedProps<import("react-native").RecursiveArray<false | ViewStyle | import("react-native").RegisteredStyle<ViewStyle> | null | undefined>> | null | undefined;
78+
onPress?: ((props: TouchableOpacityProps) => void) | undefined;
7679
} & {
7780
/**
7881
* card custom width
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import React from 'react';
2+
import { StyleProp, ViewStyle } from 'react-native';
3+
export interface PageControlProps {
4+
/**
5+
* Limit the number of page indicators shown.
6+
* enlargeActive prop is disabled in this state,
7+
* When set to true there will be maximum of 7 shown.
8+
* Only relevant when numOfPages > 5.
9+
*/
10+
limitShownPages?: boolean;
11+
/**
12+
* Additional styles for the top container
13+
*/
14+
containerStyle?: StyleProp<ViewStyle>;
15+
/**
16+
* Total number of pages
17+
*/
18+
numOfPages: number;
19+
/**
20+
* Zero-based index of the current page
21+
*/
22+
currentPage: number;
23+
/**
24+
* Action handler for clicking on a page indicator
25+
*/
26+
onPagePress?: (index: number) => void;
27+
/**
28+
* Color of the selected page dot and, if inactiveColor not passed, the border of the not selected pages
29+
*/
30+
color?: string;
31+
/**
32+
* Color of the unselected page dots and the border of the not selected pages
33+
*/
34+
inactiveColor?: string;
35+
/**
36+
* The size of the page indicator.
37+
* When setting limitShownPages the medium sized will be 2/3 of size and the small will be 1/3 of size.
38+
* An alternative is to send an array [smallSize, mediumSize, largeSize].
39+
*/
40+
size?: number | [number, number, number];
41+
/**
42+
* Whether to enlarge the active page indicator
43+
* Irrelevant when limitShownPages is in effect.
44+
*/
45+
enlargeActive?: boolean;
46+
/**
47+
* The space between the siblings page indicators
48+
*/
49+
spacing?: number;
50+
}
51+
declare const _default: React.ComponentClass<PageControlProps & {
52+
useCustomTheme?: boolean | undefined;
53+
}, any>;
54+
export default _default;

generatedTypes/components/touchableOpacity/index.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { TouchableOpacityProps as RNTouchableOpacityProps, StyleProp, ViewStyle, Animated } from 'react-native';
33
import { ContainerModifiers } from '../../commons/new';
4-
export declare type TouchableOpacityProps = Omit<RNTouchableOpacityProps, 'style'> & ContainerModifiers & {
4+
export declare type TouchableOpacityProps = Omit<RNTouchableOpacityProps, 'style' | 'onPress'> & ContainerModifiers & {
55
/**
66
* background color for TouchableOpacity
77
*/
@@ -30,8 +30,9 @@ export declare type TouchableOpacityProps = Omit<RNTouchableOpacityProps, 'style
3030
*/
3131
customValue?: any;
3232
style?: StyleProp<ViewStyle> | Animated.AnimatedProps<StyleProp<ViewStyle>>;
33+
onPress?: (props: TouchableOpacityProps) => void;
3334
};
34-
declare const _default: React.ComponentClass<Pick<RNTouchableOpacityProps, "testID" | "disabled" | "hitSlop" | "onLayout" | "hasTVPreferredFocus" | "tvParallaxProperties" | "accessible" | "accessibilityActions" | "accessibilityLabel" | "accessibilityRole" | "accessibilityStates" | "accessibilityState" | "accessibilityHint" | "accessibilityValue" | "onAccessibilityAction" | "accessibilityComponentType" | "accessibilityLiveRegion" | "importantForAccessibility" | "accessibilityElementsHidden" | "accessibilityTraits" | "accessibilityViewIsModal" | "onAccessibilityEscape" | "onAccessibilityTap" | "onMagicTap" | "accessibilityIgnoresInvertColors" | "activeOpacity" | "onPress" | "onLongPress" | "delayLongPress" | "delayPressIn" | "delayPressOut" | "onBlur" | "onFocus" | "onPressIn" | "onPressOut" | "pressRetentionOffset"> & Partial<Record<import("../../commons/modifiers").AlignmentLiterals, boolean>> & Partial<Record<import("../../commons/modifiers").PositionLiterals, boolean>> & Partial<Record<"padding" | "paddingL" | "paddingT" | "paddingR" | "paddingB" | "paddingH" | "paddingV", boolean>> & Partial<Record<"margin" | "marginL" | "marginT" | "marginR" | "marginB" | "marginH" | "marginV", boolean>> & Partial<Record<"flex" | "flexG" | "flexS", boolean>> & Partial<Record<"br0" | "br10" | "br20" | "br30" | "br40" | "br50" | "br60" | "br100", boolean>> & Partial<Record<"bg", boolean>> & {
35+
declare const _default: React.ComponentClass<Pick<RNTouchableOpacityProps, "testID" | "onLayout" | "hitSlop" | "hasTVPreferredFocus" | "tvParallaxProperties" | "accessible" | "accessibilityActions" | "accessibilityLabel" | "accessibilityRole" | "accessibilityStates" | "accessibilityState" | "accessibilityHint" | "accessibilityValue" | "onAccessibilityAction" | "accessibilityComponentType" | "accessibilityLiveRegion" | "importantForAccessibility" | "accessibilityElementsHidden" | "accessibilityTraits" | "accessibilityViewIsModal" | "onAccessibilityEscape" | "onAccessibilityTap" | "onMagicTap" | "accessibilityIgnoresInvertColors" | "disabled" | "activeOpacity" | "onLongPress" | "delayLongPress" | "delayPressIn" | "delayPressOut" | "onBlur" | "onFocus" | "onPressIn" | "onPressOut" | "pressRetentionOffset"> & Partial<Record<import("../../commons/modifiers").AlignmentLiterals, boolean>> & Partial<Record<import("../../commons/modifiers").PositionLiterals, boolean>> & Partial<Record<"padding" | "paddingL" | "paddingT" | "paddingR" | "paddingB" | "paddingH" | "paddingV", boolean>> & Partial<Record<"margin" | "marginL" | "marginT" | "marginR" | "marginB" | "marginH" | "marginV", boolean>> & Partial<Record<"flex" | "flexG" | "flexS", boolean>> & Partial<Record<"br0" | "br10" | "br20" | "br30" | "br40" | "br50" | "br60" | "br100", boolean>> & Partial<Record<"bg", boolean>> & {
3536
/**
3637
* background color for TouchableOpacity
3738
*/
@@ -60,6 +61,7 @@ declare const _default: React.ComponentClass<Pick<RNTouchableOpacityProps, "test
6061
*/
6162
customValue?: any;
6263
style?: false | ViewStyle | import("react-native").RegisteredStyle<ViewStyle> | import("react-native").RecursiveArray<false | ViewStyle | import("react-native").RegisteredStyle<ViewStyle> | null | undefined> | Animated.AnimatedProps<ViewStyle> | Animated.AnimatedProps<import("react-native").RegisteredStyle<ViewStyle>> | Animated.AnimatedProps<import("react-native").RecursiveArray<false | ViewStyle | import("react-native").RegisteredStyle<ViewStyle> | null | undefined>> | null | undefined;
64+
onPress?: ((props: TouchableOpacityProps) => void) | undefined;
6365
} & {
6466
useCustomTheme?: boolean | undefined;
6567
}, any>;

generatedTypes/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export {default as PanListenerView, PanListenerViewPropTypes} from './components
4141
export {default as PanResponderView, PanResponderViewPropTypes} from './components/panningViews/panResponderView';
4242
export {default as PanDismissibleView, PanDismissibleViewPropTypes, DismissibleAnimationPropTypes} from './components/panningViews/panDismissibleView';
4343
export {default as Dialog, DialogProps} from './components/dialog';
44+
export {default as PageControl, PageControlProps} from './components/pageControl';
4445

4546
/* All components with manual typings */
4647
export {
@@ -57,7 +58,6 @@ export {
5758
TextArea,
5859
MaskedInput,
5960
ListItem,
60-
PageControl,
6161
ProgressBar,
6262
Slider,
6363
GradientSlider,

src/commons/forwardRef.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@ export interface ForwardRefInjectedProps {
99
forwardedRef: any;
1010
}
1111

12-
export default function forwardRef(WrappedComponent: React.ComponentType<any>): React.ComponentType<any> {
13-
function forwardRef(props: any, ref: any) {
12+
export default function forwardRef<P = any>(WrappedComponent: React.ComponentType<P>): React.ComponentType<P> {
13+
function forwardRef(props: P, ref: any) {
1414
return <WrappedComponent {...props} forwardedRef={ref}/>;
1515
}
1616

1717
const ForwardedComponent = React.forwardRef(forwardRef);
1818

1919
hoistStatics(ForwardedComponent, WrappedComponent);
20-
//@ts-ignore
2120
ForwardedComponent.displayName = WrappedComponent.displayName;
2221
//@ts-ignore
2322
ForwardedComponent.propTypes = WrappedComponent.propTypes;

src/commons/withScrollEnabler.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import forwardRef, {ForwardRefInjectedProps} from './forwardRef';
66
//@ts-ignore
77
import hoistStatics from 'hoist-non-react-statics';
88

9-
export type ScrollEnablerProps = {
9+
type ScrollEnablerProps = {
1010
onContentSizeChange: (contentWidth: number, contentHeight: number) => void;
1111
onLayout: (event: LayoutChangeEvent) => void;
1212
scrollEnabled: boolean;
@@ -24,7 +24,7 @@ type PropTypes = ForwardRefInjectedProps & SupportedViewsProps;
2424
function withScrollEnabler<PROPS, STATICS = {}>(
2525
WrappedComponent: React.ComponentType<PROPS & WithScrollEnablerProps>
2626
): React.ComponentType<PROPS> & STATICS {
27-
const ScrollEnabler = (props: PROPS & PropTypes) => {
27+
const ScrollEnabler: React.FunctionComponent<PROPS & PropTypes> = (props: PROPS & PropTypes) => {
2828
const [scrollEnabled, setScrollEnabled] = useState(true);
2929
const contentSize = useRef(0);
3030
const layoutSize = useRef(0);
@@ -78,7 +78,9 @@ function withScrollEnabler<PROPS, STATICS = {}>(
7878

7979
hoistStatics(ScrollEnabler, WrappedComponent);
8080
ScrollEnabler.displayName = WrappedComponent.displayName;
81+
//@ts-ignore
8182
ScrollEnabler.propTypes = WrappedComponent.propTypes;
83+
//@ts-ignore
8284
ScrollEnabler.defaultProps = WrappedComponent.defaultProps;
8385
return forwardRef(ScrollEnabler) as any;
8486
}

src/commons/withScrollReached.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import forwardRef, {ForwardRefInjectedProps} from './forwardRef';
1515
import hoistStatics from 'hoist-non-react-statics';
1616
import {Constants} from '../helpers';
1717

18-
export type ScrollReachedProps = {
18+
type ScrollReachedProps = {
1919
onScroll: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
2020
/**
2121
* Is the scroll at the start (or equal\smaller than the threshold if one was given)
@@ -59,7 +59,7 @@ function withScrollReached<PROPS, STATICS = {}>(
5959
WrappedComponent: React.ComponentType<PROPS & WithScrollReachedProps>,
6060
options: WithScrollReachedOptionsProps = {}
6161
): React.ComponentType<PROPS> & STATICS {
62-
const ScrollReachedDetector = (props: PROPS & PropTypes) => {
62+
const ScrollReachedDetector: React.FunctionComponent<PROPS & PropTypes> = (props: PROPS & PropTypes) => {
6363
// The scroll starts at the start, from what I've tested this works fine
6464
const [isScrollAtStart, setScrollAtStart] = useState(true);
6565
const [isScrollAtEnd, setScrollAtEnd] = useState(false);
@@ -102,9 +102,11 @@ function withScrollReached<PROPS, STATICS = {}>(
102102

103103
hoistStatics(ScrollReachedDetector, WrappedComponent);
104104
ScrollReachedDetector.displayName = WrappedComponent.displayName;
105+
//@ts-ignore
105106
ScrollReachedDetector.propTypes = WrappedComponent.propTypes;
107+
//@ts-ignore
106108
ScrollReachedDetector.defaultProps = WrappedComponent.defaultProps;
107-
return forwardRef(ScrollReachedDetector) as any;
109+
return forwardRef<PROPS & PropTypes>(ScrollReachedDetector) as any;
108110
}
109111

110112
export default withScrollReached;

src/components/button/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,4 +572,4 @@ function createStyles() {
572572

573573
export {Button}; // For tests
574574

575-
export default asBaseComponent<ButtonPropTypes, typeof Button>(forwardRef(Button));
575+
export default asBaseComponent<ButtonPropTypes, typeof Button>(forwardRef<Props>(Button));

0 commit comments

Comments
 (0)