@@ -2,6 +2,7 @@ import React, {Component} from 'react';
2
2
import { StaticQuery , graphql } from 'gatsby' ;
3
3
import classnames from 'classnames' ;
4
4
import _ from 'lodash' ;
5
+ import fuzzysearch from 'fuzzysearch' ;
5
6
6
7
import './index.scss' ;
7
8
import searchIcon from '../../images/search.svg' ;
@@ -35,15 +36,17 @@ class Navbar extends Component {
35
36
}
36
37
} ;
37
38
38
- getMarkdownPages ( data ) {
39
+ getMarkdownPages = data => {
40
+ const { filter} = this . state ;
39
41
const markdownPages = data . allFile . edges ;
40
42
const pages = _ . flow (
41
43
pages => _ . map ( pages , ( { node} ) => node . childMarkdownRemark . frontmatter ) ,
44
+ items => _ . filter ( items , item => fuzzysearch ( _ . toLower ( filter ) , _ . toLower ( item . title ) ) ) ,
42
45
items => _ . sortBy ( items , 'index' )
43
46
) ( markdownPages ) ;
44
47
45
48
return pages ;
46
- }
49
+ } ;
47
50
48
51
getNavbarComponents ( data ) {
49
52
const components = data . allComponentMetadata . edges ;
@@ -84,9 +87,8 @@ class Navbar extends Component {
84
87
const markdowns = this . getMarkdownPages ( data ) ;
85
88
const components = this . getNavbarComponents ( data ) ;
86
89
const filteredComponents = _ . filter ( components , component =>
87
- _ . includes ( _ . lowerCase ( component . node . displayName ) , _ . lowerCase ( filter ) )
90
+ fuzzysearch ( _ . toLower ( filter ) , _ . toLower ( component . node . displayName ) )
88
91
) ;
89
-
90
92
const componentsByGroups = _ . groupBy ( filteredComponents , c => _ . split ( c . node . displayName , '.' ) [ 0 ] ) ;
91
93
92
94
const navbarClassName = classnames ( 'navbar' , {
@@ -99,17 +101,20 @@ class Navbar extends Component {
99
101
{ this . renderSearch ( ) }
100
102
< ul >
101
103
{ _ . map ( markdowns , page => {
102
- return < Item id = { page . title } link = { page . path } onLinkClick = { ( ) => this . toggleNavbar ( false ) } /> ;
104
+ return (
105
+ < Item key = { page . title } id = { page . title } link = { page . path } onLinkClick = { ( ) => this . toggleNavbar ( false ) } />
106
+ ) ;
103
107
} ) }
104
- < li className = "separator" />
108
+ { ! _ . isEmpty ( markdowns ) && < li className = "separator" /> }
105
109
{ _ . map ( componentsByGroups , ( components , key ) => {
106
110
return (
107
111
< Item
112
+ key = { key }
108
113
id = { key }
109
114
components = { components }
110
115
currentPage = { currentPage }
111
116
onLinkClick = { ( ) => this . toggleNavbar ( false ) }
112
- > </ Item >
117
+ / >
113
118
) ;
114
119
} ) }
115
120
</ ul >
0 commit comments