insomnia/app/ui/components/editors/EnvironmentEditor.js
2017-02-26 15:47:45 -08:00

58 lines
1.2 KiB
JavaScript

import React, {PropTypes, Component} from 'react';
import Editor from '../base/Editor';
class EnvironmentEditor extends Component {
_handleChange = () => {
this.props.didChange();
};
_setEditorRef = n => this._editor = n;
getValue () {
return JSON.parse(this._editor.getValue());
}
isValid () {
try {
return this.getValue() !== undefined;
} catch (e) {
// Failed to parse JSON
return false;
}
}
render () {
const {
environment,
editorFontSize,
editorKeyMap,
lineWrapping,
...props
} = this.props;
return (
<Editor
ref={this._setEditorRef}
fontSize={editorFontSize}
lineWrapping={lineWrapping}
keyMap={editorKeyMap}
onChange={this._handleChange}
value={JSON.stringify(environment)}
autoPrettify={true}
mode="application/json"
{...props}
/>
)
}
}
EnvironmentEditor.propTypes = {
environment: PropTypes.object.isRequired,
didChange: PropTypes.func.isRequired,
editorFontSize: PropTypes.number.isRequired,
editorKeyMap: PropTypes.string.isRequired,
lineWrapping: PropTypes.bool.isRequired,
};
export default EnvironmentEditor;