No Description

App.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. import React, { Component } from 'react';
  3. import { ScrollView, StyleSheet, Text, TouchableOpacity } from 'react-native';
  4. import AutoHeightWebView from 'react-native-autoheight-webview';
  5. import {
  6. autoHeightHtml0,
  7. autoHeightHtml1,
  8. autoHeightScript,
  9. autoHeightStyle0,
  10. autoWidthHtml0,
  11. autoWidthHtml1,
  12. autoWidthScript,
  13. autoWidthStyle0
  14. } from './config';
  15. export default class Explorer extends Component {
  16. constructor(props) {
  17. super(props);
  18. this.state = {
  19. html: autoHeightHtml0,
  20. script: null,
  21. webViewStyle: null,
  22. size: {
  23. height: 0,
  24. width: 0
  25. }
  26. };
  27. }
  28. changeSource = () => {
  29. this.setState(prevState => ({
  30. html: prevState.html === autoHeightHtml0 ? autoHeightHtml1 : autoHeightHtml0
  31. }));
  32. };
  33. changeStyle = () => {
  34. this.setState(prevState => ({
  35. webViewStyle: prevState.webViewStyle == null ? autoHeightStyle0 : null
  36. }));
  37. };
  38. changeScript = () => {
  39. this.setState(prevState => ({
  40. script: prevState.script === null ? autoHeightScript : null
  41. }));
  42. };
  43. render() {
  44. const { html, size, webViewStyle, script } = this.state;
  45. return (
  46. <ScrollView
  47. style={{
  48. backgroundColor: 'lightyellow'
  49. }}
  50. contentContainerStyle={{
  51. justifyContent: 'center',
  52. alignItems: 'center'
  53. }}
  54. >
  55. <AutoHeightWebView
  56. customStyle={webViewStyle}
  57. onError={() => console.log('on error')}
  58. onLoad={() => console.log('on load')}
  59. onLoadStart={() => console.log('on load start')}
  60. onLoadEnd={() => console.log('on load end')}
  61. onShouldStartLoadWithRequest={result => {
  62. console.log(result);
  63. return true;
  64. }}
  65. onSizeUpdated={size => this.setState({ size })}
  66. source={{ html }}
  67. customScript={script}
  68. />
  69. <TouchableOpacity onPress={this.changeSource} style={styles.button}>
  70. <Text>change source</Text>
  71. </TouchableOpacity>
  72. <TouchableOpacity onPress={this.changeStyle} style={styles.button}>
  73. <Text>change style</Text>
  74. </TouchableOpacity>
  75. <TouchableOpacity onPress={this.changeScript} style={styles.button}>
  76. <Text>change script (have to change source to reload on android)</Text>
  77. </TouchableOpacity>
  78. <Text style={{ padding: 5 }}>
  79. height: {size.height}, width: {size.width}
  80. </Text>
  81. </ScrollView>
  82. );
  83. }
  84. }
  85. const styles = StyleSheet.create({
  86. button: {
  87. marginTop: 15,
  88. backgroundColor: 'aliceblue',
  89. borderRadius: 5,
  90. padding: 5
  91. }
  92. });