React-Codemirror match-highlighter addon not highlighting the text

747 views Asked by At

I am using react-codemirror and want to highlight the text 'Hello' in the Codemirror but the match-highlighter addon is not highlighting the same. Below is the code for the same.

import React, { Component } from 'react';
import { render } from 'react-dom';
import CodeMirror from 'react-codemirror';
import 'codemirror/lib/codemirror.css';
import 'codemirror/addon/search/match-highlighter';
import 'codemirror/mode/javascript/javascript';

class App extends Component {
  constructor() {
    super();
    this.state = {
      name: 'CodeMirror',
      code: '//Test Codemirror'
    };
  }

  updateCode(newCode) {
        this.setState({
            code: newCode,
        });
    }

  render() {
    let options = {
        lineNumbers: true,
        mode: 'javascript',
        highlightSelectionMatches: {
          minChars: 2,
          showToken: /Hello/,
          style:'matchhighlight'
        },
        styleActiveLine: true,
        styleActiveSelected: true,
    };
    return (
      <div>
        <CodeMirror value={this.state.code} onChange={this.updateCode.bind(this)} options={options}/>
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

Current output is in the screenshot below and the word is not highlighted.

Word Hello is not highlighted

1

There are 1 answers

0
Ananth On BEST ANSWER

I found a solution for this issue. Inorder to enable the highlighting one need to add a css corresponding to the style property. I added the below code in css file and it started working

.cm-matchhighlight {
  background: red !important
}

Now it highlights the token properly