refactor Search

This commit is contained in:
monoid 2022-06-21 14:41:33 +09:00
parent b0ff34f097
commit a25df086f3

View File

@ -104,6 +104,7 @@ export const Headline = (prop: {
<Divider /> <Divider />
{prop.menu} {prop.menu}
</>); </>);
return (<div style={{ display: 'flex' }}> return (<div style={{ display: 'flex' }}>
<CssBaseline /> <CssBaseline />
<AppBar position="fixed" sx={{ <AppBar position="fixed" sx={{
@ -141,18 +142,13 @@ export const Headline = (prop: {
alignItems: 'center', alignItems: 'center',
justifyContent: 'center' justifyContent: 'center'
}}> }}>
<SearchIcon onClick={() => navigate(`/search?word=${encodeURIComponent(search)}`)} /> <SearchIcon onClick={() => navSearch(search)} />
</div> </div>
<StyledInputBase placeholder="search" <StyledInputBase placeholder="search"
onChange={(e) => setSearch(e.target.value)} onChange={(e) => setSearch(e.target.value)}
onKeyUp={(e) => { onKeyUp={(e) => {
if (e.key === "Enter") { if (e.key === "Enter") {
let words = search.includes("&") ? search.split("&") : [search]; navSearch(search);
words = words.map(w => w.trim())
.map(w => w.includes(":") ?
`allow_tag=${w}`
: `word=${encodeURIComponent(w)}`);
navigate(`/search?${words.join("&")}`);
} }
}} }}
value={search}></StyledInputBase> value={search}></StyledInputBase>
@ -207,6 +203,14 @@ export const Headline = (prop: {
{prop.children} {prop.children}
</main> </main>
</div>); </div>);
function navSearch(search: string){
let words = search.includes("&") ? search.split("&") : [search];
words = words.map(w => w.trim())
.map(w => w.includes(":") ?
`allow_tag=${w}`
: `word=${encodeURIComponent(w)}`);
navigate(`/search?${words.join("&")}`);
}
}; };
export default Headline; export default Headline;