1import { Component, createRef } from "react";
2import { glossary } from "../lib/glossary";
3import { withRouter } from "next/router";
4import Downshift from "downshift";
6class Search extends Component {
12 this.searchEndpoint = this.searchEndpoint.bind(this);
13 this.onInputValueChange = this.onInputValueChange.bind(this);
14 this.onSelect = this.onSelect.bind(this);
15 this.glossarySearch = this.glossarySearch.bind(this);
18 searchEndpoint(query) {
19 return `/api/search?q=${query}`;
22 glossarySearch(query) {
23 return glossary.filter((entry) => {
24 return query.toLowerCase() === entry.name || entry.symbol === query;
31 this.props.router.push(item.slug);
39 this.props.closeSearch();
42 onInputValueChange(query) {
44 fetch(this.searchEndpoint(query))
45 .then((res) => res.json())
47 // Wrap results in an object which will tell React what component to use to render results.
48 const results = res.results.map((item) => ({
53 const glossaryResults = this.glossarySearch(query).map((item) => ({
54 type: "GLOSSARY_RESULT",
58 const list = [...glossaryResults, ...results];
60 this.setState({ results: list });
63 this.setState({ results: [] });
68 const { state, props } = this;
70 if (props.showSearch) {
73 onSelect={(selection) => this.onSelect(selection)}
74 onInputValueChange={(event) => this.onInputValueChange(event)}
75 itemToString={(item) => (item ? item.slug : "")}
76 defaultHighlightedIndex={0}
89 <div className="fixed w-screen h-screen z-50 flex flex-col items-center p-4">
91 onClick={(event) => props.closeSearch(event)}
92 className="top-0 left-0 fixed w-screen h-screen bg-washedWall"
94 <div className="relative flex flex-col max-w-screen-lg md:my-32 w-full md:w-10/12 lg:w-8/12 xl:w-6/12 rounded-xl bg-white min-h-0 overflow-hidden">
96 style={{ display: "inline-block" }}
97 {...getRootProps({}, { suppressRefError: true })}
101 className="text-lg md:text-xl lg:text-2xl font-medium text-green bg-transparent py-2 px-4 outline-none relative w-full"
102 placeholder="Search..."
104 onClick={(e) => e.stopPropagation()}
106 onKeyDown: (event) => {
107 if (event.key === "Escape") {
108 // Prevent Downshift's default 'Escape' behavior.
109 event.nativeEvent.preventDownshiftDefault = true;
110 this.props.closeSearch(event);
116 <ul {...getMenuProps()} className="overflow-y-scroll">
118 ? state.results.map((item, index) => {
119 const selected = highlightedIndex === index;
120 if (item.type === "GLOSSARY_RESULT") {
123 className={`cursor-pointer flex text-left w-full ${
124 selected ? "bg-green" : ""
127 key: item.content.slug + "-" + index,
130 selected: highlightedIndex === index,
133 <div className="font-semibold p-3">
135 className={`text-base ${
136 selected ? "text-white" : "text-black"
139 {item.content.symbol.length > 0 && (
141 className={`mr-1 rounded px-1 py-0.5 ${
142 selected ? "bg-washedWhite" : "bg-wall"
145 {item.content.symbol}
151 className={`font-normal text-base mt-1 ${
152 selected ? "text-white" : "text-black"
161 if (item.type === "RESULT") {
164 className={`cursor-pointer flex text-left w-full ${
165 selected ? "bg-green" : ""
168 key: item.content.link + "-" + index,
174 <div className="p-3">
176 className={`font-medium text-base ${
177 selected ? "text-white" : "text-black"
180 {item.content.parent !== "Content"
181 ? `${item.content.parent} /`
186 className={`text-base font-regular text-small ${
187 selected ? "text-midWhite" : "text-gray"
190 {item.content.content}
211export default withRouter(Search);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list