From cef0826587ab7015ba271dcae14dca5dd247cb67 Mon Sep 17 00:00:00 2001 From: Bastian Meissner Date: Tue, 8 Mar 2022 20:53:17 +0100 Subject: [PATCH] Add autoFocus functionality to search.json --- README.md | 7 +++++ data/search.json | 1 + src/components/searchBar.tsx | 6 ++++ src/data/search.json | 1 + src/test/components/searchBar.spec.tsx | 39 ++++++++++++++++++-------- 5 files changed, 42 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 396c820..399c6ed 100644 --- a/README.md +++ b/README.md @@ -214,6 +214,13 @@ For search providers to work, make sure your `search.json` resembles the followi } ``` +You can also customize whether or not the search bar automatically focusses on render by adding +the following to `search.json`, on the same level where properties like `providers` are located: + +```json + "autoFocus": true, +``` + ### Imprint In order for the imprint-modal to show up, make sure your `imprint.json` resembles the following: diff --git a/data/search.json b/data/search.json index f138c7b..f1add11 100644 --- a/data/search.json +++ b/data/search.json @@ -1,6 +1,7 @@ { "placeholder": "", "defaultProvider": "https://google.com/search?q=", + "autoFocus": false, "providers": [ { "name": "Allmusic", diff --git a/src/components/searchBar.tsx b/src/components/searchBar.tsx index beb14a8..6da10a5 100644 --- a/src/components/searchBar.tsx +++ b/src/components/searchBar.tsx @@ -44,6 +44,7 @@ export interface ISearchProviderProps { export interface ISearchProps { placeholder: string; defaultProvider: string; + autoFocus?: boolean; providers: Array | undefined; } @@ -99,6 +100,10 @@ const SearchBar = ({ search }: ISearchBarProps) => { e.preventDefault(); }; + const autoFocus = () => { + return search.autoFocus !== undefined ? search.autoFocus : false; + }; + return ( handleSearchQuery(e)}> { data-testid="search-input" value={input} placeholder={search.placeholder} + autoFocus={autoFocus()} onChange={(e: React.ChangeEvent) => setInput(e.target.value) } diff --git a/src/data/search.json b/src/data/search.json index f138c7b..f1add11 100644 --- a/src/data/search.json +++ b/src/data/search.json @@ -1,6 +1,7 @@ { "placeholder": "", "defaultProvider": "https://google.com/search?q=", + "autoFocus": false, "providers": [ { "name": "Allmusic", diff --git a/src/test/components/searchBar.spec.tsx b/src/test/components/searchBar.spec.tsx index 3930ac8..0c8e46a 100644 --- a/src/test/components/searchBar.spec.tsx +++ b/src/test/components/searchBar.spec.tsx @@ -5,7 +5,7 @@ import SearchBar, { ISearchProps, } from "../../components/searchBar"; -const props: ISearchProps = { +const defaultProps: ISearchProps = { defaultProvider: "https://test.com?q=", placeholder: "", providers: [ @@ -27,7 +27,7 @@ const props: ISearchProps = { ], }; -const setup = () => { +const setup = (props: ISearchProps = defaultProps) => { const searchBar = render(); const input = searchBar.getByTestId("search-input"); const clearButton = searchBar.getByTestId("search-clear"); @@ -55,25 +55,25 @@ describe("searchBar.tsx", () => { }); it("Tests SearchBar rendering with properties", () => { - const { asFragment } = render(); - expect(asFragment).toMatchSnapshot(); + const { searchBar } = setup(); + expect(searchBar.asFragment).toMatchSnapshot(); }); it("Tests searchBar rendering without properties", () => { - const { asFragment } = render(); - expect(asFragment).toMatchSnapshot(); + const searchBar = render(); + expect(searchBar.asFragment).toMatchSnapshot(); }); it("Tests handleQueryWithProvider", () => { - props.providers?.forEach((provider: ISearchProviderProps) => { - handleQueryWithProvider(props, provider.prefix + " test"); + defaultProps.providers?.forEach((provider: ISearchProviderProps) => { + handleQueryWithProvider(defaultProps, provider.prefix + " test"); expect(window.location.href).toEqual(provider.url + "test"); }); }); it("Tests handleQueryWithProvider default", () => { - handleQueryWithProvider(props, "test"); - expect(window.location.href).toEqual(props.defaultProvider + "test"); + handleQueryWithProvider(defaultProps, "test"); + expect(window.location.href).toEqual(defaultProps.defaultProvider + "test"); }); it("Tests handleQueryWithProvider without providers", () => { @@ -87,12 +87,27 @@ describe("searchBar.tsx", () => { expect(window.location.href).toEqual(test.defaultProvider + "test"); }); + it("Tests SearchBar with autoFocus not set", () => { + const { input } = setup(); + expect(input).not.toHaveFocus(); + }); + + it("Tests SearchBar with autoFocus set to true", () => { + const { input } = setup({ ...defaultProps, autoFocus: true }); + expect(input).toHaveFocus(); + }); + + it("Tests SearchBar with autoFocus set to false", () => { + const { input } = setup({ ...defaultProps, autoFocus: false }); + expect(input).not.toHaveFocus(); + }); + it("Tests SearchBar component with default search", () => { const { input, submitButton } = setup(); fireEvent.change(input, { target: { value: "test" } }); fireEvent.click(submitButton); - expect(window.location.href).toEqual(props.defaultProvider + "test"); + expect(window.location.href).toEqual(defaultProps.defaultProvider + "test"); }); it("Tests SearchBar component with other search", () => { @@ -113,6 +128,6 @@ describe("searchBar.tsx", () => { fireEvent.click(clearButton); fireEvent.click(submitButton); - expect(window.location.href).toEqual(props.defaultProvider); + expect(window.location.href).toEqual(defaultProps.defaultProvider); }); });