Skip to content

Commit 1e5b1b0

Browse files
committed
add autoSave property
The autoSave property will persist every CodeMirror change to the underlying text area by repeatedly calling codemirror.save(). This fixes an issue where a user will submit the form and expect the current codemirror text to submit as the textarea value but due to some quirk or unclear behavior, the textarea value is not set by the time an onSubmit handler is triggered.
1 parent 0d5c296 commit 1e5b1b0

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ React.render(<App />, document.getElementById('app'));
6262
*`options``Object (newValue)` options passed to the CodeMirror instance
6363
*`onChange``Function (newValue)` called when a change is made
6464
*`onFocusChange``Function (focused)` called when the editor is focused or loses focus
65+
*`autoSave``Boolean` automatically persist changes to underlying textarea (default false)
6566

6667
See the [CodeMirror API Docs](https://codemirror.net/doc/manual.html#api) for the available options.
6768

‎lib/Codemirror.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ var CodeMirror = React.createClass({
1313
options: React.PropTypes.object,
1414
path: React.PropTypes.string,
1515
value: React.PropTypes.string,
16-
className: React.PropTypes.any
16+
className: React.PropTypes.any,
17+
autoSave: React.PropTypes.bool
1718
},
1819

1920
getInitialState: functiongetInitialState(){
@@ -72,6 +73,9 @@ var CodeMirror = React.createClass({
7273
codemirrorValueChanged: functioncodemirrorValueChanged(doc,change){
7374
varnewValue=doc.getValue();
7475
this._currentCodemirrorValue=newValue;
76+
if(this.props.autoSave){
77+
this.getCodeMirror().save();
78+
}
7579
this.props.onChange&&this.props.onChange(newValue);
7680
},
7781

@@ -87,4 +91,4 @@ var CodeMirror = React.createClass({
8791

8892
});
8993

90-
module.exports=CodeMirror;
94+
module.exports=CodeMirror;

0 commit comments

Comments
(0)