mirror of
https://github.com/gethomepage/homepage.git
synced 2026-09-29 15:31:16 -07:00
Fix: dont fire search on empty field (#7101)
Release Drafter / Update Release Draft (push) Has been cancelled
Release Drafter / Auto Label PR (push) Has been cancelled
Tests / vitest (1) (push) Has been cancelled
Docker CI / Docker Build & Push (push) Has been cancelled
Lint / Linting Checks (push) Has been cancelled
Tests / vitest (2) (push) Has been cancelled
Tests / vitest (3) (push) Has been cancelled
Tests / vitest (4) (push) Has been cancelled
Release Drafter / Update Release Draft (push) Has been cancelled
Release Drafter / Auto Label PR (push) Has been cancelled
Tests / vitest (1) (push) Has been cancelled
Docker CI / Docker Build & Push (push) Has been cancelled
Lint / Linting Checks (push) Has been cancelled
Tests / vitest (2) (push) Has been cancelled
Tests / vitest (3) (push) Has been cancelled
Tests / vitest (4) (push) Has been cancelled
This commit is contained in:
@@ -139,6 +139,8 @@ export default function Search({ options }) {
|
|||||||
}, [selectedProvider, options, query, searchSuggestions]);
|
}, [selectedProvider, options, query, searchSuggestions]);
|
||||||
|
|
||||||
function doSearch(value) {
|
function doSearch(value) {
|
||||||
|
if (!value) return;
|
||||||
|
|
||||||
const q = encodeURIComponent(value);
|
const q = encodeURIComponent(value);
|
||||||
const { url } = selectedProvider;
|
const { url } = selectedProvider;
|
||||||
if (url) {
|
if (url) {
|
||||||
|
|||||||
@@ -24,7 +24,19 @@ vi.mock("@headlessui/react", async () => {
|
|||||||
<div>{children}</div>
|
<div>{children}</div>
|
||||||
</ComboboxContext.Provider>
|
</ComboboxContext.Provider>
|
||||||
),
|
),
|
||||||
ComboboxInput: (props) => <input {...props} />,
|
ComboboxInput: (props) => {
|
||||||
|
const ctx = useContext(ComboboxContext);
|
||||||
|
// real headlessui fires onChange(null) when the input is cleared
|
||||||
|
return (
|
||||||
|
<input
|
||||||
|
{...props}
|
||||||
|
onChange={(event) => {
|
||||||
|
props.onChange?.(event);
|
||||||
|
if (event.target.value === "") ctx?.onChange?.(null);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
},
|
||||||
ComboboxOption: ({ as: As = "div", value, children, ...props }) => {
|
ComboboxOption: ({ as: As = "div", value, children, ...props }) => {
|
||||||
const ctx = useContext(ComboboxContext);
|
const ctx = useContext(ComboboxContext);
|
||||||
const content = typeof children === "function" ? children({ active: false }) : children;
|
const content = typeof children === "function" ? children({ active: false }) : children;
|
||||||
@@ -83,6 +95,21 @@ describe("components/widgets/search", () => {
|
|||||||
openSpy.mockRestore();
|
openSpy.mockRestore();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("does not search when the input is cleared", () => {
|
||||||
|
const openSpy = vi.spyOn(window, "open").mockImplementation(() => null);
|
||||||
|
|
||||||
|
renderWithProviders(<Search options={{ provider: ["google"], showSearchSuggestions: false, target: "_self" }} />, {
|
||||||
|
settings: {},
|
||||||
|
});
|
||||||
|
|
||||||
|
const input = screen.getByPlaceholderText("search.placeholder");
|
||||||
|
fireEvent.change(input, { target: { value: "a" } });
|
||||||
|
fireEvent.change(input, { target: { value: "" } });
|
||||||
|
|
||||||
|
expect(openSpy).not.toHaveBeenCalled();
|
||||||
|
openSpy.mockRestore();
|
||||||
|
});
|
||||||
|
|
||||||
it("accepts provider configured as a string", () => {
|
it("accepts provider configured as a string", () => {
|
||||||
const openSpy = vi.spyOn(window, "open").mockImplementation(() => null);
|
const openSpy = vi.spyOn(window, "open").mockImplementation(() => null);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user