Skip to content

Commit 2e04042

Browse files
committed
migrate TouchableOpacity to typescript
1 parent 60a7005 commit 2e04042

File tree

4 files changed

+67
-35
lines changed

4 files changed

+67
-35
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React from 'react';
2+
import { TouchableOpacityProps } from 'react-native';
3+
declare type IProps = TouchableOpacityProps & {
4+
/**
5+
* background color for TouchableOpacity
6+
*/
7+
backgroundColor?: string;
8+
/**
9+
* throttle time in MS for onPress callback
10+
*/
11+
throttleTime?: number;
12+
/**
13+
* throttle options {leading, trailing}
14+
*/
15+
throttleOptions?: {
16+
leading: boolean;
17+
trailing: boolean;
18+
};
19+
/**
20+
* Apply background color on TouchableOpacity when active (press is on)
21+
*/
22+
activeBackgroundColor?: string;
23+
/**
24+
* Should use a more native touchable opacity component
25+
*/
26+
useNative?: boolean;
27+
};
28+
declare const _default: React.ComponentType<IProps>;
29+
export default _default;

generatedTypes/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
export * from './style';
77
export {default as View} from './components/view';
88
export {default as Text} from './components/text';
9+
export {default as TouchableOpacity} from './components/touchableOpacity';
910

1011
/* All components with manual typings */
1112
export {
@@ -60,8 +61,7 @@ export {
6061
StateScreen,
6162
WheelPicker,
6263
Incubator,
63-
ColorPicker,
64-
TouchableOpacity
64+
ColorPicker
6565
} from '../typings';
6666

6767
/* All components that are missing either manual or auto generated typings */

src/components/touchableOpacity/index.js renamed to src/components/touchableOpacity/index.tsx

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
11
import React, {PureComponent} from 'react';
2-
import {TouchableOpacity as RNTouchableOpacity} from 'react-native';
3-
import PropTypes from 'prop-types';
2+
import {TouchableOpacity as RNTouchableOpacity, TouchableOpacityProps} from 'react-native';
43
import _ from 'lodash';
5-
import {asBaseComponent, forwardRef} from '../../commons';
4+
import {asBaseComponent, forwardRef, BaseComponentInjectedProps, ForwardRefInjectedProps} from '../../commons/new';
5+
// @ts-ignore
66
import Incubator from '../../incubator';
77

8+
type IProps = TouchableOpacityProps & {
9+
/**
10+
* background color for TouchableOpacity
11+
*/
12+
backgroundColor?: string;
13+
/**
14+
* throttle time in MS for onPress callback
15+
*/
16+
throttleTime?: number;
17+
/**
18+
* throttle options {leading, trailing}
19+
*/
20+
throttleOptions?: {leading: boolean, trailing: boolean};
21+
/**
22+
* Apply background color on TouchableOpacity when active (press is on)
23+
*/
24+
activeBackgroundColor?: string;
25+
/**
26+
* Should use a more native touchable opacity component
27+
*/
28+
useNative?: boolean;
29+
};
30+
31+
type Props = BaseComponentInjectedProps & ForwardRefInjectedProps & IProps;
32+
833
/**
934
* @description: A wrapper for TouchableOpacity component. Support onPress, throttling and activeBackgroundColor
1035
* @extends: TouchableOpacity
@@ -13,33 +38,10 @@ import Incubator from '../../incubator';
1338
* @gif: https://media.giphy.com/media/xULW8AMIgw7l31zjm8/giphy.gif
1439
* @example: https://github.com/wix/react-native-ui-lib/blob/master/src/components/touchableOpacity/index.js
1540
*/
16-
class TouchableOpacity extends PureComponent {
41+
class TouchableOpacity extends PureComponent<Props, {active: boolean}> {
1742
static displayName = 'TouchableOpacity';
1843

19-
static propTypes = {
20-
/**
21-
* background color for TouchableOpacity
22-
*/
23-
backgroundColor: PropTypes.string,
24-
/**
25-
* throttle time in MS for onPress callback
26-
*/
27-
throttleTime: PropTypes.number,
28-
/**
29-
* throttle options
30-
*/
31-
throttleOptions: PropTypes.shape({leading: PropTypes.bool, trailing: PropTypes.bool}),
32-
/**
33-
* Apply background color on TouchableOpacity when active (press is on)
34-
*/
35-
activeBackgroundColor: PropTypes.string,
36-
/**
37-
* Should use a more native touchable opacity component
38-
*/
39-
useNative: PropTypes.bool
40-
};
41-
42-
constructor(props) {
44+
constructor(props: Props) {
4345
super(props);
4446

4547
const {throttleTime, throttleOptions} = this.props;
@@ -50,7 +52,6 @@ class TouchableOpacity extends PureComponent {
5052
}
5153

5254
state = {
53-
...this.state,
5455
active: false
5556
};
5657

@@ -62,14 +63,14 @@ class TouchableOpacity extends PureComponent {
6263
};
6364
}
6465

65-
onPressIn(...args) {
66+
onPressIn(...args: any) {
6667
this.setState({
6768
active: true
6869
});
6970
_.invoke(this.props, 'onPressIn', ...args);
7071
}
7172

72-
onPressOut(...args) {
73+
onPressOut(...args: any) {
7374
this.setState({
7475
active: false
7576
});
@@ -103,6 +104,7 @@ class TouchableOpacity extends PureComponent {
103104
}
104105

105106
return (
107+
// @ts-ignore
106108
<RNTouchableOpacity
107109
{...this.getAccessibilityInfo()}
108110
{...others}
@@ -129,4 +131,4 @@ class TouchableOpacity extends PureComponent {
129131
}
130132
}
131133

132-
export default asBaseComponent(forwardRef(TouchableOpacity));
134+
export default asBaseComponent<IProps>(forwardRef(TouchableOpacity));

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
export * from './style';
88
export {default as View} from './components/view';
99
export {default as Text} from './components/text';
10+
export {default as TouchableOpacity} from './components/touchableOpacity';
1011

1112
//================ Manual typings (all those exports should be removed one day) ==========
1213
export {
@@ -15,5 +16,5 @@ export {
1516
PanGestureView, PanListenerView, PanDismissibleView, PanResponderView, Picker, ProgressBar, Slider, GradientSlider,
1617
ColorSliderGroup, Stepper, TabBar, TagsInput, RadioButton, RadioGroup, SharedTransition, StackAggregator, Toast,
1718
WheelPickerDialog, Assets, BaseComponent, PureBaseComponent, UIComponent, forwardRef, AvatarHelper, Constants,
18-
DocsGenerator, LogService, LoaderScreen, Modal, StateScreen, WheelPicker, Incubator, ColorPicker, TouchableOpacity
19+
DocsGenerator, LogService, LoaderScreen, Modal, StateScreen, WheelPicker, Incubator, ColorPicker
1920
} from '../typings';

0 commit comments

Comments
 (0)