<tap-pat-search>

Search page. Shows recent searches while the query is empty, and results otherwise. Suited to remote search; for filtering a local list use <tap-pat-picker searchable>.

Typing emits search; the caller fetches the results and sets items (with loading meanwhile). Pressing Enter or tapping a recent search emits submit, which can be used to save the history. Selecting a result only emits change.

Example

Ignore stale responses so a slow request does not overwrite newer results.

let latest = '';

const onSearch = async (evt) => {
  const search = evt.target;
  const query = (latest = evt.detail);
  search.loading = !!query;
  if (!query) return;
  const items = await fetchSuggestions(query);
  if (query !== latest) return;
  search.items = items;
  search.loading = false;
};

API

搜索页,空查询时显示搜索历史,有查询时显示结果

输入时发出 search,结果由调用方异步获取后设置 items;选择后只发出 change

Extends GemElement

Instance Properties

Property(Attribute) Reactive Type Description
heading(heading) Yes string
placeholder(placeholder) Yes string
back(back) Yes boolean Show the back button of the navbar, pops the closest stack
loading(loading) Yes boolean Show the loading progress while fetching items
items Yes SearchItem[] Results of the current query
history Yes string[] Recent queries, shown when the query is empty
search Emitter<string> Query changed, fetch the results
submit Emitter<string> Enter pressed or a recent query tapped, e.g. save it to the history
change Emitter<string>
clearhistory Emitter<null>

Other

Type Value
Event search, submit, change, clearhistory