Skip to content

[RFC] Typeahead/Tokenizer: no way to hold the search until the query is long enough #5384

Closed

Description

@freddymeta

Problem Statement

A typeahead backed by a remote source has no way to say "don't search until the user has typed enough to be worth searching". BaseTypeahead searches on every non-empty query, and performSearch opens the layer whenever searchQuery.length > 0 — even when the source returned nothing — so at one character the user gets a request they didn't need and a menu that says No results found for a query that matched too much, not too little.

The user-visible half is the worse one: "no results" is a factual claim about the data. After one character of a name search it is usually false, and it is the state that teaches people the field is broken.

Evidence of Demand

  • Astryx already has the concept, one layer up. PowerSearchField.typeaheadMinQueryLength (packages/core/src/PowerSearch/types.ts) exists precisely so a field stays out of the typeahead until the query is long enough, and usePowerSearchSource enforces it. It is scoped to PowerSearch's own field list; a plain Typeahead/Tokenizer over a remote source has no equivalent.
  • External systems ship it as a first-class prop, with the same default. jQuery UI Autocomplete: minLength, default 1 — "the minimum number of characters a user must type before a search is performed … a higher value should be used when a single character search could match a few thousand items". PrimeReact AutoComplete: minLength, default 1, gating on query.length >= props.minLength.
  • Internal migration signal. In the Meta-internal design system currently being migrated onto Astryx, the equivalent prop is set at 36 call sites on its typeahead/tokenizer — people-search fields over large directories, where 1–2 characters match thousands of rows.

Why Existing Components Don't Cover This

A SearchSource can already refuse to answer a short query — return [] and no request goes out. That covers the network half and nothing else:

const source: SearchSource<User> = {
  search: q => (q.length < 3 ? [] : fetchUsers(q)),
  bootstrap: () => [],
};

The menu still opens on the first character and renders the empty state, because the open decision lives inside BaseTypeahead (performSearch → showLayer() when searchQuery.length > 0) and is not reachable from the outside: there is no controlled isOpen, no imperative handle, and onOpenChange only reports. From a wrapper the only lever left is hiding Astryx's own popover with CSS, which is worse than the honest empty state it replaces.

The result-count live-region announcement has the same shape — screen-reader users hear "no results found" for the search that never ran.

Rough Approaches Considered

A. A threshold prop on the engine, forwarded by Typeahead and Tokenizer. Below it: no search, no announcement, menu closed. Default 1 = today's behaviour exactly.

<Typeahead label="Assignee" searchSource={userSource} value={v} onChange={setV} minQueryLength={3} />

Naming: minLength is what jQuery UI and PrimeReact call it, but on a component that renders an <input> it reads as the native minlength validation attribute — a different thing about a different string. minQueryLength says which string is being measured and matches the typeaheadMinQueryLength already in the repo.

B. Expose the open state instead (controlled isOpen, or an imperative handle). More general, and a much larger surface — it hands out the layer's whole state machine to solve one threshold.

C. Let the source signal "not yet" — e.g. a sentinel return distinct from "no matches". Keeps the API count flat, but puts a protocol in the data layer for something that is a UI decision, and every source implementer then has to know the protocol.

A is the one I've prototyped; the other two are here to show the space.

Accessibility Considerations

No change to the combobox pattern (ARIA 1.2 combobox + listbox popup). Below the threshold the popup simply never opens, so aria-expanded stays false and no aria-activedescendant is published — the same state as an untouched field, which is what the user is in.

The gain is in what is not said: the live-region result-count announcement currently fires for every keystroke, so a 1-character query announces "no results found" before the user has finished the first syllable. Holding the search holds that announcement too.

Open question for the team, and the reason this is an RFC rather than just a PR: should a below-threshold field say anything? Options are (a) silence — the field looks untouched, (b) a menu with a "type N or more characters" hint, which is what the internal system does today and is a new user-facing string plus an i18n key, or (c) leave it to the consumer's description/help text. I lean (a) for the component and (c) for products, but that is a design call, not mine.

Pre-submission Checklist

  • I have read the Contributing guide
  • I have read the API Conventions
  • I have checked that existing Astryx components cannot compose to solve this
  • This is a general-purpose UI pattern (not specific to one product)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions