Skip to content

Commit bc10316

Browse files
authored
Use React Dom Polyfill to support React 0.12+
1 parent e4a98ae commit bc10316

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

lib/Codemirror.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22

33
var React = require('react');
4+
var ReactDOM = require('react-dom-polyfill')(React);
5+
var findDOMNode = ReactDOM.findDOMNode;
46
var className = require('classnames');
57
var debounce = require('lodash.debounce');
68

@@ -10,11 +12,12 @@ var CodeMirror = React.createClass({
1012
propTypes: {
1113
onChange: React.PropTypes.func,
1214
onFocusChange: React.PropTypes.func,
15+
onScroll: React.PropTypes.func,
1316
options: React.PropTypes.object,
1417
path: React.PropTypes.string,
1518
value: React.PropTypes.string,
1619
className: React.PropTypes.any,
17-
codeMirrorInstance: React.PropTypes.object
20+
codeMirrorInstance: React.PropTypes.func
1821
},
1922
getCodeMirrorInstance: function getCodeMirrorInstance() {
2023
return this.props.codeMirrorInstance || require('codemirror');
@@ -25,12 +28,13 @@ var CodeMirror = React.createClass({
2528
};
2629
},
2730
componentDidMount: function componentDidMount() {
28-
var textareaNode = this.refs.textarea;
31+
var textareaNode = findDOMNode(this.refs.textarea);
2932
var codeMirrorInstance = this.getCodeMirrorInstance();
3033
this.codeMirror = codeMirrorInstance.fromTextArea(textareaNode, this.props.options);
3134
this.codeMirror.on('change', this.codemirrorValueChanged);
3235
this.codeMirror.on('focus', this.focusChanged.bind(this, true));
3336
this.codeMirror.on('blur', this.focusChanged.bind(this, false));
37+
this.codeMirror.on('scroll', this.scrollChanged);
3438
this.codeMirror.setValue(this.props.defaultValue || this.props.value || '');
3539
},
3640
componentWillUnmount: function componentWillUnmount() {
@@ -65,6 +69,9 @@ var CodeMirror = React.createClass({
6569
});
6670
this.props.onFocusChange && this.props.onFocusChange(focused);
6771
},
72+
scrollChanged: function scrollChanged(cm) {
73+
this.props.onScroll && this.props.onScroll(cm.getScrollInfo());
74+
},
6875
codemirrorValueChanged: function codemirrorValueChanged(doc, change) {
6976
if (this.props.onChange && change.origin != 'setValue') {
7077
this.props.onChange(doc.getValue());

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@
2323
"happiness": "^1.0.7",
2424
"react": "^15.0.0",
2525
"react-dom": "^15.0.0",
26-
"react-component-gulp-tasks": "^0.7.7"
26+
"react-component-gulp-tasks": "^0.7.7",
27+
"react-dom-polyfill": "^1.0.0-beta.0"
2728
},
2829
"peerDependencies": {
29-
"react": "^0.14.0 || ^15.0.0"
30+
"react": ">=0.12 <16"
31+
},
32+
"optionalDependencies": {
33+
"react-dom": "<16"
3034
},
3135
"browserify-shim": {
3236
"react": "global:React"

src/Codemirror.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const React = require('react');
2+
const ReactDOM = require('react-dom-polyfill')(React);
3+
const findDOMNode = ReactDOM.findDOMNode;
24
const className = require('classnames');
35
const debounce = require('lodash.debounce');
46

@@ -22,7 +24,7 @@ const CodeMirror = React.createClass({
2224
};
2325
},
2426
componentDidMount () {
25-
const textareaNode = this.refs.textarea;
27+
const textareaNode = findDOMNode(this.refs.textarea);
2628
const codeMirrorInstance = this.getCodeMirrorInstance();
2729
this.codeMirror = codeMirrorInstance.fromTextArea(textareaNode, this.props.options);
2830
this.codeMirror.on('change', this.codemirrorValueChanged);

0 commit comments

Comments
 (0)