Skip to content

Commit 3c8d4c5

Browse files
M-i-k-e-lethanshar
andauthored
Migrate panning views to typescript (#923)
* Rename panningContext from .js to .ts * Rename asPanViewConsumer from .js to .tsx * Migrate asPanViewConsumer to typescript * Rename PanningProvider from .js to .tsx * Migrate PanningProvider to typescript * Rename panListenerView from .js to .tsx * Migrate PanListenerView to typescript + fixes * PanListenerView - allow undefined values * PanListenerView - allow undefined values (2) * Fix some TS errors * Remove onLayout - seems like it's not used * Rename PanListenerScreen from .js to .tsx * Migrate PanListenerScreen to typescript + renaming + exporting properly * Rename PanResponderView from .js to .tsx * Migrate PanResponderView to typescript * Rename PanResponderScreen from .js to .tsx * Migrate PanResponderScreen to typescript + some fixes * Rename PanDismissibleView from .js to .tsx * Migrate PanDismissibleView to typescript + some fixes * Remove comments * Rename PanDismissibleScreen from .js to .tsx * Migrate PanDismissibleScreen to typescript + some fixes * Fix some imports * Rename PanGestureView from .js to .tsx * Migrate PanGestureView to typescript * Fix TS errors * Change from using ? to = (more like a default prop) Co-authored-by: Ethan Sharabi <[email protected]>
1 parent 8f4b172 commit 3c8d4c5

21 files changed

+638
-334
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import _ from 'lodash';
22
import React, {Component} from 'react';
3-
import {View, Text, PanListenerView, PanningProvider} from 'react-native-ui-lib'; //eslint-disable-line
3+
import {View, Text, PanListenerView, PanningProvider, PanDirectionsProps, PanAmountsProps} from 'react-native-ui-lib'; //eslint-disable-line
44

55
export default class PanListenerScreen extends Component {
66
state = {
77
locationText: '',
8-
endType: '',
8+
endType: ''
99
};
1010

11-
onDrag = ({directions, deltas}) => {
11+
onDrag = ({directions, deltas}: ({directions: PanDirectionsProps, deltas: PanAmountsProps})) => {
1212
this.setState({locationText: `Dragged: ${directions.x}, ${directions.y}`});
1313
};
1414

15-
onSwipe = ({directions, velocities}) => {
15+
onSwipe = ({directions, velocities}: ({directions: PanDirectionsProps, velocities: PanAmountsProps})) => {
1616
this.setState({locationText: `Swiped: ${directions.x}, ${directions.y}`});
1717
};
1818

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,28 @@ import {
99
Typography,
1010
PanListenerView,
1111
PanningProvider,
12-
PanResponderView,
13-
} from 'react-native-ui-lib'; //eslint-disable-line
12+
PanResponderView
13+
} from 'react-native-ui-lib';
1414

1515
const PAN_LISTENER_VIEW_HEIGHT = 100;
1616

1717
export default class PanResponderScreen extends Component {
1818
state = {
1919
location: {left: 50, top: 50},
20-
isCoupled: false,
20+
isCoupled: false
2121
};
2222

2323
switchExample = () => {
2424
const {isCoupled, location} = this.state;
2525
if (isCoupled) {
2626
this.setState({
2727
isCoupled: false,
28-
location: {left: location.left, top: location.top - PAN_LISTENER_VIEW_HEIGHT},
28+
location: {left: location.left, top: location.top - PAN_LISTENER_VIEW_HEIGHT}
2929
});
3030
} else {
3131
this.setState({
3232
isCoupled: true,
33-
location: {left: location.left, top: location.top + PAN_LISTENER_VIEW_HEIGHT},
33+
location: {left: location.left, top: location.top + PAN_LISTENER_VIEW_HEIGHT}
3434
});
3535
}
3636
};
@@ -71,29 +71,29 @@ const styles = StyleSheet.create({
7171
container: {
7272
flexDirection: 'row',
7373
height: 50,
74-
alignItems: 'center',
74+
alignItems: 'center'
7575
},
7676
panResponder: {
7777
width: 250,
7878
height: 250,
79-
backgroundColor: Colors.blue30,
79+
backgroundColor: Colors.blue30
8080
},
8181
panListener: {
8282
width: '100%',
8383
height: PAN_LISTENER_VIEW_HEIGHT,
8484
backgroundColor: Colors.blue60,
85-
justifyContent: 'center',
85+
justifyContent: 'center'
8686
},
8787
largeText: {
8888
...Typography.text50,
89-
marginLeft: 40,
89+
marginLeft: 40
9090
},
9191
smallText: {
9292
...Typography.text70,
93-
marginLeft: 20,
93+
marginLeft: 20
9494
},
9595
switch: {
9696
marginLeft: 20,
97-
alignSelf: 'center',
98-
},
97+
alignSelf: 'center'
98+
}
9999
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React from 'react';
2+
declare function asPanViewConsumer<PROPS>(WrappedComponent: React.ComponentType<any>): React.ComponentClass<PROPS>;
3+
export default asPanViewConsumer;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React from 'react';
2+
import { StyleProp, ViewStyle } from 'react-native';
3+
import { PanningDirections, PanAmountsProps } from './panningProvider';
4+
export interface DismissibleAnimationPropTypes {
5+
/**
6+
* The return animation speed (default is 20)
7+
*/
8+
speed: number;
9+
/**
10+
* The return animation bounciness (default is 6)
11+
*/
12+
bounciness: number;
13+
/**
14+
* The dismiss animation duration (default is 280)
15+
*/
16+
duration: number;
17+
}
18+
export interface PanDismissibleViewPropTypes {
19+
/**
20+
* Additional styling
21+
*/
22+
style?: StyleProp<ViewStyle>;
23+
/**
24+
* The directions of the allowed pan (default allows all directions)
25+
* Types: UP, DOWN, LEFT and RIGHT (using PanningProvider.Directions.###)
26+
*/
27+
directions?: PanningDirections[];
28+
/**
29+
* onDismiss callback
30+
*/
31+
onDismiss?: () => void;
32+
/**
33+
* Some animation options to choose from, defaults are set for:
34+
* speed - the animation speed (default is 20)
35+
* bounciness - the animation bounciness (default is 6)
36+
* duration - the dismiss animation duration (default is 280)
37+
*/
38+
animationOptions: DismissibleAnimationPropTypes;
39+
/**
40+
* Override the default threshold (height/2 and width/2) with different values.
41+
*/
42+
threshold?: PanAmountsProps;
43+
/**
44+
* Allow diagonal dismiss, this is false by default,
45+
* since it looks better and most cases.
46+
*/
47+
allowDiagonalDismiss?: boolean;
48+
}
49+
declare const _default: React.ComponentClass<PanDismissibleViewPropTypes, any>;
50+
export default _default;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
import { StyleProp, ViewStyle } from 'react-native';
3+
export declare enum GestureDirections {
4+
UP = "up",
5+
DOWN = "down"
6+
}
7+
export interface PanGestureViewPropTypes {
8+
/**
9+
* Additional styling
10+
*/
11+
style?: StyleProp<ViewStyle>;
12+
/**
13+
* onDismiss callback
14+
*/
15+
onDismiss?: () => void;
16+
/**
17+
* The direction of the allowed pan (default is down)
18+
*/
19+
direction?: GestureDirections;
20+
}
21+
declare const _default: React.ComponentClass<PanGestureViewPropTypes & {
22+
useCustomTheme?: boolean | undefined; /**
23+
* onDismiss callback
24+
*/
25+
}, any>;
26+
export default _default;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import React from 'react';
2+
import { PanningDirections, PanDirectionsProps, PanAmountsProps } from './panningProvider';
3+
import { ViewPropTypes } from '../view';
4+
interface PanningPropTypes {
5+
/**
6+
* This is were you will get notified when a drag occurs
7+
* onDrag = ({directions, deltas}) => {...}
8+
* directions - array of directions
9+
* deltas - array of deltas (same length and order as directions)
10+
* Both arrays will have {x, y} - if no x or y drag has occurred this value will be undefined
11+
*/
12+
onDrag?: ({ directions, deltas }: ({
13+
directions: PanDirectionsProps;
14+
deltas: PanAmountsProps;
15+
})) => void;
16+
/**
17+
* This is were you will get notified when a swipe occurs
18+
* onSwipe = ({directions, velocities}) => {...}
19+
* directions - array of directions
20+
* velocities - array of velocities (same length and order as directions)
21+
* Both arrays will have {x, y} - if no x or y swipe has occurred this value will be undefined
22+
*/
23+
onSwipe?: ({ directions, velocities }: ({
24+
directions: PanDirectionsProps;
25+
velocities: PanAmountsProps;
26+
})) => void;
27+
/**
28+
* This is were you will get notified when the pan starts
29+
*/
30+
onPanStart?: () => void;
31+
/**
32+
* This is were you will get notified when the pan ends
33+
* The user has released all touches while this view is the responder.
34+
* This typically means a gesture has succeeded
35+
*/
36+
onPanRelease?: () => void;
37+
/**
38+
* This is were you will get notified when the pan ends
39+
* Another component has become the responder,
40+
* so this gesture should be cancelled
41+
*/
42+
onPanTerminated?: () => void;
43+
}
44+
export interface PanListenerViewPropTypes extends PanningPropTypes, ViewPropTypes {
45+
/**
46+
* The directions of the allowed pan (default allows all directions)
47+
* Types: UP, DOWN, LEFT and RIGHT (using PanningProvider.Directions.###)
48+
*/
49+
directions?: PanningDirections[];
50+
/**
51+
* The sensitivity beyond which a pan is no longer considered a single click (default is 5)
52+
*/
53+
panSensitivity?: number;
54+
/**
55+
* The sensitivity beyond which a pan is no longer considered a drag, but a swipe (default is 1.8)
56+
* Note: a pan would have to occur (i.e. the panSensitivity has already been surpassed)
57+
*/
58+
swipeVelocitySensitivity?: number;
59+
/**
60+
* Is there a view that is clickable (has onPress etc.) in the PanListenerView.
61+
* This can affect the panability of this component.
62+
*/
63+
isClickable?: boolean;
64+
}
65+
declare const _default: React.ComponentClass<PanListenerViewPropTypes, any>;
66+
export default _default;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react';
2+
import { ViewPropTypes } from '../view';
3+
import { PanLocationProps } from './panningProvider';
4+
export interface PanResponderViewPropTypes extends ViewPropTypes {
5+
/**
6+
* Will be called with the current location ({left, top}) when the pan has ended
7+
*/
8+
onPanLocationChanged?: (location: PanLocationProps) => void;
9+
/**
10+
* Ignore panning events while this is true
11+
*/
12+
ignorePanning?: boolean;
13+
/**
14+
* Allow the view to be animated (send animation via style; default is false)
15+
*/
16+
isAnimated?: boolean;
17+
}
18+
declare const _default: React.ComponentClass<PanResponderViewPropTypes, any>;
19+
export default _default;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React from 'react';
2+
declare const PanningContext: React.Context<{}>;
3+
export default PanningContext;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { Component } from 'react';
2+
export declare enum PanningDirections {
3+
UP = "up",
4+
DOWN = "down",
5+
LEFT = "left",
6+
RIGHT = "right"
7+
}
8+
export interface PanLocationProps {
9+
left?: number;
10+
top?: number;
11+
}
12+
export interface PanDirectionsProps {
13+
x?: PanningDirections;
14+
y?: PanningDirections;
15+
}
16+
export interface PanAmountsProps {
17+
x?: number;
18+
y?: number;
19+
}
20+
interface State {
21+
isPanning: boolean;
22+
wasTerminated: boolean;
23+
dragDirections: PanDirectionsProps;
24+
dragDeltas: PanAmountsProps;
25+
swipeDirections: PanDirectionsProps;
26+
swipeVelocities: PanAmountsProps;
27+
panLocation: PanLocationProps;
28+
}
29+
/**
30+
* @description: Wraps the panResponderView and panListenerView to provide a shared context
31+
*/
32+
export default class PanningProvider extends Component<{}, State> {
33+
static displayName: string;
34+
static Directions: typeof PanningDirections;
35+
constructor(props: {});
36+
getProviderContextValue: () => {
37+
onPanStart: () => void;
38+
onPanRelease: () => void;
39+
onPanTerminated: () => void;
40+
isPanning: boolean;
41+
wasTerminated: boolean;
42+
onDrag: ({ directions, deltas }: {
43+
directions: PanDirectionsProps;
44+
deltas: PanAmountsProps;
45+
}) => void;
46+
dragDirections: PanDirectionsProps;
47+
dragDeltas: PanAmountsProps;
48+
onSwipe: ({ directions, velocities }: {
49+
directions: PanDirectionsProps;
50+
velocities: PanAmountsProps;
51+
}) => void;
52+
swipeDirections: PanDirectionsProps;
53+
swipeVelocities: PanAmountsProps;
54+
onPanLocationChanged: (location: PanLocationProps) => void;
55+
panLocation: PanLocationProps;
56+
};
57+
onPanStart: () => void;
58+
onPanRelease: () => void;
59+
onPanTerminated: () => void;
60+
onDrag: ({ directions, deltas }: {
61+
directions: PanDirectionsProps;
62+
deltas: PanAmountsProps;
63+
}) => void;
64+
onSwipe: ({ directions, velocities }: {
65+
directions: PanDirectionsProps;
66+
velocities: PanAmountsProps;
67+
}) => void;
68+
onPanLocationChanged: (location: PanLocationProps) => void;
69+
render(): JSX.Element;
70+
}
71+
export {};

generatedTypes/index.d.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,20 @@ export {default as Overlay, OverlayTypes} from './components/overlay';
2020
export {default as RadioButton, RadioButtonPropTypes} from './components/radioButton/RadioButton';
2121
export {default as RadioGroup, RadioGroupPropTypes} from './components/radioButton/RadioGroup';
2222
export {default as TabBar} from './components/TabBar';
23-
export {default as Fader, FaderProps} from './components/fader';
23+
export {default as Fader, FaderProps, FaderPosition} from './components/fader';
2424
export {default as Modal, ModalProps, ModalTopBarProps} from './components/modal';
25+
export {default as PanningContext} from './components/panningViews/panningContext';
26+
export {default as asPanViewConsumer} from './components/panningViews/asPanViewConsumer';
27+
export {
28+
default as PanningProvider,
29+
PanningDirections,
30+
PanLocationProps,
31+
PanAmountsProps,
32+
PanDirectionsProps
33+
} from './components/panningViews/panningProvider';
34+
export {default as PanListenerView, PanListenerViewPropTypes} from './components/panningViews/panListenerView';
35+
export {default as PanResponderView, PanResponderViewPropTypes} from './components/panningViews/panResponderView';
36+
export {default as PanDismissibleView, PanDismissibleViewPropTypes, DismissibleAnimationPropTypes} from './components/panningViews/panDismissibleView';
2537

2638
/* All components with manual typings */
2739
export {
@@ -41,11 +53,6 @@ export {
4153
MaskedInput,
4254
ListItem,
4355
PageControl,
44-
PanningProvider,
45-
PanGestureView,
46-
PanListenerView,
47-
PanDismissibleView,
48-
PanResponderView,
4956
ProgressBar,
5057
Slider,
5158
GradientSlider,
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import React, {Component} from 'react';
1+
import React, {Component, Ref} from 'react';
22
import PanningContext from './panningContext';
33

4-
function asPanViewConsumer(WrappedComponent) {
5-
class PanViewConsumer extends Component {
6-
saveRef = r => {
4+
function asPanViewConsumer<PROPS>(WrappedComponent: React.ComponentType<any>): React.ComponentClass<PROPS> {
5+
class PanViewConsumer extends Component<PROPS> {
6+
contentRef: any;
7+
8+
saveRef = (r: Ref<React.Component<any>>) => {
79
this.contentRef = r;
810
};
911

@@ -16,7 +18,7 @@ function asPanViewConsumer(WrappedComponent) {
1618
}
1719
}
1820

19-
return PanViewConsumer;
21+
return PanViewConsumer as any;
2022
}
2123

2224
export default asPanViewConsumer;

0 commit comments

Comments
 (0)