SearchForm.vue 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <script setup lang="ts">
  2. import { Refresh, Search } from '@element-plus/icons-vue'
  3. import { ElButton, ElCol, ElForm, ElFormItem, ElInput, ElOption, ElRow, ElSelect } from 'element-plus'
  4. import { ref } from 'vue'
  5. type TSearchParams = {
  6. nameOrTags: string,
  7. }
  8. interface Props {
  9. modelValue: TSearchParams
  10. loading?: boolean
  11. }
  12. interface Emits {
  13. (e: 'update:modelValue', value: TSearchParams): void
  14. (e: 'search'): void
  15. (e: 'reset'): void
  16. }
  17. const props = withDefaults(defineProps<Props>(), {
  18. loading: false,
  19. })
  20. const emit = defineEmits<Emits>()
  21. const formRef = ref()
  22. const searchParams = computed({
  23. get: () => props.modelValue,
  24. set: value => emit('update:modelValue', value),
  25. })
  26. /**
  27. * 搜索
  28. */
  29. function handleSearch() {
  30. emit('search')
  31. }
  32. /**
  33. * 重置
  34. */
  35. function handleReset() {
  36. formRef.value?.resetFields()
  37. emit('reset')
  38. }
  39. function handleClear() {
  40. // 清空后自动触发搜索
  41. handleSearch()
  42. }
  43. </script>
  44. <template>
  45. <ElForm :inline="true" ref="formRef" :model="searchParams" label-width="80px">
  46. <ElFormItem label="名称" prop="nameOrTags" label-width="120">
  47. <ElInput v-model="searchParams.nameOrTags" placeholder="请输入" clearable
  48. @clear="handleClear" />
  49. </ElFormItem>
  50. <ElFormItem>
  51. <ElButton type="primary" :icon="Search" :loading="loading" @click="handleSearch">
  52. 查询
  53. </ElButton>
  54. <ElButton :icon="Refresh" @click="handleReset">
  55. 重置
  56. </ElButton>
  57. </ElFormItem>
  58. </ElForm>
  59. </template>