Search.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <script setup lang="ts">
  2. import { ContentWrap } from '@/components/ContentWrap'
  3. import { useI18n } from '@/hooks/web/useI18n'
  4. import { Search } from '@/components/Search'
  5. import { reactive, ref, unref } from 'vue'
  6. import { getDictOneApi } from '@/api/common'
  7. import { FormSchema } from '@/components/Form'
  8. import { useSearch } from '@/hooks/web/useSearch'
  9. const { t } = useI18n()
  10. const { searchRegister, searchMethods } = useSearch()
  11. const { setSchema, setProps, setValues, getFormData } = searchMethods
  12. const treeSelectData = [
  13. {
  14. value: '1',
  15. label: 'Level one 1',
  16. children: [
  17. {
  18. value: '1-1',
  19. label: 'Level two 1-1',
  20. children: [
  21. {
  22. value: '1-1-1',
  23. label: 'Level three 1-1-1'
  24. }
  25. ]
  26. }
  27. ]
  28. },
  29. {
  30. value: '2',
  31. label: 'Level one 2',
  32. children: [
  33. {
  34. value: '2-1',
  35. label: 'Level two 2-1',
  36. children: [
  37. {
  38. value: '2-1-1',
  39. label: 'Level three 2-1-1'
  40. }
  41. ]
  42. },
  43. {
  44. value: '2-2',
  45. label: 'Level two 2-2',
  46. children: [
  47. {
  48. value: '2-2-1',
  49. label: 'Level three 2-2-1'
  50. }
  51. ]
  52. }
  53. ]
  54. },
  55. {
  56. value: '3',
  57. label: 'Level one 3',
  58. children: [
  59. {
  60. value: '3-1',
  61. label: 'Level two 3-1',
  62. children: [
  63. {
  64. value: '3-1-1',
  65. label: 'Level three 3-1-1'
  66. }
  67. ]
  68. },
  69. {
  70. value: '3-2',
  71. label: 'Level two 3-2',
  72. children: [
  73. {
  74. value: '3-2-1',
  75. label: 'Level three 3-2-1'
  76. }
  77. ]
  78. }
  79. ]
  80. }
  81. ]
  82. // 模拟远程加载
  83. const getTreeSelectData = () => {
  84. return new Promise((resolve) => {
  85. setTimeout(() => {
  86. resolve(treeSelectData)
  87. }, 3000)
  88. })
  89. }
  90. const schema = reactive<FormSchema[]>([
  91. {
  92. field: 'field1',
  93. label: t('formDemo.input'),
  94. component: 'Input'
  95. },
  96. {
  97. field: 'field2',
  98. label: t('formDemo.select'),
  99. component: 'Select',
  100. componentProps: {
  101. options: [
  102. {
  103. label: 'option1',
  104. value: '1'
  105. },
  106. {
  107. label: 'option2',
  108. value: '2'
  109. }
  110. ],
  111. on: {
  112. change: (value: string) => {
  113. console.log(value)
  114. }
  115. }
  116. }
  117. },
  118. {
  119. field: 'field3',
  120. label: t('formDemo.radio'),
  121. component: 'RadioGroup',
  122. componentProps: {
  123. options: [
  124. {
  125. label: 'option-1',
  126. value: '1'
  127. },
  128. {
  129. label: 'option-2',
  130. value: '2'
  131. }
  132. ]
  133. }
  134. },
  135. {
  136. field: 'field5',
  137. component: 'DatePicker',
  138. label: t('formDemo.datePicker'),
  139. componentProps: {
  140. type: 'date'
  141. }
  142. },
  143. {
  144. field: 'field6',
  145. component: 'TimeSelect',
  146. label: t('formDemo.timeSelect')
  147. },
  148. {
  149. field: 'field8',
  150. label: t('formDemo.input'),
  151. component: 'Input'
  152. },
  153. {
  154. field: 'field9',
  155. label: t('formDemo.input'),
  156. component: 'Input'
  157. },
  158. {
  159. field: 'field10',
  160. label: t('formDemo.input'),
  161. component: 'Input'
  162. },
  163. {
  164. field: 'field11',
  165. label: t('formDemo.input'),
  166. component: 'Input'
  167. },
  168. {
  169. field: 'field12',
  170. label: t('formDemo.input'),
  171. component: 'Input'
  172. },
  173. {
  174. field: 'field13',
  175. label: t('formDemo.input'),
  176. component: 'Input'
  177. },
  178. {
  179. field: 'field14',
  180. label: t('formDemo.input'),
  181. component: 'Input'
  182. },
  183. {
  184. field: 'field15',
  185. label: t('formDemo.input'),
  186. component: 'Input'
  187. },
  188. {
  189. field: 'field16',
  190. label: t('formDemo.input'),
  191. component: 'Input'
  192. },
  193. {
  194. field: 'field17',
  195. label: t('formDemo.input'),
  196. component: 'Input'
  197. },
  198. {
  199. field: 'field18',
  200. label: t('formDemo.input'),
  201. component: 'Input'
  202. },
  203. {
  204. field: 'field19',
  205. label: `${t('formDemo.treeSelect')}`,
  206. component: 'TreeSelect',
  207. // 远程加载option
  208. optionApi: async () => {
  209. const res = await getTreeSelectData()
  210. return res
  211. }
  212. }
  213. ])
  214. const isGrid = ref(false)
  215. const changeGrid = (grid: boolean) => {
  216. setProps({
  217. isCol: grid
  218. })
  219. // isGrid.value = grid
  220. }
  221. const layout = ref('inline')
  222. const changeLayout = () => {
  223. layout.value = unref(layout) === 'inline' ? 'bottom' : 'inline'
  224. }
  225. const buttonPosition = ref('left')
  226. const changePosition = (position: string) => {
  227. layout.value = 'bottom'
  228. buttonPosition.value = position
  229. }
  230. const getDictOne = async () => {
  231. const res = await getDictOneApi()
  232. if (res) {
  233. setSchema([
  234. {
  235. field: 'field2',
  236. path: 'componentProps.options',
  237. value: res.data
  238. }
  239. ])
  240. }
  241. }
  242. const handleSearch = async (data: any) => {
  243. const formData = await getFormData()
  244. console.log(formData)
  245. console.log(data)
  246. }
  247. const delRadio = () => {
  248. setSchema([
  249. {
  250. field: 'field3',
  251. path: 'remove',
  252. value: true
  253. }
  254. ])
  255. }
  256. const restoreRadio = () => {
  257. setSchema([
  258. {
  259. field: 'field3',
  260. path: 'remove',
  261. value: false
  262. }
  263. ])
  264. }
  265. const setValue = () => {
  266. setValues({
  267. field1: 'Joy'
  268. })
  269. }
  270. const searchLoading = ref(false)
  271. const changeSearchLoading = () => {
  272. searchLoading.value = true
  273. setTimeout(() => {
  274. searchLoading.value = false
  275. }, 2000)
  276. }
  277. const resetLoading = ref(false)
  278. const changeResetLoading = () => {
  279. resetLoading.value = true
  280. setTimeout(() => {
  281. resetLoading.value = false
  282. }, 2000)
  283. }
  284. </script>
  285. <template>
  286. <ContentWrap
  287. :title="`${t('searchDemo.search')} ${t('searchDemo.operate')}`"
  288. style="margin-bottom: 20px"
  289. >
  290. <BaseButton @click="changeGrid(true)">{{ t('searchDemo.grid') }}</BaseButton>
  291. <BaseButton @click="changeGrid(false)">
  292. {{ t('searchDemo.restore') }} {{ t('searchDemo.grid') }}
  293. </BaseButton>
  294. <BaseButton @click="changeLayout">
  295. {{ t('searchDemo.button') }} {{ t('searchDemo.position') }}
  296. </BaseButton>
  297. <BaseButton @click="changePosition('left')">
  298. {{ t('searchDemo.bottom') }} {{ t('searchDemo.position') }}-{{ t('searchDemo.left') }}
  299. </BaseButton>
  300. <BaseButton @click="changePosition('center')">
  301. {{ t('searchDemo.bottom') }} {{ t('searchDemo.position') }}-{{ t('searchDemo.center') }}
  302. </BaseButton>
  303. <BaseButton @click="changePosition('right')">
  304. {{ t('searchDemo.bottom') }} {{ t('searchDemo.position') }}-{{ t('searchDemo.right') }}
  305. </BaseButton>
  306. <BaseButton @click="getDictOne">
  307. {{ t('formDemo.select') }} {{ t('searchDemo.dynamicOptions') }}
  308. </BaseButton>
  309. <BaseButton @click="delRadio">{{ t('searchDemo.deleteRadio') }}</BaseButton>
  310. <BaseButton @click="restoreRadio">{{ t('searchDemo.restoreRadio') }}</BaseButton>
  311. <BaseButton @click="setValue">{{ t('formDemo.setValue') }}</BaseButton>
  312. <BaseButton @click="changeSearchLoading">
  313. {{ t('searchDemo.search') }} {{ t('searchDemo.loading') }}
  314. </BaseButton>
  315. <BaseButton @click="changeResetLoading">
  316. {{ t('searchDemo.reset') }} {{ t('searchDemo.loading') }}
  317. </BaseButton>
  318. </ContentWrap>
  319. <ContentWrap :title="t('searchDemo.search')" :message="t('searchDemo.searchDes')">
  320. <Search
  321. :schema="schema"
  322. :is-col="isGrid"
  323. :layout="layout"
  324. :button-position="buttonPosition"
  325. :search-loading="searchLoading"
  326. :reset-loading="resetLoading"
  327. show-expand
  328. expand-field="field6"
  329. @search="handleSearch"
  330. @reset="handleSearch"
  331. @register="searchRegister"
  332. />
  333. </ContentWrap>
  334. </template>
  335. <style lang="less" scoped>
  336. .el-button {
  337. margin-top: 10px;
  338. }
  339. </style>