Skip to content

Instantly share code, notes, and snippets.

@casprwang
Created December 30, 2017 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save casprwang/73ffb6a1e246ab500d504ea210fe8e6e to your computer and use it in GitHub Desktop.
Save casprwang/73ffb6a1e246ab500d504ea210fe8e6e to your computer and use it in GitHub Desktop.
Higher order function for client side searching a term in react.
// 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