Skip to content

Add support for language selection #423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@
},
"dependencies": {},
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/plugin-proposal-class-properties": "^7.3.0",
"@babel/preset-env": "^7.3.1",
"@babel/preset-flow": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.5",
"codemirror": "^5.43.0",
"css-loader": "^2.1.0",
"@babel/core": "^7.8.3",
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/preset-env": "^7.8.3",
"@babel/preset-flow": "^7.8.3",
"@babel/preset-react": "^7.8.3",
"babel-loader": "^8.0.6",
"codemirror": "^5.50.2",
"css-loader": "^3.4.2",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"less": "^3.9.0",
"less": "^3.10.3",
"less-loader": "^5.0.0",
"mini-css-extract-plugin": "^0.6.0",
"optimize-css-assets-webpack-plugin": "^5.0.1",
"react": "^16.7.0",
"react-dom": "^16.7.0",
"terser-webpack-plugin": "^1.2.1",
"webpack": "^4.29.0",
"webpack-cli": "^3.2.1",
"webpack-dev-server": "^3.1.14"
"mini-css-extract-plugin": "^0.9.0",
"optimize-css-assets-webpack-plugin": "^5.0.3",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"terser-webpack-plugin": "^2.3.2",
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.10.1"
}
}
150 changes: 150 additions & 0 deletions website/src/components/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import React from 'react';
import Panel from './CodeMirrorPanel';
import Header from './Header';
import { parse } from 'react-docgen';

const codeSample = `import React, { Component } from 'react';
import PropTypes from 'prop-types';

/**
* General component description.
*/
class MyComponent extends Component {
render() {
// ...
}
}

MyComponent.propTypes = {
/**
* Description of prop "foo".
*/
foo: PropTypes.number,
/**
* Description of prop "bar" (a custom validation function).
*/
bar: function(props, propName, componentName) {
// ...
},
baz: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]).isRequired,
};

MyComponent.defaultProps = {
foo: 42,
bar: 21
};

export default MyComponent;
`;

const defaultPlugins = [
'jsx',
'asyncGenerators',
'bigInt',
'classProperties',
'classPrivateProperties',
'classPrivateMethods',
['decorators', { decoratorsBeforeExport: false }],
'doExpressions',
'dynamicImport',
'exportDefaultFrom',
'exportNamespaceFrom',
'functionBind',
'functionSent',
'importMeta',
'logicalAssignment',
'nullishCoalescingOperator',
'numericSeparator',
'objectRestSpread',
'optionalCatchBinding',
'optionalChaining',
['pipelineOperator', { proposal: 'minimal' }],
'throwExpressions',
'topLevelAwait',
];

export default class App extends React.Component {
constructor() {
super();
this._jsonRef = React.createRef();

const options = this.buildOptions('js');
this.state = {
value: this.compile(codeSample, options),
mode: 'application/json',
content: codeSample,
options,
};
}

compile(value, options) {
return JSON.stringify(parse(value, null, null, options), null, 2);
}

handleChange = value => {
let result;
let mode = 'text/plain';

try {
result = this.compile(value, this.state.options);
mode = 'application/json';
} catch (err) {
result = String(err);
}
this.setState({ value: result, mode, content: value });
};

buildOptions(language) {
const options = {
babelrc: false,
babelrcRoots: false,
configFile: false,
filename: 'playground.js',
parserOptions: {
plugins: [...defaultPlugins],
},
};
switch (language) {
case 'ts':
options.parserOptions.plugins.push('typescript');
options.filename = 'playground.tsx';
break;
case 'flow':
options.parserOptions.plugins.push('flow');
break;
}

return options;
}

handleLanguageChange = language => {
this.setState({ options: this.buildOptions(language) }, () =>
this.handleChange(this.state.content),
);
};

render() {
return (
<>
<Header onLanguageChange={this.handleLanguageChange} />
<div className="panels">
<Panel
value={this.state.content}
mode="text/jsx"
codeSample={codeSample}
onChange={this.handleChange}
/>
<Panel
readOnly={true}
ref={this._jsonRef}
value={this.state.value}
mode={this.state.mode}
/>
</div>
</>
);
}
}
37 changes: 37 additions & 0 deletions website/src/components/Header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';

export default class Header extends React.Component {
render() {
return (
<header className="page-header">
<h1 className="page-title">
react-docgen: <span className="subtitle">PLAYGROUND</span>{' '}
<select
className="flavor-select"
onChange={event => this.props.onLanguageChange(event.target.value)}
>
<option value="js">JavaScript</option>
<option value="ts">TypeScript</option>
<option value="flow">Flow</option>
</select>
</h1>
<a className="nav-link" href="https://github.com/reactjs/react-docgen">
<span>View on Github</span>
<svg
version="1.1"
width="16"
height="16"
viewBox="0 0 16 16"
className="octicon octicon-mark-github"
aria-hidden="true"
>
<path
fillRule="evenodd"
d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0 0 16 8c0-4.42-3.58-8-8-8z"
></path>
</svg>
</a>
</header>
);
}
}
19 changes: 0 additions & 19 deletions website/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,5 @@
<meta property="og:type" content="article">
</head>
<body>
<header class="page-header">
<h1 class="page-title">react-docgen: <span class="subtitle">PLAYGROUND</span></h1>
<a class="nav-link" href="https://github.com/reactjs/react-docgen">
<span class="view-github">View on Github</span>
<svg
version="1.1"
width="16"
height="16"
viewBox="0 0 16 16"
class="octicon octicon-mark-github"
aria-hidden="true"
>
<path
fill-rule="evenodd"
d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0 0 16 8c0-4.42-3.58-8-8-8z"
></path>
</svg>
</div>
</header>
<div id="root"></div>
</body>
88 changes: 1 addition & 87 deletions website/src/index.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,8 @@
import React from 'react';
import ReactDOM from 'react-dom';
import Panel from './CodeMirrorPanel';
import { parse } from 'react-docgen';
import App from './components/App';

import 'codemirror/lib/codemirror.css';
import './react-docgen.less';

const codeSample = `import React, { Component } from 'react';
import PropTypes from 'prop-types';

/**
* General component description.
*/
class MyComponent extends Component {
render() {
// ...
}
}

MyComponent.propTypes = {
/**
* Description of prop "foo".
*/
foo: PropTypes.number,
/**
* Description of prop "bar" (a custom validation function).
*/
bar: function(props, propName, componentName) {
// ...
},
baz: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]).isRequired,
};

MyComponent.defaultProps = {
foo: 42,
bar: 21
};

export default MyComponent;
`;

class App extends React.Component {
constructor() {
super();
this._jsonRef = React.createRef();
this.state = {
value: this.compile(codeSample),
mode: 'application/json',
content: codeSample,
};
}

compile(value) {
return JSON.stringify(parse(value), null, 2);
}

handleChange = value => {
let result;
let mode = 'text/plain';

try {
result = this.compile(value);
mode = 'application/json';
} catch (err) {
result = String(err);
}
this.setState({ value: result, mode, content: value });
};

render() {
return (
<>
<Panel
value={this.state.content}
mode="text/jsx"
codeSample={codeSample}
onChange={this.handleChange}
/>
<Panel
readOnly={true}
ref={this._jsonRef}
value={this.state.value}
mode={this.state.mode}
/>
</>
);
}
}

ReactDOM.render(<App />, document.getElementById('root'));
18 changes: 13 additions & 5 deletions website/src/react-docgen.less
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@ a {
}
}

#root {
padding: 0;
margin: 0;
height: 100vh;
}

.page-header {
height: 40px;
padding: .5rem 1.5rem;
padding: 0.5rem 1.5rem;
border-bottom: 1px solid rgba(0, 0, 0, 0.2);
display: flex;
justify-content: flex-end !important;
Expand All @@ -41,10 +47,15 @@ a {
}
}

.flavor-select {
vertical-align: middle;
}

.octicon {
display: inline-block;
vertical-align: text-top !important;
fill: currentColor;
margin-left: 0.3rem;
}

.nav-link {
Expand All @@ -57,11 +68,8 @@ a {
text-decoration: none;
}
}
.view-gituhb {
display: inline !important;
}

#root {
.panels {
height: calc(100% - 41px - 1rem);
overflow: hidden;
display: flex;
Expand Down
Loading