SearchForm.vue 1.5 KB

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