react-native-webview.git

Alerts.js 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow
  9. */
  10. import React, {Component} from 'react';
  11. import {Text, View} from 'react-native';
  12. import WebView from 'react-native-webview';
  13. const HTML = `
  14. <!DOCTYPE html>\n
  15. <html>
  16. <head>
  17. <title>Alerts</title>
  18. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  19. <meta name="viewport" content="width=320, user-scalable=no">
  20. <style type="text/css">
  21. body {
  22. margin: 0;
  23. padding: 0;
  24. font: 62.5% arial, sans-serif;
  25. background: #ccc;
  26. }
  27. h1 {
  28. padding: 45px;
  29. margin: 0;
  30. text-align: center;
  31. color: #33f;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <button onclick="showAlert()">Show alert</button>
  37. <button onclick="showConfirm()">Show confirm</button>
  38. <button onclick="showPrompt()">Show prompt</button>
  39. <p id="demo"></p>
  40. <script>
  41. function showAlert() {
  42. alert("Hello! I am an alert box!");
  43. document.getElementById("demo").innerHTML = "Alert dismissed!";
  44. }
  45. function showConfirm() {
  46. var response;
  47. if (confirm("Press a button!")) {
  48. response = "You pressed OK on confirm!";
  49. } else {
  50. response = "You pressed Cancel on confirm!";
  51. }
  52. document.getElementById("demo").innerHTML = response;
  53. }
  54. function showPrompt() {
  55. var message;
  56. const name = prompt("Please enter your name", "Name");
  57. if (name !== null) {
  58. message = "Hello " + name;
  59. } else {
  60. message = "You pressed Cancel on prompt!";
  61. }
  62. document.getElementById("demo").innerHTML = message;
  63. }
  64. </script>
  65. </body>
  66. </html>
  67. `;
  68. type Props = {};
  69. type State = {};
  70. export default class Alerts extends Component<Props, State> {
  71. state = {};
  72. render() {
  73. return (
  74. <View style={{ height: 120 }}>
  75. <WebView
  76. source={{html: HTML}}
  77. automaticallyAdjustContentInsets={false}
  78. />
  79. </View>
  80. );
  81. }
  82. }