SearchBar 搜索栏
注:
- 该组件本身没有固定顶部功能,可以配合 Sticky 组件实现置顶功能。
vue
<template>
<ta-group title="基础用法">
<ta-search-bar />
</ta-group>
<ta-group title="搜索建议">
<ta-search-bar @input="onInput" />
</ta-group>
<ta-group title="显示取消按钮">
<ta-search-bar show-cancel />
</ta-group>
<ta-group title="设置候选项">
<ta-search-bar :placeholders="placeholders" />
</ta-group>
<ta-group title="深色适配">
<ta-search-bar class="exp-searchBar-dark-style" show-cancel ghost />
</ta-group>
<ta-group title="只读(readonly=true)">
<ta-search-bar readonly :placeholders="placeholders" />
</ta-group>
<ta-group title="事件监听">
<ta-search-bar
show-cancel
:placeholders="placeholders"
@input="onInput2"
@focus="onFocus"
@blur="onBlur"
@cancel="onCancel"
@search="onSearch"
>
</ta-search-bar>
</ta-group>
<ta-group title="事件监听(readonly=true)">
<ta-search-bar readonly :placeholders="placeholders" @field-click="onClick" />
</ta-group>
</template>
<script lang="ts">
import { placeholders } from './data'
import {
type SearchBarOnFieldClick,
type SearchBarOnInput,
type SearchBarOnSearch,
showToast
} from '@/index'
import { defineComponent } from 'vue'
export default defineComponent({
name: 'ExpSearchBar',
setup() {
const onCancel = () => {
showToast('取消按钮点击')
}
const onInput: SearchBarOnInput = (text, fn) => {
fn(
text
? 'ABCD'.split('').map(v => {
return {
text: `${text} ${v}`,
tags: ['tag1', 'tag2']
}
})
: []
)
}
const onInput2: SearchBarOnInput = (text, fn) => {
showToast(`输入了 ${text}`)
onInput(text, fn)
}
const onSearch: SearchBarOnSearch = res => {
console.log('search', res)
showToast(`搜索了 ${res.text}`)
}
const onClick: SearchBarOnFieldClick = res => {
console.log('field-click', res)
showToast(`搜索词 ${res.text}`)
}
return {
placeholders,
onFocus: () => showToast('focus'),
onBlur: () => showToast('blur'),
onCancel,
onInput,
onInput2,
onSearch,
onClick
}
}
})
</script>
Import
js
import { TaSearchBar } from 'tantalum-ui-mobile'
具体的引入方式可以参考引入组件。
Import Type
组件导出的类型定义:
ts
import type {
SearchBarSetSuggestList,
SearchBarOnFocus,
SearchBarOnBlur,
SearchBarOnInput,
SearchBarOnSearch,
SearchBarOnFieldClick
} from 'tantalum-ui-mobile'
Props
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
placeholders | string/string[] | 否 | 设置候选值列表,多个时轮询显示 | |
placeholder-interval | number | 5000 | 否 | placeholder 切换时间,placeholders 有多个时生效 |
ghost | boolean | false | 否 | 是否开启反色模式,开始后适用于深色底色,通过 CSS 设置背景色 |
readonly | boolean | false | 否 | 是否只读模式,开启后主要用于搜索入口场景 |
maxlength | number | 100 | 否 | 最大长度 |
focus | boolean | false | 否 | 加载时是否获取焦点 |
show-cancel | boolean | false | 否 | 是否展示取消按钮 |
Events
事件 | 描述 | 回调函数参数 | TypeScript 函数 |
---|---|---|---|
input | 输入值改变时触发,包括 clear 的 | ( text: string, setSuggestList: SearchBarSetSuggestList ) | SearchBarOnInput |
focus | 输入框获取焦点时触发 | ( text: string, setSuggestList: SearchBarSetSuggestList ) | SearchBarOnFocus |
blur | 输入框移出焦点时触发 | ( text: string, setSuggestList: SearchBarSetSuggestList ) | SearchBarOnBlur |
cancel-click | 取消按钮点击 | e: Event | |
field-click | 输入框点击,配合 readonly 使用,获取到当前候选值 | payload: { text: string } | SearchBarOnFieldClick |
search | 触发搜索时 | payload: { text: string, source: string } | SearchBarOnSearch |
SearchBarSetSuggestList
ts
type SearchBarSetSuggestList = (
res: (
| string
| number
| {
text: string | number
tags?: string[]
}
)[]
) => void
const suggestList = ['suggest A', 'suggest B']
// Or
const suggestList = [
{
text: '油烟机',
tags: ['厨房', '电器']
}
]