Search.vue 7.3 KB

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