UseFormDemo.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <script setup lang="ts">
  2. import { Form, FormSchema } from '@/components/Form'
  3. import { ContentWrap } from '@/components/ContentWrap'
  4. import { useI18n } from '@/hooks/web/useI18n'
  5. import { useForm } from '@/hooks/web/useForm'
  6. import { reactive, unref, ref } from 'vue'
  7. import { ElButton, ElInput, FormItemProp, ComponentSize } from 'element-plus'
  8. import { useValidator } from '@/hooks/web/useValidator'
  9. import { getDictOneApi } from '@/api/common'
  10. const { required } = useValidator()
  11. const { t } = useI18n()
  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. formItemProps: {
  96. rules: [required()]
  97. }
  98. },
  99. {
  100. field: 'field2',
  101. label: t('formDemo.select'),
  102. component: 'Select',
  103. componentProps: {
  104. options: [
  105. {
  106. label: 'option1',
  107. value: '1'
  108. },
  109. {
  110. label: 'option2',
  111. value: '2'
  112. }
  113. ]
  114. },
  115. formItemProps: {
  116. rules: [required()]
  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: 'field4',
  138. label: t('formDemo.checkbox'),
  139. component: 'CheckboxGroup',
  140. value: [],
  141. componentProps: {
  142. options: [
  143. {
  144. label: 'option-1',
  145. value: '1'
  146. },
  147. {
  148. label: 'option-2',
  149. value: '2'
  150. },
  151. {
  152. label: 'option-3',
  153. value: '3'
  154. }
  155. ]
  156. }
  157. },
  158. {
  159. field: 'field5',
  160. component: 'DatePicker',
  161. label: t('formDemo.datePicker'),
  162. componentProps: {
  163. type: 'date'
  164. }
  165. },
  166. {
  167. field: 'field6',
  168. component: 'TimeSelect',
  169. label: t('formDemo.timeSelect')
  170. },
  171. {
  172. field: 'field7',
  173. label: `${t('formDemo.treeSelect')}`,
  174. component: 'TreeSelect',
  175. // 远程加载option
  176. optionApi: async () => {
  177. const res = await getTreeSelectData()
  178. return res
  179. }
  180. }
  181. ])
  182. const { formRegister, formMethods } = useForm()
  183. const {
  184. setProps,
  185. delSchema,
  186. addSchema,
  187. setValues,
  188. setSchema,
  189. getComponentExpose,
  190. getFormItemExpose,
  191. getElFormExpose
  192. } = formMethods
  193. const changeLabelWidth = (width: number | string) => {
  194. setProps({
  195. labelWidth: width
  196. })
  197. }
  198. const changeSize = (size: ComponentSize) => {
  199. setProps({
  200. size
  201. })
  202. }
  203. const changeDisabled = (bool: boolean) => {
  204. setProps({
  205. disabled: bool
  206. })
  207. }
  208. const changeSchema = (del: boolean) => {
  209. if (del) {
  210. delSchema('field2')
  211. } else if (!del && schema[1].field !== 'field2') {
  212. addSchema(
  213. {
  214. field: 'field2',
  215. label: t('formDemo.select'),
  216. component: 'Select',
  217. componentProps: {
  218. options: [
  219. {
  220. label: 'option1',
  221. value: '1'
  222. },
  223. {
  224. label: 'option2',
  225. value: '2'
  226. }
  227. ]
  228. }
  229. },
  230. 1
  231. )
  232. }
  233. }
  234. const setValue = async (reset: boolean) => {
  235. const elFormExpose = await getElFormExpose()
  236. if (reset) {
  237. elFormExpose?.resetFields()
  238. } else {
  239. setValues({
  240. field1: 'field1',
  241. field2: '2',
  242. field3: '2',
  243. field4: ['1', '3'],
  244. field5: '2022-01-27',
  245. field6: '17:00'
  246. })
  247. }
  248. }
  249. const index = ref(7)
  250. const setLabel = () => {
  251. setSchema([
  252. {
  253. field: 'field2',
  254. path: 'label',
  255. value: `${t('formDemo.select')} ${index.value}`
  256. },
  257. {
  258. field: 'field2',
  259. path: 'componentProps.options',
  260. value: [
  261. {
  262. label: 'option-1',
  263. value: '1'
  264. },
  265. {
  266. label: 'option-2',
  267. value: '2'
  268. },
  269. {
  270. label: 'option-3',
  271. value: '3'
  272. }
  273. ]
  274. }
  275. ])
  276. index.value++
  277. }
  278. const addItem = () => {
  279. if (unref(index) % 2 === 0) {
  280. addSchema({
  281. field: `field${unref(index)}`,
  282. label: `${t('formDemo.input')}${unref(index)}`,
  283. component: 'Input'
  284. })
  285. } else {
  286. addSchema(
  287. {
  288. field: `field${unref(index)}`,
  289. label: `${t('formDemo.input')}${unref(index)}`,
  290. component: 'Input'
  291. },
  292. unref(index)
  293. )
  294. }
  295. index.value++
  296. }
  297. const formValidation = async () => {
  298. const elFormExpose = await getElFormExpose()
  299. elFormExpose?.validate((isValid) => {
  300. console.log(isValid)
  301. })
  302. }
  303. const verifyReset = async () => {
  304. const elFormExpose = await getElFormExpose()
  305. elFormExpose?.resetFields()
  306. }
  307. const getDictOne = async () => {
  308. const res = await getDictOneApi()
  309. if (res) {
  310. setSchema([
  311. {
  312. field: 'field2',
  313. path: 'componentProps.options',
  314. value: res.data
  315. }
  316. ])
  317. }
  318. }
  319. const inoutFocus = async () => {
  320. const inputEl: ComponentRef<typeof ElInput> = await getComponentExpose('field1')
  321. inputEl?.focus()
  322. }
  323. const inoutValidation = async () => {
  324. const formItem = await getFormItemExpose('field1')
  325. const inputEl: ComponentRef<typeof ElInput> = await getComponentExpose('field1')
  326. inputEl?.focus()
  327. formItem?.validate('focus', (val: boolean) => {
  328. console.log(val)
  329. })
  330. }
  331. const formValidate = (prop: FormItemProp, isValid: boolean, message: string) => {
  332. console.log(prop, isValid, message)
  333. }
  334. </script>
  335. <template>
  336. <ContentWrap :title="`UseForm ${t('formDemo.operate')}`" style="margin-bottom: 20px">
  337. <ElButton @click="changeLabelWidth(150)">{{ t('formDemo.change') }} labelWidth</ElButton>
  338. <ElButton @click="changeLabelWidth('auto')">{{ t('formDemo.restore') }} labelWidth</ElButton>
  339. <ElButton @click="changeSize('large')">{{ t('formDemo.change') }} size</ElButton>
  340. <ElButton @click="changeSize('default')">{{ t('formDemo.restore') }} size</ElButton>
  341. <ElButton @click="changeDisabled(true)">{{ t('formDemo.disabled') }}</ElButton>
  342. <ElButton @click="changeDisabled(false)">{{ t('formDemo.disablement') }}</ElButton>
  343. <ElButton @click="changeSchema(true)">
  344. {{ t('formDemo.delete') }} {{ t('formDemo.select') }}
  345. </ElButton>
  346. <ElButton @click="changeSchema(false)">
  347. {{ t('formDemo.add') }} {{ t('formDemo.select') }}
  348. </ElButton>
  349. <ElButton @click="setValue(false)">{{ t('formDemo.setValue') }}</ElButton>
  350. <ElButton @click="setValue(true)">{{ t('formDemo.resetValue') }}</ElButton>
  351. <ElButton @click="setLabel">
  352. {{ t('formDemo.set') }} {{ t('formDemo.select') }} label
  353. </ElButton>
  354. <ElButton @click="addItem"> {{ t('formDemo.add') }} {{ t('formDemo.subitem') }} </ElButton>
  355. <ElButton @click="formValidation"> {{ t('formDemo.formValidation') }} </ElButton>
  356. <ElButton @click="verifyReset"> {{ t('formDemo.verifyReset') }} </ElButton>
  357. <ElButton @click="getDictOne">
  358. {{ `${t('formDemo.select')} ${t('searchDemo.dynamicOptions')}` }}
  359. </ElButton>
  360. <ElButton @click="inoutFocus">
  361. {{ `${t('formDemo.input')} ${t('formDemo.focus')}` }}
  362. </ElButton>
  363. <ElButton @click="inoutValidation">
  364. {{ `${t('formDemo.input')} ${t('formDemo.formValidation')}` }}
  365. </ElButton>
  366. </ContentWrap>
  367. <ContentWrap :title="`UseForm ${t('formDemo.example')}`">
  368. <Form :schema="schema" @register="formRegister" @validate="formValidate" />
  369. </ContentWrap>
  370. </template>
  371. <style lang="less" scoped>
  372. .el-button {
  373. margin-top: 10px;
  374. }
  375. </style>