My Docs
BlogGithubLinkedin
Blog-Notes
Blog-Notes
  • Page 1
  • Adding a Site Search to Your Stackbit Gatsby Site
  • Adding Search to Your Gatsby Site
  • Home
  • Netlify Injected Content
  • SEO.md
  • THINGS TO EMBED.md
  • Technologies Used.md
  • gatsby-remark-code-buttons
  • Adding a Mailing List to Your Gatsby Site
  • Broken links on your site
  • Configure external DNS for a custom domain
  • contentauthoring.md
  • full text search example
  • inject 4.md
  • inject3.md
  • inject4.md
  • Snippet injection
  • injected part3.md
  • links 2 embed.md
  • links to remember
  • privacy policy.md
  • medium
    • Page 2
      • Docs
    • Medium
    • 2021-02-27_Basic-Web-Development-Environment-Setup-9f36c3f15afe
Powered by GitBook
On this page

Was this helpful?

full text search example

import React from 'react';

import { Link } from 'gatsby'

export default class SearchBar extends React.Component {
  state = {
    query: '',
    results: [],
  }

  render() {
    return (
      <div className={this.props.classNames+" search__parent__div flex-md"}>
        <input className='search__input' type='text' value={this.state.query} onChange={this.search} placeholder={'Search'} aria-label="Search" />
        { // Results list
        (this.state.results.length > 0) && ( // Only show when there are results
        <div className='search__list'>
            <span>Search results</span>
            <ul>
              {this.state.results.map((page) => (
              <li key={page.url}>
                <Link className='search__list_white search__list_non-decoration'
                  to={page.url}>
                  {page.title}
                </Link>
              </li>
              ))}
            </ul>
        </div>
        ) }
      </div>
    )
  }

  getSearchResults(query) {
    if (!query || !window.__LUNR__) return []
    const lunrIndex =  window.__LUNR__[this.props.lng];
    const results = lunrIndex.index.search(query) // you can  customize your search , see https://lunrjs.com/guides/searching.html
    return results.map(({ ref }) => lunrIndex.store[ref])
  }

  search = event => {
    const query = event.target.value
    const results = this.getSearchResults(query)
    this.setState({ results, query })
  }
}
Previouscontentauthoring.mdNextinject 4.md

Last updated 3 years ago

Was this helpful?