Added disappearing buttons to submit and clear

This commit is contained in:
phntxx 2021-03-21 10:55:27 +01:00
parent 79fe8dedd8
commit bc897a6bfb
2 changed files with 52 additions and 12 deletions

View file

@ -50,7 +50,8 @@ export const Button = styled.button`
border: 1px solid ${selectedTheme.mainColor}; border: 1px solid ${selectedTheme.mainColor};
color: ${selectedTheme.mainColor}; color: ${selectedTheme.mainColor};
background: none; background: none;
min-height: 3em;
min-height: 2rem;
height: 100%; height: 100%;
&:hover { &:hover {

View file

@ -1,22 +1,43 @@
import React, { useState } from "react"; import React, { useEffect, useState } from "react";
import styled from "styled-components"; import styled from "styled-components";
import selectedTheme from "./themeManager"; import selectedTheme from "./themeManager";
import { Button } from "./elements";
const Search = styled.form`
width: 100%;
height: 2rem;
display: flex;
padding-top: 0.25rem;
`;
const SearchInput = styled.input` const SearchInput = styled.input`
width: 100%; width: 100%;
font-size: 1rem; font-size: 1rem;
border: none; border: none;
border-bottom: 1px solid ${selectedTheme.accentColor}; border-bottom: 1px solid ${selectedTheme.accentColor};
background: none; background: none;
border-radius: 0; border-radius: 0;
color: ${selectedTheme.mainColor}; color: ${selectedTheme.mainColor};
margin: 0px;
:focus { :focus {
outline: none; outline: none;
} }
`; `;
const SearchButton = styled(Button)`
margin: 0px 2px;
min-height: 0;
`;
export interface ISearchProviderProps { export interface ISearchProviderProps {
name: string; name: string;
url: string; url: string;
@ -28,10 +49,15 @@ interface ISearchBarProps {
} }
const SearchBar = ({ providers }: ISearchBarProps) => { const SearchBar = ({ providers }: ISearchBarProps) => {
let [input, setInput] = useState(""); let [input, setInput] = useState<string>("");
let [buttonsHidden, setButtonsHidden] = useState<boolean>(true);
useEffect(() => {
setButtonsHidden(input === "");
}, [input]);
const handleSearchQuery = (e: React.FormEvent) => { const handleSearchQuery = (e: React.FormEvent) => {
var query = input || ""; var query: string = input || "";
if (query.split(" ")[0].includes("/")) { if (query.split(" ")[0].includes("/")) {
handleQueryWithProvider(query); handleQueryWithProvider(query);
@ -43,13 +69,14 @@ const SearchBar = ({ providers }: ISearchBarProps) => {
}; };
const handleQueryWithProvider = (query: string) => { const handleQueryWithProvider = (query: string) => {
let queryArray = query.split(" "); let queryArray: Array<string> = query.split(" ");
let prefix = queryArray[0]; let prefix: string = queryArray[0];
queryArray.shift(); queryArray.shift();
let searchQuery = queryArray.join(" "); let searchQuery: string = queryArray.join(" ");
let providerFound = false; let providerFound: boolean = false;
if (providers) { if (providers) {
providers.forEach((provider: ISearchProviderProps) => { providers.forEach((provider: ISearchProviderProps) => {
if (provider.prefix === prefix) { if (provider.prefix === prefix) {
@ -64,13 +91,25 @@ const SearchBar = ({ providers }: ISearchBarProps) => {
}; };
return ( return (
<form onSubmit={(e) => handleSearchQuery(e)}> <Search onSubmit={(e) => handleSearchQuery(e)}>
<SearchInput <SearchInput
type="text" type="text"
onChange={(e) => setInput(e.target.value)} value={input}
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
setInput(e.target.value)
}
></SearchInput> ></SearchInput>
<button type="submit" hidden /> <SearchButton
</form> type="button"
onClick={() => setInput("")}
hidden={buttonsHidden}
>
Clear
</SearchButton>
<SearchButton type="submit" hidden={buttonsHidden}>
Search
</SearchButton>
</Search>
); );
}; };