import React, { Component } from "react"; import { Index } from "elasticlunr"; import Link from "./Link"; // Search component export default class Search extends Component { constructor(props) { super(props); this.state = { query: ``, results: [] }; } render() { var list; if (this.state.results.length !== 0) { list = ( ); } else { if (this.state.query !== "") { list = No curves found.; } else { list = ; } } return (

{list}
); } getOrCreateIndex = () => this.index ? this.index : // Create an elastic lunr index and hydrate with graphql query results Index.load(this.props.searchIndex); search = evt => { const query = evt.target.value; this.index = this.getOrCreateIndex(); const results = this.index .search(query, { expand: true }) // Map over each ID and return the full document .map(({ ref }) => this.index.documentStore.getDoc(ref)); this.setState({ query, // Query the index with search string to get an [] of IDs results: results }); }; }