KSearchBar 搜索栏
配置驱动的搜索表单,把常见的 input / select / date / cascader 等字段收敛到一份 items 声明里。 点击「搜索」时会对输出做一次清理(默认去掉空字符串、空数组、空对象,保留 0 和 false)。
基础用法
items 中每项至少要有 key;type 决定渲染哪种 Element Plus 表单控件。支持 v-model 双向绑定。
搜索条件
搜索结果:
(尚未搜索)docs/examples/search-bar/basic.vue
vue
<script setup lang="ts">
import { ref } from 'vue';
import { KSearchBar } from '@wujies/components';
const model = ref({ keyword: '', status: '', date: '' });
const result = ref('');
const items = [
{ key: 'keyword', label: '关键字', type: 'input', placeholder: '名称 / 编码', span: 8 },
{
key: 'status',
label: '状态',
type: 'select',
span: 8,
options: [
{ label: '启用', value: '1' },
{ label: '停用', value: '0' }
]
},
{ key: 'date', label: '创建日期', type: 'date', span: 8 }
];
function onSearch(value: Record<string, any>) {
result.value = JSON.stringify(value);
}
function onReset() {
result.value = '';
}
</script>
<template>
<div>
<KSearchBar v-model="model" :items="items" @search="onSearch" @reset="onReset" />
<div style="margin-top: 14px; font-size: 13px; color: var(--el-text-color-secondary)">
搜索结果:<code>{{ result || '(尚未搜索)' }}</code>
</div>
</div>
</template>
API
Attributes
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| v-model | 表单值对象 | Record<string, any> | {} |
| items | 字段配置列表 | KSearchBarItem[] | [] |
| span | 默认栅格宽度(24 栅格) | number | 6 |
| actionSpan | 操作按钮占宽 | number | 6 |
| gutter | el-row 间距 | number | 16 |
| labelPosition | 标签位置 | left / right / top | right |
| showReset | 是否显示重置按钮 | boolean | true |
| showSearch | 是否显示搜索按钮 | boolean | true |
| title | 折叠面板标题 | string | 搜索条件 |
| searchText | 搜索按钮文案 | string | 搜索 |
| resetText | 重置按钮文案 | string | 重置 |
| sanitizeOutput | 输出清理规则 | Partial<SanitizeOutputOptions> | {} |
KSearchBarItem 常用字段:key、label、type、hidden、span(及 sm/md/lg/xl)、placeholder、options、props、render。 支持的 type:input、number、select、switch、checkbox、checkboxgroup、radiogroup、date、daterange、datetime、datetimerange、cascader、timepicker、timeselect、treeselect。
Events
| 事件名 | 说明 | 回调参数 |
|---|---|---|
| search | 点击搜索 | 清理后的值对象 |
| reset | 点击重置 | — |
| update:modelValue | 表单值变化 | 值对象 |
Slots
| 插槽名 | 说明 |
|---|---|
[item.key] | 自定义某个字段,作用域参数 { item, modelValue } |
| actions-before / actions-after | 搜索/重置按钮前后 |
Exposes
reset()、validate(...args)、getOutput()、formRef。