search-form.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <!-- 搜索框入口 -->
  3. <wd-search
  4. :placeholder="searchPlaceholder"
  5. :hide-cancel="true"
  6. disabled
  7. @click="visible = true"
  8. />
  9. <!-- 搜索弹窗 -->
  10. <wd-popup
  11. v-model="visible"
  12. position="top"
  13. custom-style="border-radius: 0 0 24rpx 24rpx;"
  14. safe-area-inset-top
  15. @close="visible = false"
  16. >
  17. <view class="p-32rpx">
  18. <view class="mb-24rpx text-32rpx text-[#333] font-semibold">
  19. 搜索日志
  20. </view>
  21. <view class="mb-24rpx">
  22. <view class="mb-12rpx text-28rpx text-[#666]">
  23. 用户编号
  24. </view>
  25. <wd-input
  26. v-model="formData.userId"
  27. placeholder="请输入用户编号"
  28. clearable
  29. />
  30. </view>
  31. <view class="mb-32rpx">
  32. <view class="mb-12rpx text-28rpx text-[#666]">
  33. 应用名
  34. </view>
  35. <wd-input
  36. v-model="formData.applicationName"
  37. placeholder="请输入应用名"
  38. clearable
  39. />
  40. </view>
  41. <!-- TODO @芋艿:后续增加时间范围的检索 -->
  42. <view class="w-full flex justify-center gap-24rpx">
  43. <wd-button class="flex-1" plain @click="handleReset">
  44. 重置
  45. </wd-button>
  46. <wd-button class="flex-1" type="primary" @click="handleSearch">
  47. 搜索
  48. </wd-button>
  49. </view>
  50. </view>
  51. </wd-popup>
  52. </template>
  53. <script lang="ts" setup>
  54. import { computed, reactive, ref, watch } from 'vue'
  55. /** 搜索表单数据 */
  56. export interface SearchFormData {
  57. userId?: number
  58. applicationName?: string
  59. }
  60. const props = defineProps<{
  61. searchParams?: Partial<SearchFormData> // 初始搜索参数
  62. }>()
  63. const emit = defineEmits<{
  64. search: [data: SearchFormData]
  65. reset: []
  66. }>()
  67. const visible = ref(false)
  68. const formData = reactive<SearchFormData>({
  69. userId: undefined,
  70. applicationName: undefined,
  71. })
  72. /** 搜索条件 placeholder 拼接 */
  73. const searchPlaceholder = computed(() => {
  74. const conditions: string[] = []
  75. if (props.searchParams?.userId) {
  76. conditions.push(`用户编号:${props.searchParams.userId}`)
  77. }
  78. if (props.searchParams?.applicationName) {
  79. conditions.push(`应用名:${props.searchParams.applicationName}`)
  80. }
  81. return conditions.length > 0 ? conditions.join(' | ') : '搜索日志'
  82. })
  83. /** 监听弹窗打开,同步外部参数 */
  84. watch(visible, (val) => {
  85. if (val && props.searchParams) {
  86. formData.userId = props.searchParams.userId
  87. formData.applicationName = props.searchParams.applicationName
  88. }
  89. })
  90. /** 搜索 */
  91. function handleSearch() {
  92. visible.value = false
  93. emit('search', { ...formData })
  94. }
  95. /** 重置 */
  96. function handleReset() {
  97. formData.userId = undefined
  98. formData.applicationName = undefined
  99. visible.value = false
  100. emit('reset')
  101. }
  102. </script>