Skip to content

Commit 777c6d3

Browse files
committed
dev: first version
1 parent 05874c5 commit 777c6d3

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

lib/loading-bar.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
import React from 'react';
22
import { View, StyleSheet } from 'react-native';
3+
import PropTypes from 'prop-types';
34

4-
const LoadingBar = () => (
5-
<View>
5+
const LoadingBar = ({ color, percent }) => {
6+
const style = {
7+
backgroundColor: color,
8+
width: `${percent * 100}%`
9+
};
10+
return <View style={[styles.container, style]} />;
11+
};
612

7-
</View>
8-
);
13+
LoadingBar.propTypes = {
14+
color: PropTypes.string
15+
};
16+
17+
const styles = StyleSheet.create({
18+
container: {
19+
position: 'absolute',
20+
height: 3,
21+
zIndex: 10,
22+
top: 0,
23+
left: 0,
24+
},
25+
});
926

1027
export default LoadingBar;

lib/webview.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,68 @@ export default class ProgressBarWebView extends React.PureComponent {
88

99
static propTypes = {
1010
color: PropTypes.string,
11+
errorColor: PropTypes.string,
12+
disappearDuration: PropTypes.number,
13+
onLoadProgress: PropTypes.func,
14+
onError: PropTypes.func,
15+
onLoadStart: PropTypes.func,
16+
onLoadEnd: PropTypes.func,
1117
};
1218

19+
static defaultProps = {
20+
color: '#3B78E7',
21+
errorColor: '#f30',
22+
disappearDuration: 300,
23+
};
24+
25+
state = {
26+
percent: 0, //range: 0 - 1
27+
color: this.props.color,
28+
visible: false,
29+
};
30+
31+
_onLoadProgress = (syntheticEvent) => {
32+
this.setState({ percent: syntheticEvent.nativeEvent.progress });
33+
const { onLoadProgress } = this.props;
34+
onLoadProgress && onLoadProgress(syntheticEvent);
35+
};
36+
37+
_onError = (syntheticEvent) => {
38+
this.setState({ color: this.props.errorColor, percent: 1 });
39+
const { onError } = this.props;
40+
onError && onError(syntheticEvent);
41+
};
42+
43+
_onLoadStart = (syntheticEvent) => {
44+
this.setState({ visible: true });
45+
const { onLoadStart } = this.props;
46+
onLoadStart && onLoadStart(syntheticEvent);
47+
};
48+
49+
_onLoadEnd = (syntheticEvent) => {
50+
const { onLoadEnd, disappearDuration } = this.props;
51+
this.timer = setTimeout(() => {
52+
this.setState({ visible: false });
53+
}, disappearDuration);
54+
onLoadEnd && onLoadEnd(syntheticEvent);
55+
};
56+
57+
componentWillUnmount(): void {
58+
clearTimeout(this.timer);
59+
}
60+
1361
render() {
62+
const { percent, color, visible } = this.state;
1463
return (
1564
<View style={styles.container}>
16-
<LoadingBar/>
17-
<WebView {...this.props} />
65+
{visible && <LoadingBar color={color} percent={percent}/>}
66+
<WebView
67+
onLoadStart={this._onLoadStart}
68+
onLoadEnd={this._onLoadEnd}
69+
onLoadProgress={this._onLoadProgress}
70+
onError={this._onError}
71+
{...this.props}
72+
/>
1873
</View>
1974
);
2075
}

0 commit comments

Comments
 (0)