Skip to content

Commit 4ae1bf3

Browse files
authored
Typescript/connection status bar (#1384)
* ConnectionStatusBar migrate to typescript * migrate screen and index to typescript * docs * generated types * types file and export asBaseComponent * Fix tests import
1 parent 0d54108 commit 4ae1bf3

File tree

7 files changed

+148
-44
lines changed

7 files changed

+148
-44
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ import React, {Component} from 'react';
22
import {View, Text, StyleSheet} from 'react-native';
33
import {ConnectionStatusBar, Typography, Colors} from 'react-native-ui-lib'; //eslint-disable-line
44

5+
type ConnectionStatusBarScreenState = {
6+
isConnected: boolean;
7+
};
8+
59
ConnectionStatusBar.registerGlobalOnConnectionLost(() => {
610
// console.warn('what what?!? connection has been lost');
711
});
812

9-
export default class ConnectionStatusBarScreen extends Component {
10-
constructor(props) {
13+
export default class ConnectionStatusBarScreen extends Component<{}, ConnectionStatusBarScreenState> {
14+
constructor(props: any) {
1115
super(props);
1216
this.state = {
13-
isConnected: true,
17+
isConnected: true
1418
};
1519
}
1620

@@ -25,7 +29,7 @@ export default class ConnectionStatusBarScreen extends Component {
2529
textAlign: 'center',
2630
marginBottom: 10,
2731
...Typography.text60,
28-
color: Colors.dark40,
32+
color: Colors.dark40
2933
}}
3034
>
3135
Turn your wifi on/off to see the component in action
@@ -43,6 +47,6 @@ const styles = StyleSheet.create({
4347
flex: 1,
4448
alignItems: 'center',
4549
justifyContent: 'center',
46-
paddingHorizontal: 25,
47-
},
50+
paddingHorizontal: 25
51+
}
4852
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export declare type ConnectionStatusBarProps = {
2+
/**
3+
* Text to show as the status
4+
*/
5+
label?: string;
6+
/**
7+
* Handler to get connection change events propagation
8+
*/
9+
onConnectionChange?: (isConnected: boolean, isInitial: boolean) => void;
10+
/**
11+
* Text to show as the status
12+
*/
13+
allowDismiss?: boolean;
14+
/**
15+
* Use absolute position for the component
16+
*/
17+
useAbsolutePosition?: boolean;
18+
};
19+
export declare type ConnectionStatusBarState = {
20+
type?: string;
21+
isConnected: boolean;
22+
isCancelled: boolean;
23+
};
24+
export declare const DEFAULT_PROPS: {
25+
label: string;
26+
allowDismiss: boolean;
27+
useAbsolutePosition: boolean;
28+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React, { PureComponent } from 'react';
2+
import { ConnectionStatusBarProps, ConnectionStatusBarState } from './Types';
3+
export { ConnectionStatusBarProps };
4+
/**
5+
* @description: Top bar to show a "no internet" connection status. Note: Run on real device for best results
6+
* @image: https://user-images.githubusercontent.com/33805983/34683190-f3b1904c-f4a9-11e7-9d46-9a340bd35448.png, https://user-images.githubusercontent.com/33805983/34484206-edc6c6e4-efcb-11e7-88b2-cd394c19dd5e.png
7+
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/ConnectionStatusBarScreen.js
8+
* @notes: The component requires installing the '@react-native-community/netinfo' native library
9+
*/
10+
declare class ConnectionStatusBar extends PureComponent<ConnectionStatusBarProps, ConnectionStatusBarState> {
11+
static displayName: string;
12+
static defaultProps: {
13+
label: string;
14+
allowDismiss: boolean;
15+
useAbsolutePosition: boolean;
16+
};
17+
styles?: any;
18+
unsubscribe?: any;
19+
static onConnectionLost?: () => void;
20+
static registerGlobalOnConnectionLost(callback: () => void): void;
21+
static unregisterGlobalOnConnectionLost(): void;
22+
constructor(props: ConnectionStatusBarProps);
23+
generateStyles(): void;
24+
componentDidMount(): void;
25+
componentWillUnmount(): void;
26+
onConnectionChange(state: ConnectionStatusBarState): void;
27+
getInitialConnectionState(): Promise<void>;
28+
isStateConnected(state: ConnectionStatusBarState): boolean;
29+
render(): false | JSX.Element;
30+
}
31+
export { ConnectionStatusBar };
32+
declare const _default: React.ComponentClass<ConnectionStatusBarProps & {
33+
useCustomTheme?: boolean | undefined;
34+
}, any> & typeof ConnectionStatusBar;
35+
export default _default;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {PureComponent} from 'react';
2+
3+
export type ConnectionStatusBarProps = {
4+
/**
5+
* Text to show as the status
6+
*/
7+
label?: string;
8+
/**
9+
* Handler to get connection change events propagation
10+
*/
11+
onConnectionChange?: (isConnected: boolean, isInitial: boolean) => void;
12+
/**
13+
* Text to show as the status
14+
*/
15+
allowDismiss?: boolean;
16+
/**
17+
* Use absolute position for the component
18+
*/
19+
useAbsolutePosition?: boolean;
20+
};
21+
22+
export type ConnectionStatusBarState = {
23+
/* Current connection type */
24+
type?: string;
25+
isConnected: boolean;
26+
isCancelled: boolean;
27+
};
28+
29+
export const DEFAULT_PROPS = {
30+
label: 'No internet. Check your connection.',
31+
allowDismiss: false,
32+
useAbsolutePosition: true
33+
};
34+
35+
/**
36+
* @description: Top bar to show a "no internet" connection status. Note: Run on real device for best results
37+
* @image: https://user-images.githubusercontent.com/33805983/34683190-f3b1904c-f4a9-11e7-9d46-9a340bd35448.png, https://user-images.githubusercontent.com/33805983/34484206-edc6c6e4-efcb-11e7-88b2-cd394c19dd5e.png
38+
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/ConnectionStatusBarScreen.js
39+
* @notes: The component requires installing the '@react-native-community/netinfo' native library
40+
*/
41+
// @ts-ignore
42+
class FakeConnectionStatusBarForDocs extends PureComponent<ConnectionStatusBarProps, ConnectionStatusBarState> { // eslint-disable-line
43+
static displayName = 'ConnectionStatusBar';
44+
45+
static defaultProps = DEFAULT_PROPS;
46+
47+
render() {
48+
return null;
49+
}
50+
}

src/components/connectionStatusBar/__tests__/index.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import _ from 'lodash';
2-
import ConnectionStatusBar from '../index';
2+
import {ConnectionStatusBar} from '../index';
33

44
describe('ConnectionStatusBar', () => {
55
let uut;

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

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,39 @@
1-
import React from 'react';
2-
import PropTypes from 'prop-types';
1+
import React, {PureComponent} from 'react';
32
import _ from 'lodash';
43
import {StyleSheet, Text} from 'react-native';
54
import {NetInfoPackage as NetInfo} from '../../optionalDependencies';
65
import {Constants} from '../../helpers';
7-
import {PureBaseComponent} from '../../commons';
86
import {Colors, Typography} from '../../style';
97
import TouchableOpacity from '../touchableOpacity';
108
import View from '../view';
9+
import {asBaseComponent} from '../../commons/new';
10+
import {ConnectionStatusBarProps, ConnectionStatusBarState, DEFAULT_PROPS} from './Types';
11+
export {ConnectionStatusBarProps};
1112

1213
/**
1314
* @description: Top bar to show a "no internet" connection status. Note: Run on real device for best results
1415
* @image: https://user-images.githubusercontent.com/33805983/34683190-f3b1904c-f4a9-11e7-9d46-9a340bd35448.png, https://user-images.githubusercontent.com/33805983/34484206-edc6c6e4-efcb-11e7-88b2-cd394c19dd5e.png
1516
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/ConnectionStatusBarScreen.js
1617
* @notes: The component requires installing the '@react-native-community/netinfo' native library
1718
*/
18-
export default class ConnectionStatusBar extends PureBaseComponent {
19+
class ConnectionStatusBar extends PureComponent<ConnectionStatusBarProps, ConnectionStatusBarState> {
1920
static displayName = 'ConnectionStatusBar';
20-
static propTypes = {
21-
/**
22-
* Text to show as the status
23-
*/
24-
label: PropTypes.string,
25-
/**
26-
* Handler to get connection change events propagation
27-
*/
28-
onConnectionChange: PropTypes.func,
29-
/**
30-
* Text to show as the status
31-
*/
32-
allowDismiss: PropTypes.bool,
33-
34-
/**
35-
* Use absolute position for the component
36-
*/
37-
useAbsolutePosition: PropTypes.bool
38-
};
39-
40-
static defaultProps = {
41-
label: 'No internet. Check your connection.',
42-
allowDismiss: false,
43-
useAbsolutePosition: true
44-
};
45-
46-
static onConnectionLost;
47-
static registerGlobalOnConnectionLost(callback) {
21+
22+
static defaultProps = DEFAULT_PROPS;
23+
24+
styles?: any;
25+
unsubscribe?: any;
26+
static onConnectionLost?: () => void;
27+
28+
static registerGlobalOnConnectionLost(callback: () => void) {
4829
ConnectionStatusBar.onConnectionLost = callback;
4930
}
5031

5132
static unregisterGlobalOnConnectionLost() {
5233
delete ConnectionStatusBar.onConnectionLost;
5334
}
5435

55-
constructor(props) {
36+
constructor(props: ConnectionStatusBarProps) {
5637
super(props);
5738
this.onConnectionChange = this.onConnectionChange.bind(this);
5839

@@ -82,7 +63,7 @@ export default class ConnectionStatusBar extends PureBaseComponent {
8263
}
8364
}
8465

85-
onConnectionChange(state) {
66+
onConnectionChange(state: ConnectionStatusBarState) {
8667
const isConnected = this.isStateConnected(state);
8768
if (isConnected !== this.state.isConnected) {
8869
this.setState({
@@ -114,7 +95,7 @@ export default class ConnectionStatusBar extends PureBaseComponent {
11495
}
11596
}
11697

117-
isStateConnected(state) {
98+
isStateConnected(state: ConnectionStatusBarState) {
11899
const lowerCaseState = _.lowerCase(state.type);
119100
const isConnected = lowerCaseState !== 'none';
120101
return isConnected;
@@ -174,8 +155,12 @@ function createStyles() {
174155
alignSelf: 'center'
175156
},
176157
x: {
177-
fontSize: Typography.text80.fontSize,
158+
fontSize: Typography.text80?.fontSize,
178159
color: Colors.black
179160
}
180161
});
181162
}
163+
164+
export {ConnectionStatusBar}; // For tests
165+
166+
export default asBaseComponent<ConnectionStatusBarProps, typeof ConnectionStatusBar>(ConnectionStatusBar);

typings/components/ConnectionStatusBar.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ export interface ConnectionStatusBarProps {
88
useAbsolutePosition?: boolean;
99
}
1010

11-
export class ConnectionStatusBar extends PureBaseComponent<ConnectionStatusBarProps> {}
11+
export class ConnectionStatusBar extends PureBaseComponent<ConnectionStatusBarProps> {
12+
static registerGlobalOnConnectionLost: any;
13+
}

0 commit comments

Comments
 (0)