Guest User

Untitled

a guest
Jan 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import ReactDOM from "react-dom";
  3. import jsx from 'react-jsx';
  4. import PropTypes from 'prop-types';
  5.  
  6. window.React = React; // ??????
  7.  
  8. class Form extends Component {
  9. constructor(...args) {
  10. super(...args);
  11. this.state = {
  12. value: '',
  13. };
  14. }
  15.  
  16. onValueChange = (e) => {
  17. this.setState({ value: e.target.value });
  18. }
  19.  
  20. getChildContext() {
  21. return {
  22. onSubmit: this.onSubmit,
  23. value: this.state.value,
  24. controlFunc: this.onValueChange
  25. };
  26. }
  27.  
  28. onSubmit() {
  29. alert('Form clicked');
  30. }
  31.  
  32. render() {
  33. return (
  34. <div>
  35. {this.state.value}
  36. {this.props.children}
  37. </div>
  38. );
  39. }
  40. }
  41.  
  42. Form.childContextTypes = {
  43. onSubmit: PropTypes.func,
  44. value: PropTypes.oneOfType([
  45. PropTypes.string,
  46. PropTypes.number,
  47. ]),
  48. controlFunc: PropTypes.func,
  49. };
  50.  
  51. class Button extends Component {
  52. render() {
  53. return (
  54. <div onClick={this.context.onSubmit}>
  55. Button
  56. </div>
  57. )
  58. }
  59. }
  60.  
  61. Button.contextTypes = {
  62. onSubmit: PropTypes.func
  63. }
  64.  
  65. class Input extends Component {
  66. render() {
  67. return (
  68. <input type="text" value={this.context.value} onChange={this.context.controlFunc} />
  69. );
  70. }
  71. }
  72.  
  73. Input.contextTypes = {
  74. value: PropTypes.oneOfType([
  75. PropTypes.string,
  76. PropTypes.number,
  77. ]),
  78. controlFunc: PropTypes.func,
  79. }
  80.  
  81. const UIMappingForm = {
  82. Form,
  83. Button,
  84. Input,
  85. };
  86.  
  87. export class StringToJsxComponent extends Component {
  88. render() {
  89. return jsx
  90. .client(`
  91. <Form>
  92. <Input />
  93. <Button/>
  94. </Form>
  95. `)(UIMappingForm);
  96. }
  97. }
  98.  
  99.  
  100. ReactDOM.render(
  101. <StringToJsxComponent />,
  102. document.getElementById("root")
  103. );
Add Comment
Please, Sign In to add comment