2017-02-28 06:26:24 +00:00
|
|
|
import React, {PureComponent, PropTypes} from 'react';
|
2017-02-27 21:00:13 +00:00
|
|
|
import Editor from './Editor';
|
|
|
|
|
2017-02-28 06:26:24 +00:00
|
|
|
class OneLineEditor extends PureComponent {
|
2017-02-27 21:00:13 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.value = props.defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
focus () {
|
2017-02-28 06:26:24 +00:00
|
|
|
if (!this.editor.hasFocus()) {
|
|
|
|
this.editor.focusEnd();
|
|
|
|
}
|
2017-02-27 21:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_handleChange = value => {
|
|
|
|
this.value = value;
|
|
|
|
this.props.onChange && this.props.onChange(value);
|
|
|
|
};
|
|
|
|
|
2017-03-01 00:10:23 +00:00
|
|
|
_handleKeyDown = e => {
|
|
|
|
// submit form if needed
|
|
|
|
if (e.keyCode === 13) {
|
|
|
|
let node = e.target;
|
|
|
|
for (let i = 0; i < 20 && node; i++) {
|
|
|
|
if (node.tagName === 'FORM') {
|
|
|
|
node.dispatchEvent(new Event('submit'));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
node = node.parentNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also call the original if there was one
|
|
|
|
this.props.onKeyDown && this.props.onKeyDown(e);
|
|
|
|
};
|
|
|
|
|
2017-02-27 21:00:13 +00:00
|
|
|
_setRef = n => this.editor = n;
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const {defaultValue, ...props} = this.props;
|
|
|
|
return (
|
|
|
|
<Editor
|
|
|
|
ref={this._setRef}
|
|
|
|
{...props}
|
2017-02-28 06:26:24 +00:00
|
|
|
defaultTabBehavior
|
|
|
|
hideLineNumbers
|
|
|
|
hideScrollbars
|
|
|
|
noDragDrop
|
|
|
|
noMatchBrackets
|
|
|
|
singleLine
|
2017-02-28 21:32:23 +00:00
|
|
|
tabIndex={0}
|
2017-03-01 00:10:23 +00:00
|
|
|
onKeyDown={this._handleKeyDown}
|
2017-02-27 21:00:13 +00:00
|
|
|
onChange={this._handleChange}
|
|
|
|
className="editor--single-line"
|
|
|
|
value={defaultValue}
|
|
|
|
lineWrapping={false}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OneLineEditor.propTypes = Object.assign({}, Editor.propTypes, {
|
|
|
|
defaultValue: PropTypes.string.isRequired,
|
|
|
|
});
|
|
|
|
|
|
|
|
export default OneLineEditor;
|