@@ -2,14 +2,15 @@ import React from 'react';
22import { Scrollbars } from 'react-custom-scrollbars' ;
33import { connect } from 'react-redux' ;
44
5- import Input from './common/Input ' ;
5+ import regExpEscape from './../helpers ' ;
66import * as actions from '../actions' ;
77
8- class Syntaxes extends React . Component {
8+ class Syntaxes extends React . PureComponent {
99 constructor ( props ) {
1010 super ( props ) ;
1111 this . state = { activeIndex : - 1 } ;
1212 this . onClickHandler = this . onClickHandler . bind ( this ) ;
13+ this . onSyntaxSearch = this . onSyntaxSearch . bind ( this ) ;
1314 }
1415
1516 componentDidMount ( ) {
@@ -24,30 +25,51 @@ class Syntaxes extends React.Component {
2425 this . props . onClick ( syntax ) ;
2526 }
2627
27- render ( ) {
28+ onSyntaxSearch ( e ) {
29+ this . setState ( { searchSyntax : e . target . value . trim ( ) } ) ;
30+ }
31+
32+ getSyntaxes ( ) {
33+ let { syntaxes } = this . props ;
34+
35+ if ( this . state . searchSyntax ) {
36+ syntaxes = this . filterSyntaxes ( regExpEscape ( this . state . searchSyntax ) ) ;
37+ }
38+
39+ return syntaxes . map ( ( syntax , index ) => {
40+ const active = this . state . activeIndex === index ? 'active' : '' ;
41+ return (
42+ < li
43+ className = { `new-snippet-lang-item ${ active } ` }
44+ data-syntax = { syntax }
45+ data-index = { index }
46+ key = { syntax }
47+ >
48+ { syntax }
49+ </ li >
50+ ) ;
51+ } ) ;
52+ }
53+
54+ filterSyntaxes ( searchSyntax ) {
2855 const { syntaxes } = this . props ;
56+ const regExp = new RegExp ( searchSyntax , 'gi' ) ;
57+ return syntaxes . filter ( syntax => syntax . match ( regExp ) ) ;
58+ }
59+
60+ render ( ) {
61+ const syntaxes = this . getSyntaxes ( ) ;
2962
3063 return (
3164 [
3265 < div className = "new-snippet-lang-header" key = "Syntax input" >
33- < Input placeholder = "Type to search..." />
66+ < input className = "input" placeholder = "Type to search..." onChange = { this . onSyntaxSearch } />
3467 </ div > ,
3568 < div className = "new-snippet-lang-list-wrapper" key = "Syntax list" >
69+ { syntaxes . size ? null : < div className = "new-snippet-lang-empty" > No results found</ div > }
3670 < Scrollbars >
3771 < ul className = "new-snippet-lang-list" onClick = { this . onClickHandler } role = "presentation" >
38- { syntaxes . map ( ( syntax , index ) => {
39- const active = this . state . activeIndex === index ? 'active' : '' ;
40- return (
41- < li
42- className = { `new-snippet-lang-item ${ active } ` }
43- data-syntax = { syntax }
44- data-index = { index }
45- key = { syntax }
46- >
47- { syntax }
48- </ li >
49- ) ;
50- } ) }
72+ { syntaxes }
5173 </ ul >
5274 </ Scrollbars >
5375 </ div > ,
0 commit comments