Skip to content

ProSearchForm

ProSearchForm 基于 ProForm 实现,保留原有 fields 配置能力,并补充查询场景常用的:

  • 查询按钮
  • 重置按钮
  • 折叠 / 展开
  • 自动查询
  • 查询表单实例能力透传

基础用法

ProSearchForm 仍然使用 fields 描述查询项,最后一行会自动补上操作区。默认插槽会渲染在按钮区域里,适合补充额外操作按钮。

关键字
状态
请选择状态
部门
请选择部门
城市
请选择城市
备注
{
  "keyword": "",
  "status": "",
  "department": "",
  "city": "",
  "remark": ""
}

<template>
  <div class="pro-search-form-doc-demo">
    <ProSearchForm
      label-width="88px"
      :model="searchModel"
      :fields="fields"
      :gutter="16"
      :collapsed-count="3"
      button-justify="flex-start"
      default-collapsed
      @search="handleSearch"
      @reset="handleReset"
    >
      <el-button @click="fillKeyword">填充关键字</el-button>
    </ProSearchForm>

    <pre class="json">{{ JSON.stringify(searchModel, null, 2) }}</pre>
  </div>
</template>

<script setup lang="tsx">
import { computed, h, reactive } from 'vue'
import { ElInput, ElOption, ElSelect, ElMessage } from 'element-plus'
import {
  provideProFormComponents,
  type ProSearchFormFields,
} from '@coderhd/pro-element-plus'

type SearchModel = {
  keyword: string
  status: string
  department: string
  city: string
  remark: string
}

provideProFormComponents({
  ElInput,
  ElSelect,
})

const searchModel = reactive<SearchModel>({
  keyword: '',
  status: '',
  department: '',
  city: '',
  remark: '',
})

const statusOptions = [
  { label: '启用', value: 'enabled' },
  { label: '停用', value: 'disabled' },
]

const departmentOptions = [
  { label: '研发部', value: 'rd' },
  { label: '产品部', value: 'pm' },
  { label: '运营部', value: 'op' },
]

const cityOptions = [
  { label: '杭州', value: 'hangzhou' },
  { label: '上海', value: 'shanghai' },
  { label: '深圳', value: 'shenzhen' },
]

const fields = computed(
  (): ProSearchFormFields<SearchModel> => [
    {
      prop: 'keyword',
      label: '关键字',
      component: 'ElInput',
      componentProps: {
        placeholder: '请输入关键字',
        clearable: true,
      },
      colProps: {
        span: 8,
      },
    },
    {
      prop: 'status',
      label: '状态',
      component: 'ElSelect',
      componentProps: {
        placeholder: '请选择状态',
        clearable: true,
      },
      componentSlots: {
        default: () =>
          statusOptions.map((item) =>
            h(ElOption, {
              key: item.value,
              label: item.label,
              value: item.value,
            }),
          ),
      },
      colProps: {
        span: 8,
      },
    },
    {
      prop: 'department',
      label: '部门',
      component: 'ElSelect',
      componentProps: {
        placeholder: '请选择部门',
        clearable: true,
      },
      componentSlots: {
        default: () =>
          departmentOptions.map((item) =>
            h(ElOption, {
              key: item.value,
              label: item.label,
              value: item.value,
            }),
          ),
      },
      colProps: {
        span: 8,
      },
    },
    {
      prop: 'city',
      label: '城市',
      component: 'ElSelect',
      componentProps: {
        placeholder: '请选择城市',
        clearable: true,
        filterable: true,
      },
      componentSlots: {
        default: () =>
          cityOptions.map((item) =>
            h(ElOption, {
              key: item.value,
              label: item.label,
              value: item.value,
            }),
          ),
      },
      colProps: {
        span: 8,
      },
    },
    {
      prop: 'remark',
      label: '备注',
      component: 'ElInput',
      componentProps: {
        placeholder: '请输入备注',
        clearable: true,
      },
      colProps: {
        span: 16,
      },
    },
  ],
)

const handleSearch = () => {
  ElMessage.success('已触发查询')
}

const handleReset = () => {
  ElMessage.info('已执行重置')
}

const fillKeyword = () => {
  searchModel.keyword = '测试关键字'
}
</script>

<style lang="scss" scoped>
.json {
  margin-top: 20px;
  padding: 16px;
  border-radius: 12px;
  background: var(--el-fill-color-light);
  color: var(--el-text-color-regular);
  overflow: auto;
}
</style>

API

Props

属性名说明类型默认值
fields查询项配置ProSearchFormFields[][]
initSearch是否初始化时立即触发查询监听booleantrue
autoSearch是否在 model 变化后自动查询booleantrue
autoSearchDebounce自动查询防抖时间number300
resetAfterSearch重置后是否自动执行查询booleantrue
buttonJustify按钮区对齐方式flex-start | flex-end | center | space-between | space-around | space-evenlyflex-start
hideSearchButton是否隐藏查询按钮booleanfalse
hideResetButton是否隐藏重置按钮booleanfalse
hideCollapseButton是否隐藏折叠按钮booleanfalse
defaultCollapsed是否默认折叠booleanfalse
collapsedCount折叠时保留的字段数number1
resetButtonText重置按钮文本string重置
searchButtonText查询按钮文本string查询
collapseButtonText折叠按钮文本[string, string]['展开', '收起']
...ProForm props继续透传 ProForm 的表单与布局能力--

Events

事件名说明回调参数
search查询成功时触发(params?: Record<string, any>) => void
reset点击重置时触发() => void
collapsed折叠状态变化时触发(collapsed: boolean) => void
validate继续透传 el-formvalidate 事件(prop, isValid, message) => void

Exposes

ProSearchForm 除了透传 ProForm / el-form 的实例方法外,还额外暴露:

  • reset
  • search
  • toggleCollapse

同时继续可用:

  • getInstance
  • validate
  • validateField
  • resetFields
  • scrollToField
  • clearValidate
  • fields
  • getField