Skip to content

Commit

Permalink
Merge pull request #81 from Etlas/patch-1
Browse files Browse the repository at this point in the history
Fix Errors during Enzyme test
  • Loading branch information
RIP21 authored Jan 27, 2019
2 parents b5fe7ae + 2515171 commit 0c98791
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export default class SimpleMDEEditor extends React.PureComponent<
SimpleMDEEditorProps,
SimpleMDEEditorState
> {
private elementWrapperRef: any;

static defaultProps = {
events: {},
onChange: noop,
Expand Down Expand Up @@ -91,7 +93,9 @@ export default class SimpleMDEEditor extends React.PureComponent<
}

componentWillUnmount() {
this.removeEvents();
if (this.editorEl !== null && this.editorEl !== undefined) {
this.removeEvents();
}
}

createEditor = () => {
Expand Down Expand Up @@ -121,16 +125,11 @@ export default class SimpleMDEEditor extends React.PureComponent<
};

addEvents = () => {
const wrapperId = `${this.id}-wrapper`;
const wrapperEl = document.getElementById(`${wrapperId}`);

this.editorEl = wrapperEl!.getElementsByClassName("CodeMirror")[0];
this.editorToolbarEl = wrapperEl!.getElementsByClassName(
"editor-toolbar"
)[0];
this.editorEl = this.elementWrapperRef;
this.editorToolbarEl = this.elementWrapperRef.getElementsByClassName("editor-toolbar")[0];

this.editorEl.addEventListener("keyup", this.eventWrapper);
this.editorEl.addEventListener("paste", this.eventWrapper);
this.editorEl!.addEventListener("keyup", this.eventWrapper);
this.editorEl!.addEventListener("paste", this.eventWrapper);
this.editorToolbarEl &&
this.editorToolbarEl.addEventListener("click", this.eventWrapper);

Expand Down Expand Up @@ -166,6 +165,10 @@ export default class SimpleMDEEditor extends React.PureComponent<
this.simpleMde!.codemirror.setOption("extraKeys", this.props.extraKeys);
}
};

private buildWrapperRef = (e: any) => {
this.elementWrapperRef = e;
};

render() {
const {
Expand All @@ -182,7 +185,7 @@ export default class SimpleMDEEditor extends React.PureComponent<
...rest
} = this.props;
return (
<div id={`${this.id}-wrapper`} {...rest}>
<div id={`${this.id}-wrapper`} {...rest} ref={this.buildWrapperRef}>
{label && <label htmlFor={this.id}> {label} </label>}
<textarea id={this.id} />
</div>
Expand Down

0 comments on commit 0c98791

Please sign in to comment.