Skip to content
العربية
Forms

Autocomplete

A text input with suggestions. The typed text is always the value; a suggestion only fills it in. No row is highlighted until you press Down or Up. Rows can show a detail. Server search: onQueryChange gets each typed query and suggestions are shown as given; loading and unavailable show a status row, and emptyText shows a row when nothing matches. openOnFocus opens the list when focus comes from a pointer or the Tab key. token suggests for one part of the text, such as one item of a comma list. mono is for names, hashes and paths. start puts an icon before the text; the list then has the width of the whole field.

import { Autocomplete } from "@percentsa/ui";

Examples

Type any text; suggestions open on focus

Code
export function AutocompleteExample() {
  const t = useT();
  const [city, setCity] = useState("");
  const cities = useCitySuggestions();
  return (
    <Field label={t("City", "المدينة")} hint={t("Type any text; suggestions open on focus", "اكتب أي نص؛ تظهر الاقتراحات عند التركيز على الحقل")} className="w-full max-w-xs">
      <Autocomplete value={city} onValueChange={setCity} suggestions={cities} openOnFocus name="city" start={<MapPin aria-hidden />} />
    </Field>
  );
}
Code
export function AutocompleteAsyncExample() {
  const t = useT();
  const [value, setValue] = useState("");
  const [results, setResults] = useState<AutocompleteSuggestion[]>([]);
  const [loading, setLoading] = useState(false);
  const [unavailable, setUnavailable] = useState(false);
  const cities = useCitySuggestions();
  const timer = useRef<ReturnType<typeof setTimeout>>(undefined);
  useEffect(() => () => clearTimeout(timer.current), []);
  // Stands in for a server search. A real app debounces the query in its data hook.
  const search = (query: string) => {
    clearTimeout(timer.current);
    setLoading(true);
    timer.current = setTimeout(() => {
      setResults(cities.filter((o) => searchKey(o.value).includes(searchKey(query))));
      setLoading(false);
    }, 600);
  };
  return (
    <Stack gap={4} className="w-full max-w-xs">
      <Field label={t("City", "المدينة")}>
        <Autocomplete
          value={value}
          onValueChange={setValue}
          suggestions={unavailable ? [] : results}
          onQueryChange={search}
          loading={loading}
          unavailable={unavailable}
          emptyText={t("No city with this name. Your text stays as typed.", "لا توجد مدينة بهذا الاسم. يبقى نصك كما كتبته.")}
          name="city-search"
        />
      </Field>
      <Switch label={t("Search is unavailable", "البحث غير متاح")} checked={unavailable} onCheckedChange={setUnavailable} />
    </Stack>
  );
}

const TAGS: AutocompleteSuggestion[] = [{ value: "alpha" }, { value: "beta" }, { value: "stable" }, { value: "staging" }, { value: "release" }];

/** The range of the comma-separated item at the cursor, without the spaces around it. */
const tagAt = (text: string, cursor: number) => {
  let start = text.lastIndexOf(",", cursor - 1) + 1;
  while (text[start] === " ") start++;
  let end = text.indexOf(",", cursor);
  if (end === -1) end = text.length;
  while (end > start && text[end - 1] === " ") end--;
  return { start: Math.min(start, cursor), end: Math.max(end, cursor) };
};

Separate with commas; each item gets suggestions

Code
export function AutocompleteTokenExample() {
  const t = useT();
  const [tags, setTags] = useState("alpha, st");
  return (
    <Field label={t("Tags", "الوسوم")} hint={t("Separate with commas; each item gets suggestions", "افصل بفواصل؛ يحصل كل عنصر على اقتراحات")} className="w-full max-w-xs">
      <Autocomplete value={tags} onValueChange={setTags} suggestions={TAGS} token={tagAt} mono name="tags" />
    </Field>
  );
}