Skip to content

Commit 5525aa3

Browse files
authored
Typescript/state screen (#1455)
* index.js -> index.tsx * migrate StateScreen to typecript * added fake type for docs * EmptyStateScreen.js -> EmptyStateScreen.tsx * EmptyStateScreen migration to ts * Support nested text * optional style support * Fix styles * declerations * fix export * types should be tsx and not ts (for docs) * PR comments * export also in generatedTypes + remove manual typings * Uses different image that's working
1 parent 3ac1e49 commit 5525aa3

File tree

8 files changed

+120
-77
lines changed

8 files changed

+120
-77
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import React, {Component} from 'react';
22
import {StyleSheet, ScrollView, View} from 'react-native';
3-
import {StateScreen, Constants, PageControl} from 'react-native-ui-lib';//eslint-disable-line
4-
const localImageSource = require('../../assets/images/empty-state.jpg'); // eslint-disable-line
5-
const remoteImageSource = {uri: 'https://static.pexels.com/photos/169651/pexels-photo-169651.jpeg'};
3+
import {StateScreen, Constants, PageControl} from 'react-native-ui-lib';
4+
const localImageSource = require('../../assets/images/empty-state.jpg');
5+
const remoteImageSource = {uri: 'https://cdn.pixabay.com/photo/2017/04/19/20/10/morning-2243465_1280.jpg'};
66

7-
export default class EmptyStateScreen extends Component {
8-
constructor(props) {
9-
super(props);
10-
this.state = {currentPage: 0};
11-
}
7+
type State = {
8+
currentPage: number;
9+
};
10+
11+
export default class EmptyStateScreen extends Component<{}, State> {
12+
state = {currentPage: 0};
1213

13-
setCurrentPage(offsetX) {
14+
setCurrentPage(offsetX: number) {
1415
if (offsetX >= 0) {
1516
this.setState({
16-
currentPage: Math.floor(offsetX / Constants.screenWidth),
17+
currentPage: Math.floor(offsetX / Constants.screenWidth)
1718
});
1819
}
1920
}
@@ -26,24 +27,24 @@ export default class EmptyStateScreen extends Component {
2627
horizontal
2728
showsHorizontalScrollIndicator={false}
2829
pagingEnabled
29-
onScroll={(event) => {
30+
onScroll={event => {
3031
this.setCurrentPage(event.nativeEvent.contentOffset.x);
3132
}}
3233
scrollEventThrottle={200}
3334
>
3435
<View style={styles.pageView}>
3536
<StateScreen
36-
title='Oppsie (with local image)'
37-
subtitle='Nothing to see here..'
38-
ctaLabel='OK'
37+
title={'Oppsie (with local image)'}
38+
subtitle={'Nothing to see here..'}
39+
ctaLabel={'OK'}
3940
imageSource={localImageSource}
4041
/>
4142
</View>
4243
<View style={styles.pageView}>
4344
<StateScreen
44-
title='Oppsie (with remote image)'
45-
subtitle='Nothing to see here..'
46-
ctaLabel='OK'
45+
title={'Oppsie (with remote image)'}
46+
subtitle={'Nothing to see here..'}
47+
ctaLabel={'OK'}
4748
imageSource={remoteImageSource}
4849
/>
4950
</View>
@@ -53,7 +54,7 @@ export default class EmptyStateScreen extends Component {
5354
position: 'absolute',
5455
bottom: 10,
5556
left: 0,
56-
width: Constants.screenWidth,
57+
width: Constants.screenWidth
5758
}}
5859
numOfPages={2}
5960
currentPage={this.state.currentPage}
@@ -66,10 +67,10 @@ export default class EmptyStateScreen extends Component {
6667
const styles = StyleSheet.create({
6768
container: {
6869
flex: 1,
69-
flexDirection: 'column',
70+
flexDirection: 'column'
7071
},
7172
pageView: {
7273
width: Constants.screenWidth,
73-
height: Constants.screenHeight,
74-
},
74+
height: Constants.screenHeight
75+
}
7576
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react';
2+
import { StateScreenProps } from './types';
3+
export { StateScreenProps };
4+
declare const _default: React.ComponentClass<StateScreenProps & {
5+
useCustomTheme?: boolean | undefined;
6+
}, any>;
7+
export default _default;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/// <reference types="react" />
2+
import { ImageURISource } from 'react-native';
3+
export declare type StateScreenProps = {
4+
/**
5+
* The image source that's showing at the top. use an image that was required locally
6+
*/
7+
imageSource?: ImageURISource;
8+
source?: ImageURISource;
9+
/**
10+
* To to show as the title
11+
*/
12+
title: string;
13+
/**
14+
* Text to to show as the subtitle
15+
*/
16+
subtitle?: string | React.ReactNode;
17+
/**
18+
* Text to to show in the "call to action" button
19+
*/
20+
ctaLabel?: string;
21+
/**
22+
* Action handler for "call to action" button
23+
*/
24+
onCtaPress?: () => void;
25+
/**
26+
* Use to identify the container in tests
27+
*/
28+
testId?: string;
29+
testID?: string;
30+
};

generatedTypes/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export {default as PageControl, PageControlProps} from './components/pageControl
8080
export {default as Carousel, CarouselProps, PageControlPosition} from './components/carousel';
8181
export {default as ActionSheet} from './components/actionSheet';
8282
export {default as Wizard, WizardProps, WizardStepProps, WizardStepStates, WizardStepConfig, WizardStepsConfig} from './components/wizard';
83+
export {default as StateScreen, StateScreenProps} from './components/stateScreen';
8384
export {default as LoaderScreen, LoaderScreenProps} from './components/loaderScreen';
8485

8586
/* All components with manual typings */
@@ -102,7 +103,6 @@ export {
102103
UIComponent,
103104
forwardRef,
104105
AvatarHelper,
105-
StateScreen,
106106
WheelPicker,
107107
WheelPickerProps,
108108
Picker,

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

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,32 @@
11
import _ from 'lodash';
2-
import PropTypes from 'prop-types';
3-
import React from 'react';
2+
import React, {Component} from 'react';
43
import {StyleSheet} from 'react-native';
54
import {LogService} from '../../services';
65
import {Constants} from '../../helpers';
76
import {Typography, Colors} from '../../style';
8-
import {BaseComponent} from '../../commons';
7+
import {asBaseComponent} from '../../commons/new';
98
import View from '../../components/view';
109
import Image from '../../components/image';
1110
import Button from '../../components/button';
1211
import Text from '../../components/text';
12+
import {StateScreenProps} from './types';
1313

14-
/**
15-
* @description: Component that shows a full screen for a certain state, like an empty state
16-
* @image: https://user-images.githubusercontent.com/33805983/34672894-f262ab84-f488-11e7-83f0-4ee0f0ac34ba.png
17-
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/EmptyStateScreen.js
18-
*/
19-
export default class StateScreen extends BaseComponent {
14+
class StateScreen extends Component<StateScreenProps> {
2015
static displayName = 'StateScreen';
21-
static propTypes = {
22-
/**
23-
* The image source that's showing at the top. use an image that was required locally
24-
*/
25-
imageSource: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
26-
source: PropTypes.oneOfType([PropTypes.object, PropTypes.number]), // TODO: remove after deprecation
27-
/**
28-
* To to show as the title
29-
*/
30-
title: PropTypes.string.isRequired,
31-
/**
32-
* Text to to show as the subtitle
33-
*/
34-
subtitle: PropTypes.any,
35-
/**
36-
* Text to to show in the "call to action" button
37-
*/
38-
ctaLabel: PropTypes.string,
39-
/**
40-
* Action handler for "call to action" button
41-
*/
42-
onCtaPress: PropTypes.func,
43-
/**
44-
* Use to identify the container in tests
45-
*/
46-
testId: PropTypes.string
47-
};
4816

49-
constructor(props) {
17+
styles: any;
18+
constructor(props: StateScreenProps) {
5019
super(props);
51-
20+
5221
if (props.testId) {
5322
LogService.deprecationWarn({component: 'StateScreen', oldProp: 'testId', newProp: 'testID'});
5423

5524
}
5625
if (props.source) {
5726
LogService.deprecationWarn({component: 'StateScreen', oldProp: 'source', newProp: 'imageSource'});
5827
}
28+
29+
this.generateStyles();
5930
}
6031

6132
generateStyles() {
@@ -88,7 +59,10 @@ export default class StateScreen extends BaseComponent {
8859
}
8960
}
9061

91-
function createStyles(isRemoteImage) {
62+
export {StateScreenProps};
63+
export default asBaseComponent<StateScreenProps>(StateScreen);
64+
65+
function createStyles(isRemoteImage: boolean) {
9266
const imageStyle = _.merge({height: 200}, isRemoteImage && {width: Constants.screenWidth * 0.9});
9367
return StyleSheet.create({
9468
container: {

src/components/stateScreen/types.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {PureComponent} from 'react';
2+
import {ImageURISource} from 'react-native';
3+
4+
export type StateScreenProps = {
5+
/**
6+
* The image source that's showing at the top. use an image that was required locally
7+
*/
8+
imageSource?: ImageURISource;
9+
source?: ImageURISource; // TODO: remove after deprecation
10+
/**
11+
* To to show as the title
12+
*/
13+
title: string;
14+
/**
15+
* Text to to show as the subtitle
16+
*/
17+
subtitle?: string | React.ReactNode;
18+
/**
19+
* Text to to show in the "call to action" button
20+
*/
21+
ctaLabel?: string;
22+
/**
23+
* Action handler for "call to action" button
24+
*/
25+
onCtaPress?: () => void;
26+
/**
27+
* Use to identify the container in tests
28+
*/
29+
testId?: string;
30+
testID?: string;
31+
};
32+
33+
/**
34+
* @description: Component that shows a full screen for a certain state, like an empty state
35+
* @image: https://user-images.githubusercontent.com/33805983/34672894-f262ab84-f488-11e7-83f0-4ee0f0ac34ba.png
36+
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/EmptyStateScreen.tsx
37+
*/
38+
// @ts-ignore
39+
class FakeStateScreenForDocs extends PureComponent<StateScreenProps> { // eslint-disable-line
40+
static displayName = 'StateScreen';
41+
42+
render() {
43+
return null;
44+
}
45+
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export {default as Slider, SliderProps} from './components/slider';
7878
export {default as GradientSlider, GradientSliderProps} from './components/slider/GradientSlider';
7979
export {default as ColorSliderGroup, ColorSliderGroupProps} from './components/slider/ColorSliderGroup';
8080
export {default as LogService} from './services/LogService';
81+
export {default as StateScreen, StateScreenProps} from './components/stateScreen';
8182
export {default as LoaderScreen, LoaderScreenProps} from './components/loaderScreen';
8283

8384
//================ Manual typings (all those exports should be removed one day) ==========
@@ -87,5 +88,5 @@ export {
8788
PickerProps, ProgressBar, Stepper,
8889
TagsInput, SharedTransition, Toast, WheelPickerDialog, Assets,
8990
BaseComponent, PureBaseComponent, UIComponent, forwardRef,
90-
StateScreen, WheelPicker, WheelPickerProps
91+
WheelPicker, WheelPickerProps
9192
} from '../typings';

typings/components/StateScreen.d.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)