Skip to content

Commit

Permalink
feat(List): add auto submit to search (#1069)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lisa18289 authored Dec 18, 2024
1 parent d7403d3 commit 93bb0d6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FC, KeyboardEvent } from "react";
import React, { createElement, useState } from "react";
import React, { createElement, useEffect, useState } from "react";
import type { PropsWithClassName } from "@/lib/types/props";
import type { Search } from "@/components/List/model/search/Search";
import type { SearchFieldRenderComponent } from "@/components/List/model/search/types";
Expand All @@ -10,11 +10,28 @@ interface Props extends PropsWithClassName {
search: Search<never>;
}

const autoSubmitTimeout = 800;

const DefaultSearchFieldRender: SearchFieldRenderComponent = (props) => {
const { className, onChange, value, ...searchFieldProps } = props;
const { className, onChange, value, autoSubmit, ...searchFieldProps } = props;

const [searchString, setSearchString] = useState(value ?? "");

const submitSearch = () => {
if (searchString.trim() === "") {
onChange(undefined);
} else {
onChange(searchString);
}
};

useEffect(() => {
if (autoSubmit) {
const timeout = setTimeout(() => submitSearch(), autoSubmitTimeout);
return () => clearTimeout(timeout);
}
}, [searchString, autoSubmit]);

useOnChange(value, () => {
setSearchString(value ?? "");
}, [searchString]);
Expand All @@ -26,11 +43,7 @@ const DefaultSearchFieldRender: SearchFieldRenderComponent = (props) => {

const handleKeyPress = (e: KeyboardEvent) => {
if (e.key === "Enter") {
if (searchString.trim() === "") {
onChange(undefined);
} else {
onChange(searchString);
}
submitSearch();
} else if (e.key === "Escape") {
clearSearch();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type SupportedSearchFieldProps = Pick<
interface SearchFieldRenderProps extends SupportedSearchFieldProps {
onChange: (value: SearchValue) => unknown;
value: SearchValue;
autoSubmit?: boolean;
}

export type SearchFieldRenderComponent = ComponentType<SearchFieldRenderProps>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { ComponentType } from "react";

type Props<T> = Omit<SearchShape<T>, "render" | "textFieldProps"> & {
children?: SearchFieldRenderComponent;
autoSubmit?: boolean;
} & SearchShape<T>["textFieldProps"];

export function ListSearch<T = never>(ignoredProps: Props<T>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const meta: Meta<typeof List> = {
name="Typ"
defaultSelected={["Domain"]}
/>
<DomainList.Search autoFocus />
<DomainList.Search autoFocus autoSubmit />
<DomainList.Sorting property="domain" name="A-Z" />
<DomainList.Sorting property="domain" name="Z-A" direction="desc" />
<DomainList.Sorting property="type" name="Typ" defaultEnabled />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Nutze `<List.Sorting />` innerhalb der List, um eine Sortiermethode anzulegen.

Verwende `<List.Search />` innerhalb der List, um ein
[SearchField](/03-components/form-controls/search-field) anzuzeigen.
Standardmäßig wird die Suche bei Klick auf Enter gestartet. Um die Suche
automatisch zu starten, kann das Property `autoSubmit` gesetzt werden.

<LiveCodeEditor example="search" editorCollapsed />

Expand Down

0 comments on commit 93bb0d6

Please sign in to comment.