Skip to content

Commit

Permalink
Autoselect search bar when opening site
Browse files Browse the repository at this point in the history
  • Loading branch information
Krassnig committed Oct 1, 2023
1 parent 6ecda38 commit 1d76b3f
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion react/src/@/components/SearchField.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { SetState } from "@/types";
import "./SearchField.css";
import { useEffect, useState } from "react";

interface Props {
search: string;
setSearch: SetState<string>;
}

export const SearchField: React.FC<Props> = ({ search, setSearch }) => {
const [textInput, setTextInput] = useState<HTMLInputElement | null>(null);

useEffect(() => {
if (textInput !== null) {
textInput.select();
}
}, [textInput]);

return (
<div className="search-field">
<input className="_input" type="text" placeholder="Suche nach Rezepten..." value={search} onChange={e => setSearch(e.target.value)} />
<input ref={setTextInput} className="_input" type="text" placeholder="Suche nach Rezepten..." value={search} onChange={e => setSearch(e.target.value)} />
{ search !== '' && (<button className="_clear" type="button" onClick={() => setSearch('')}>×</button>) }
</div>
);
Expand Down

0 comments on commit 1d76b3f

Please sign in to comment.