@@ -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