|
| 1 | +import React from 'react'; |
| 2 | +import Panel from './CodeMirrorPanel'; |
| 3 | +import Header from './Header'; |
| 4 | +import { parse } from 'react-docgen'; |
| 5 | + |
| 6 | +const codeSample = `import React, { Component } from 'react'; |
| 7 | +import PropTypes from 'prop-types'; |
| 8 | +
|
| 9 | +/** |
| 10 | + * General component description. |
| 11 | + */ |
| 12 | +class MyComponent extends Component { |
| 13 | + render() { |
| 14 | + // ... |
| 15 | + } |
| 16 | +} |
| 17 | +
|
| 18 | +MyComponent.propTypes = { |
| 19 | + /** |
| 20 | + * Description of prop "foo". |
| 21 | + */ |
| 22 | + foo: PropTypes.number, |
| 23 | + /** |
| 24 | + * Description of prop "bar" (a custom validation function). |
| 25 | + */ |
| 26 | + bar: function(props, propName, componentName) { |
| 27 | + // ... |
| 28 | + }, |
| 29 | + baz: PropTypes.oneOfType([ |
| 30 | + PropTypes.number, |
| 31 | + PropTypes.string |
| 32 | + ]).isRequired, |
| 33 | +}; |
| 34 | +
|
| 35 | +MyComponent.defaultProps = { |
| 36 | + foo: 42, |
| 37 | + bar: 21 |
| 38 | +}; |
| 39 | +
|
| 40 | +export default MyComponent; |
| 41 | +`; |
| 42 | + |
| 43 | +const defaultPlugins = [ |
| 44 | + 'jsx', |
| 45 | + 'asyncGenerators', |
| 46 | + 'bigInt', |
| 47 | + 'classProperties', |
| 48 | + 'classPrivateProperties', |
| 49 | + 'classPrivateMethods', |
| 50 | + ['decorators', { decoratorsBeforeExport: false }], |
| 51 | + 'doExpressions', |
| 52 | + 'dynamicImport', |
| 53 | + 'exportDefaultFrom', |
| 54 | + 'exportNamespaceFrom', |
| 55 | + 'functionBind', |
| 56 | + 'functionSent', |
| 57 | + 'importMeta', |
| 58 | + 'logicalAssignment', |
| 59 | + 'nullishCoalescingOperator', |
| 60 | + 'numericSeparator', |
| 61 | + 'objectRestSpread', |
| 62 | + 'optionalCatchBinding', |
| 63 | + 'optionalChaining', |
| 64 | + ['pipelineOperator', { proposal: 'minimal' }], |
| 65 | + 'throwExpressions', |
| 66 | + 'topLevelAwait', |
| 67 | +]; |
| 68 | + |
| 69 | +export default class App extends React.Component { |
| 70 | + constructor() { |
| 71 | + super(); |
| 72 | + this._jsonRef = React.createRef(); |
| 73 | + |
| 74 | + const options = this.buildOptions('js'); |
| 75 | + this.state = { |
| 76 | + value: this.compile(codeSample, options), |
| 77 | + mode: 'application/json', |
| 78 | + content: codeSample, |
| 79 | + options, |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + compile(value, options) { |
| 84 | + return JSON.stringify(parse(value, null, null, options), null, 2); |
| 85 | + } |
| 86 | + |
| 87 | + handleChange = value => { |
| 88 | + let result; |
| 89 | + let mode = 'text/plain'; |
| 90 | + |
| 91 | + try { |
| 92 | + result = this.compile(value, this.state.options); |
| 93 | + mode = 'application/json'; |
| 94 | + } catch (err) { |
| 95 | + result = String(err); |
| 96 | + } |
| 97 | + this.setState({ value: result, mode, content: value }); |
| 98 | + }; |
| 99 | + |
| 100 | + buildOptions(language) { |
| 101 | + const options = { |
| 102 | + babelrc: false, |
| 103 | + babelrcRoots: false, |
| 104 | + configFile: false, |
| 105 | + filename: 'playground.js', |
| 106 | + parserOptions: { |
| 107 | + plugins: [...defaultPlugins], |
| 108 | + }, |
| 109 | + }; |
| 110 | + switch (language) { |
| 111 | + case 'ts': |
| 112 | + options.parserOptions.plugins.push('typescript'); |
| 113 | + options.filename = 'playground.tsx'; |
| 114 | + break; |
| 115 | + case 'flow': |
| 116 | + options.parserOptions.plugins.push('flow'); |
| 117 | + break; |
| 118 | + } |
| 119 | + |
| 120 | + return options; |
| 121 | + } |
| 122 | + |
| 123 | + handleLanguageChange = language => { |
| 124 | + this.setState({ options: this.buildOptions(language) }, () => |
| 125 | + this.handleChange(this.state.content), |
| 126 | + ); |
| 127 | + }; |
| 128 | + |
| 129 | + render() { |
| 130 | + return ( |
| 131 | + <> |
| 132 | + <Header onLanguageChange={this.handleLanguageChange} /> |
| 133 | + <div className="panels"> |
| 134 | + <Panel |
| 135 | + value={this.state.content} |
| 136 | + mode="text/jsx" |
| 137 | + codeSample={codeSample} |
| 138 | + onChange={this.handleChange} |
| 139 | + /> |
| 140 | + <Panel |
| 141 | + readOnly={true} |
| 142 | + ref={this._jsonRef} |
| 143 | + value={this.state.value} |
| 144 | + mode={this.state.mode} |
| 145 | + /> |
| 146 | + </div> |
| 147 | + </> |
| 148 | + ); |
| 149 | + } |
| 150 | +} |
0 commit comments