Skip to content

Added support for loadingStyle, loadingSize and containerStyle #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ Enable horizontal swipe gestures will trigger back-forward navigations. Derived
- source
- startInLoadingState
- style
- containerStyle (optional)
- loadingStyle (optional)
- loadingSize (optional)
- url (deprecated)
- bounces
- onShouldStartLoadWithRequest
Expand Down
17 changes: 9 additions & 8 deletions WKWebView.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ type ErrorEvent = {

type Event = Object;

const defaultRenderLoading = () => (
<View style={styles.loadingView}>
<ActivityIndicator />
const defaultRenderLoading = (loadingStyle, loadingSize) => (
<View style={[styles.loadingView, loadingStyle]}>
<ActivityIndicator size={loadingSize || null} />
</View>
);
const defaultRenderError = (errorDomain, errorCode, errorDesc) => (
Expand Down Expand Up @@ -203,6 +203,9 @@ class WKWebView extends React.Component {
scalesPageToFit: PropTypes.bool,
startInLoadingState: PropTypes.bool,
style: ViewPropTypes.style,
containerStyle: ViewPropTypes.style,
loadingStyle: ViewPropTypes.style,
loadingSize: null | "small" | "large",
/**
* If false injectJavaScript will run both main frame and iframe
* @platform ios
Expand Down Expand Up @@ -290,7 +293,7 @@ class WKWebView extends React.Component {
let otherView = null;

if (this.state.viewState === WebViewState.LOADING) {
otherView = (this.props.renderLoading || defaultRenderLoading)();
otherView = (this.props.renderLoading || defaultRenderLoading)(this.props.loadingStyle, this.props.loadingSize);
} else if (this.state.viewState === WebViewState.ERROR) {
const errorEvent = this.state.lastErrorEvent;
invariant(
Expand All @@ -303,9 +306,7 @@ class WKWebView extends React.Component {
errorEvent.description
);
} else if (this.state.viewState !== WebViewState.IDLE) {
console.error(
'CRAWKWebView invalid state encountered: ' + this.state.loading
);
console.error('CRAWKWebView invalid state encountered: ' + this.state.loading);
}

const webViewStyles = [styles.container, styles.webView, this.props.style];
Expand Down Expand Up @@ -372,7 +373,7 @@ class WKWebView extends React.Component {
/>;

return (
<View style={styles.container}>
<View style={[styles.container, this.props.containerStyle]}>
{webView}
{otherView}
</View>
Expand Down
19 changes: 13 additions & 6 deletions ios/RCTWKWebView/RCTWKWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -614,20 +614,27 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNaviga
NSDictionary *headers = @{};
NSInteger statusCode = 200;
if([navigationResponse.response isKindOfClass:[NSHTTPURLResponse class]]){
headers = ((NSHTTPURLResponse *)navigationResponse.response).allHeaderFields;
statusCode = ((NSHTTPURLResponse *)navigationResponse.response).statusCode;
headers = ((NSHTTPURLResponse *)navigationResponse.response).allHeaderFields;
statusCode = ((NSHTTPURLResponse *)navigationResponse.response).statusCode;
}

NSMutableDictionary<NSString *, id> *event = [self baseEvent];
[event addEntriesFromDictionary:@{
@"headers": headers,
@"status": [NSHTTPURLResponse localizedStringForStatusCode:statusCode],
@"statusCode": @(statusCode),
}];
_onNavigationResponse(event);
// check status code
if(statusCode == 200) {
_onNavigationResponse(event);
decisionHandler(WKNavigationResponsePolicyAllow);
} else {
RCTLogWarn(@"Something wrong, statusCode is %zd!", statusCode);
decisionHandler(WKNavigationResponsePolicyCancel);
}
} else {
decisionHandler(WKNavigationResponsePolicyAllow);
}

decisionHandler(WKNavigationResponsePolicyAllow);
}

@end
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@
"sync-from-example": "cp ./example/node_modules/react-native-wkwebview-reborn/*.js ./;cp -r ./example/node_modules/react-native-wkwebview-reborn/ios ./",
"sync-to-example": "cp ./*.js ./example/node_modules/react-native-wkwebview-reborn/;cp -r ./ios ./example/node_modules/react-native-wkwebview-reborn/"
},
"version": "1.22.0"
"version": "1.23.0"
}