|
| 1 | +/* |
| 2 | + * Author: Alexandre Havrileck (Oxyno-zeta) |
| 3 | + * Date: 30/10/16 |
| 4 | + * Licence: See Readme |
| 5 | + */ |
| 6 | +/* ************************************* */ |
| 7 | +/* ******** IMPORTS ******** */ |
| 8 | +/* ************************************* */ |
| 9 | +import React, { Component } from 'react'; |
| 10 | +import { JsonTree } from '../../src/JsonTree.jsx'; |
| 11 | + |
| 12 | +/* ************************************* */ |
| 13 | +/* ******** VARIABLES ******** */ |
| 14 | +/* ************************************* */ |
| 15 | +// Prop types |
| 16 | +const propTypes = {}; |
| 17 | +// Default props |
| 18 | +const defaultProps = {}; |
| 19 | + |
| 20 | +const defaultJson = { |
| 21 | + error: new Error('error'), |
| 22 | + func: () => { |
| 23 | + console.log('test'); |
| 24 | + }, |
| 25 | + text: 'text', |
| 26 | + int: 100, |
| 27 | + boolean: true, |
| 28 | + null: null, |
| 29 | + undefined: undefined, |
| 30 | + object: { |
| 31 | + text: 'text', |
| 32 | + int: 100, |
| 33 | + boolean: true, |
| 34 | + }, |
| 35 | + array: [ |
| 36 | + 1, |
| 37 | + 2, |
| 38 | + 3, |
| 39 | + { |
| 40 | + string: 'test', |
| 41 | + }, |
| 42 | + ], |
| 43 | +}; |
| 44 | + |
| 45 | +/* ************************************* */ |
| 46 | +/* ******** COMPONENT ******** */ |
| 47 | +/* ************************************* */ |
| 48 | +class Body extends Component { |
| 49 | + constructor(props) { |
| 50 | + super(props); |
| 51 | + this.state = { |
| 52 | + json: defaultJson, |
| 53 | + deltaUpdateString: '{}', |
| 54 | + globalUpdateString: '{}', |
| 55 | + textareaRef: null, |
| 56 | + readOnlyRef: null, |
| 57 | + readOnly: false, |
| 58 | + }; |
| 59 | + // Bind |
| 60 | + this.onFullyUpdate = this.onFullyUpdate.bind(this); |
| 61 | + this.onDeltaUpdate = this.onDeltaUpdate.bind(this); |
| 62 | + this.refTextarea = this.refTextarea.bind(this); |
| 63 | + this.refReadOnlyCheckbox = this.refReadOnlyCheckbox.bind(this); |
| 64 | + this.handleSubmit = this.handleSubmit.bind(this); |
| 65 | + this.handleResetToDefault = this.handleResetToDefault.bind(this); |
| 66 | + this.handleChangeReadOnly = this.handleChangeReadOnly.bind(this); |
| 67 | + } |
| 68 | + |
| 69 | + onFullyUpdate(newJson) { |
| 70 | + this.setState({ |
| 71 | + globalUpdateString: JSON.stringify(newJson, null, 4), |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + onDeltaUpdate(deltaUpdate) { |
| 76 | + this.setState({ |
| 77 | + deltaUpdateString: JSON.stringify(deltaUpdate, null, 4), |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + refTextarea(node) { |
| 82 | + this.state.textareaRef = node; |
| 83 | + } |
| 84 | + |
| 85 | + refReadOnlyCheckbox(node) { |
| 86 | + this.state.readOnlyRef = node; |
| 87 | + } |
| 88 | + |
| 89 | + handleSubmit() { |
| 90 | + const { textareaRef } = this.state; |
| 91 | + // Get data |
| 92 | + const jsonString = textareaRef.value; |
| 93 | + |
| 94 | + try { |
| 95 | + const json = JSON.parse(jsonString); |
| 96 | + this.setState({ |
| 97 | + json, |
| 98 | + }); |
| 99 | + // Reset value |
| 100 | + textareaRef.value = ''; |
| 101 | + } catch (e) { |
| 102 | + // Nothing |
| 103 | + console.error(e); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + handleResetToDefault() { |
| 108 | + this.setState({ |
| 109 | + json: defaultJson, |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + handleChangeReadOnly() { |
| 114 | + const { readOnlyRef } = this.state; |
| 115 | + |
| 116 | + this.setState({ |
| 117 | + readOnly: readOnlyRef.checked, |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + render() { |
| 122 | + const { json, deltaUpdateString, globalUpdateString, readOnly } = this.state; |
| 123 | + |
| 124 | + const style1 = { |
| 125 | + width: '100%', |
| 126 | + }; |
| 127 | + const style2 = { |
| 128 | + verticalAlign: 'top', |
| 129 | + }; |
| 130 | + const style3 = { |
| 131 | + backgroundColor: '#e0e0e0', |
| 132 | + border: '1px lightgrey solid', |
| 133 | + }; |
| 134 | + const style4 = { |
| 135 | + margin: '0 15px', |
| 136 | + minWidth: '200px', |
| 137 | + }; |
| 138 | + return ( |
| 139 | + <div> |
| 140 | + <div style={style4}> |
| 141 | + <input |
| 142 | + type="checkbox" |
| 143 | + ref={this.refReadOnlyCheckbox} |
| 144 | + onChange={this.handleChangeReadOnly} |
| 145 | + />Read Only |
| 146 | + </div> |
| 147 | + <table style={style1}> |
| 148 | + <thead> |
| 149 | + <tr> |
| 150 | + <th>Result</th> |
| 151 | + <th>Global Update</th> |
| 152 | + <th>Delta Update</th> |
| 153 | + <th>Put Your Json</th> |
| 154 | + </tr> |
| 155 | + </thead> |
| 156 | + <tbody> |
| 157 | + <tr> |
| 158 | + <td style={style2}> |
| 159 | + <div style={style4}> |
| 160 | + <JsonTree |
| 161 | + data={json} |
| 162 | + onFullyUpdate={this.onFullyUpdate} |
| 163 | + onDeltaUpdate={this.onDeltaUpdate} |
| 164 | + readOnly={readOnly} |
| 165 | + /> |
| 166 | + </div> |
| 167 | + </td> |
| 168 | + <td style={style2}> |
| 169 | + <div style={style4}> |
| 170 | + <pre style={style3}>{globalUpdateString}</pre> |
| 171 | + </div> |
| 172 | + </td> |
| 173 | + <td style={style2}> |
| 174 | + <div style={style4}> |
| 175 | + <pre style={style3}>{deltaUpdateString}</pre> |
| 176 | + </div> |
| 177 | + </td> |
| 178 | + <td style={style2}> |
| 179 | + <div style={style4}> |
| 180 | + <textarea ref={this.refTextarea} rows="15" cols="40" /> |
| 181 | + <div> |
| 182 | + <button onClick={this.handleSubmit}>Submit</button> |
| 183 | + <button onClick={this.handleResetToDefault}>Default</button> |
| 184 | + </div> |
| 185 | + </div> |
| 186 | + </td> |
| 187 | + </tr> |
| 188 | + </tbody> |
| 189 | + </table> |
| 190 | + </div> |
| 191 | + ); |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +// Add prop types |
| 196 | +Body.propTypes = propTypes; |
| 197 | +// Add default props |
| 198 | +Body.defaultProps = defaultProps; |
| 199 | + |
| 200 | +/* ************************************* */ |
| 201 | +/* ******** EXPORTS ******** */ |
| 202 | +/* ************************************* */ |
| 203 | +export default Body; |
0 commit comments