Skip to content
React NativeBetaMobile only

SearchBar

A rounded search field with a clear button, an optional iOS-style Cancel, and a debounced onSearch.

Beta. React Native support is in beta. APIs may change or break between prereleases (npm tag native).

On this page
Web preview — real device may differ
tsx
import { useState } from "react";
import { Text } from "react-native";
import { useKitTheme } from "@/component-lib/shared/native";
import { List, ListItem } from "@/component-lib/list-item";
import { SearchBar } from "@/component-lib/search-bar";
import { Section } from "./layout"; // Section and Row: page layout for the demo
 
const CITIES = ["Kochi", "Kozhikode", "Kollam", "Kannur", "Thiruvananthapuram", "Thrissur", "Palakkad", "Bengaluru", "Chennai", "Mumbai"];
 
export function SearchBarDemo() {
  const { tokens } = useKitTheme();
  const [query, setQuery] = useState("");
  const [loading, setLoading] = useState(false);
  const [submitted, setSubmitted] = useState("");
  const results = query ? CITIES.filter((c) => c.toLowerCase().includes(query.toLowerCase())) : [];
  return (
    <>
      <Section title="SearchBar · debounced, with Cancel">
        <SearchBar
          showCancel
          loading={loading}
          onValueChange={() => setLoading(true)}
          onSearch={(q) => {
            setQuery(q);
            setLoading(false);
          }}
          onSubmit={setSubmitted}
          localeText={{ placeholder: "Search cities" }}
        />
        <Text style={{ color: tokens.muted, fontSize: tokens.fontSize - 2 }}>
          {query ? `${results.length} result(s) for “${query}”` : "Results appear 250 ms after you stop typing."}
          {submitted ? ` · submitted “${submitted}”` : ""}
        </Text>
        {results.length > 0 && (
          <List variant="inset">
            {results.map((city) => (
              <ListItem key={city} title={city} />
            ))}
          </List>
        )}
      </Section>
      <Section title="SearchBar · plain and disabled">
        <SearchBar defaultValue="GST invoices" />
        <SearchBar disabled />
      </Section>
    </>
  );
}

Install

npx @grampro/kit@native add SearchBar

Usage

tsx
import { SearchBar } from "@/component-lib/search-bar";
 
<SearchBar showCancel loading={pending} onSearch={(query) => setQuery(query)} />

Props

SearchBarProps

PropTypeDescription
valuestring
defaultValuestring
onValueChange(value: string) => voidEvery keystroke.
onSubmit(value: string) => voidThe keyboard's search key.
onSearch(value: string) => voidDebounced by debounce ms; also called with "" when cleared.
debouncenumberDelay for onSearch, in ms. Default 250.
showCancelbooleanShow a Cancel text button while focused (iOS style). Cancel clears the text and dismisses the keyboard. Default false.
onCancel() => void
loadingbooleanSpinner in place of the clear button, e.g. while results load.
autoFocusboolean
disabledboolean
placeholderstringOverrides the locale placeholder.
localeTextPartial<SearchBarLocaleText>
styleStyleProp<ViewStyle>
stylesPartial<{ field: StyleProp<ViewStyle>; input: StyleProp<TextStyle>; cancel: StyleProp<T…

Notes

  • onSearch is debounced (250ms by default) and called with "" when cleared.
  • Mobile only: there is no web version of this component.