<tap-pat-search>

搜索页。查询为空时显示最近搜索,否则显示结果。适合远程搜索;过滤本地列表请用 <tap-pat-picker searchable>。

输入时发出 search,由调用方获取结果后设置 items(期间设置 loading)。回车或点击最近搜索时发出 submit,可用来保存历史。选择结果只发出 change。

Example

远程搜索

忽略过期的响应,避免慢请求覆盖较新的结果。

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