This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3ec465
commit 8849a31
Showing
7 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from 'react'; | ||
import { StaticQuery, graphql } from 'gatsby'; | ||
|
||
import Search from './Search'; | ||
|
||
const HeaderSearch = () => ( | ||
<StaticQuery | ||
query={graphql` | ||
query SearchIndexQuery { | ||
siteSearchIndex { | ||
index | ||
} | ||
} | ||
`} | ||
render={data => <Search searchIndex={data.siteSearchIndex.index} />} | ||
/> | ||
); | ||
|
||
export default HeaderSearch; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from "react" | ||
import { Index } from "elasticlunr" | ||
import { Link } from "gatsby" | ||
import './style.scss'; | ||
|
||
const Search = props => { | ||
const [query, setQuery] = React.useState(''); | ||
const [results, setResults] = React.useState([]); | ||
const handleChange = React.useCallback(evt => { | ||
const value = evt.target.value | ||
const index = Index.load(props.searchIndex) | ||
const rl = index | ||
.search(value, { expand: true }) | ||
// Map over each ID and return the full document | ||
.map(({ ref }) => index.documentStore.getDoc(ref)); | ||
setQuery(value); | ||
setResults(rl.filter(r => r.title)); | ||
}, [setQuery, setResults]); | ||
return ( | ||
<div className="HeaderSearch"> | ||
<input | ||
className="form-control" | ||
type="text" | ||
value={query} | ||
onChange={handleChange} | ||
placeholder="在 wizard ui 中搜索" | ||
/> | ||
{results.length > 0 && ( | ||
<ul> | ||
{results.map(page => ( | ||
<li key={page.id}> | ||
<Link to={page.path}> | ||
{page.title} | ||
{Boolean(page.author) && <span>{page.author}</span>} | ||
</Link> | ||
</li> | ||
))} | ||
</ul> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
export default Search; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
.HeaderSearch { | ||
position: relative; | ||
margin-right: 20px; | ||
input { | ||
margin-top: 8px; | ||
width: 180px; | ||
} | ||
ul { | ||
position: absolute; | ||
right: 0; | ||
width: 360px; | ||
background-color: #fff; | ||
border: 1px solid #ccc; | ||
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075); | ||
padding-left: 0px; | ||
border-radius: 4px; | ||
max-height: 170px; | ||
overflow: scroll; | ||
li { | ||
list-style: none; | ||
a { | ||
display: block; | ||
height: 34px; | ||
line-height: 34px; | ||
padding-left: 10px; | ||
padding-right: 10px; | ||
&:hover { | ||
text-decoration: none; | ||
background-color: #dad6f7; | ||
} | ||
} | ||
span { | ||
float: right; | ||
} | ||
} | ||
} | ||
} |
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
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