Created
December 30, 2017 00:55
-
-
Save casprwang/73ffb6a1e246ab500d504ea210fe8e6e to your computer and use it in GitHub Desktop.
Higher order function for client side searching a term in react.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Higher order function takes a input and return a fitering function | |
// original source code: https://github.com/wangsongiam/songwang.io/blob/master/src/pages/search.js#L27-L39 | |
// Live Demo: https://songwang.io/search | |
const searchingFor = term => { | |
return function(x) { | |
return ( | |
(x.node.frontmatter.tags && | |
x.node.frontmatter.tags.every(tag => | |
tag.toLowerCase().includes(term.toLowerCase()) | |
)) || | |
x.node.frontmatter.title.toLowerCase().includes(term.toLowerCase()) || | |
x.node.excerpt.toLowerCase().includes(term.toLowerCase()) || | |
!term | |
) | |
} | |
} | |
class SearchPage extends React.Component { | |
// ... | |
render(){ | |
return ( | |
{this.state.pages.filter(searchingFor(this.state.term))} | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment