UseFormDemo.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 } 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 schema = reactive<FormSchema[]>([
  13. {
  14. field: 'field1',
  15. label: t('formDemo.input'),
  16. component: 'Input',
  17. formItemProps: {
  18. rules: [required()]
  19. }
  20. },
  21. {
  22. field: 'field2',
  23. label: t('formDemo.select'),
  24. component: 'Select',
  25. componentProps: {
  26. options: [
  27. {
  28. label: 'option1',
  29. value: '1'
  30. },
  31. {
  32. label: 'option2',
  33. value: '2'
  34. }
  35. ]
  36. },
  37. formItemProps: {
  38. rules: [required()]
  39. }
  40. },
  41. {
  42. field: 'field3',
  43. label: t('formDemo.radio'),
  44. component: 'RadioGroup',
  45. componentProps: {
  46. options: [
  47. {
  48. label: 'option-1',
  49. value: '1'
  50. },
  51. {
  52. label: 'option-2',
  53. value: '2'
  54. }
  55. ]
  56. }
  57. },
  58. {
  59. field: 'field4',
  60. label: t('formDemo.checkbox'),
  61. component: 'CheckboxGroup',
  62. value: [],
  63. componentProps: {
  64. options: [
  65. {
  66. label: 'option-1',
  67. value: '1'
  68. },
  69. {
  70. label: 'option-2',
  71. value: '2'
  72. },
  73. {
  74. label: 'option-3',
  75. value: '3'
  76. }
  77. ]
  78. }
  79. },
  80. {
  81. field: 'field5',
  82. component: 'DatePicker',
  83. label: t('formDemo.datePicker'),
  84. componentProps: {
  85. type: 'date'
  86. }
  87. },
  88. {
  89. field: 'field6',
  90. component: 'TimeSelect',
  91. label: t('formDemo.timeSelect')
  92. }
  93. ])
  94. const { register, methods } = useForm()
  95. const {
  96. setProps,
  97. delSchema,
  98. addSchema,
  99. setValues,
  100. setSchema,
  101. getComponentExpose,
  102. getFormItemExpose,
  103. getElFormExpose
  104. } = methods
  105. const changeLabelWidth = (width: number | string) => {
  106. setProps({
  107. labelWidth: width
  108. })
  109. }
  110. const changeSize = (size: string) => {
  111. setProps({
  112. size
  113. })
  114. }
  115. const changeDisabled = (bool: boolean) => {
  116. setProps({
  117. disabled: bool
  118. })
  119. }
  120. const changeSchema = (del: boolean) => {
  121. if (del) {
  122. delSchema('field2')
  123. } else if (!del && schema[1].field !== 'field2') {
  124. addSchema(
  125. {
  126. field: 'field2',
  127. label: t('formDemo.select'),
  128. component: 'Select',
  129. componentProps: {
  130. options: [
  131. {
  132. label: 'option1',
  133. value: '1'
  134. },
  135. {
  136. label: 'option2',
  137. value: '2'
  138. }
  139. ]
  140. }
  141. },
  142. 1
  143. )
  144. }
  145. }
  146. const setValue = async (reset: boolean) => {
  147. const elFormExpose = await getElFormExpose()
  148. if (reset) {
  149. elFormExpose?.resetFields()
  150. } else {
  151. setValues({
  152. field1: 'field1',
  153. field2: '2',
  154. field3: '2',
  155. field4: ['1', '3'],
  156. field5: '2022-01-27',
  157. field6: '17:00'
  158. })
  159. }
  160. }
  161. const index = ref(7)
  162. const setLabel = () => {
  163. setSchema([
  164. {
  165. field: 'field2',
  166. path: 'label',
  167. value: `${t('formDemo.select')} ${index.value}`
  168. },
  169. {
  170. field: 'field2',
  171. path: 'componentProps.options',
  172. value: [
  173. {
  174. label: 'option-1',
  175. value: '1'
  176. },
  177. {
  178. label: 'option-2',
  179. value: '2'
  180. },
  181. {
  182. label: 'option-3',
  183. value: '3'
  184. }
  185. ]
  186. }
  187. ])
  188. index.value++
  189. }
  190. const addItem = () => {
  191. const { addSchema } = methods
  192. if (unref(index) % 2 === 0) {
  193. addSchema({
  194. field: `field${unref(index)}`,
  195. label: `${t('formDemo.input')}${unref(index)}`,
  196. component: 'Input'
  197. })
  198. } else {
  199. addSchema(
  200. {
  201. field: `field${unref(index)}`,
  202. label: `${t('formDemo.input')}${unref(index)}`,
  203. component: 'Input'
  204. },
  205. unref(index)
  206. )
  207. }
  208. index.value++
  209. }
  210. const formValidation = async () => {
  211. const elFormExpose = await getElFormExpose()
  212. elFormExpose?.validate((isValid) => {
  213. console.log(isValid)
  214. })
  215. }
  216. const verifyReset = async () => {
  217. const elFormExpose = await getElFormExpose()
  218. elFormExpose?.resetFields()
  219. }
  220. const getDictOne = async () => {
  221. const res = await getDictOneApi()
  222. if (res) {
  223. const { setSchema } = methods
  224. setSchema([
  225. {
  226. field: 'field2',
  227. path: 'componentProps.options',
  228. value: res.data
  229. }
  230. ])
  231. }
  232. }
  233. const inoutFocus = async () => {
  234. const inputEl: ComponentRef<typeof ElInput> = await getComponentExpose('field1')
  235. inputEl?.focus()
  236. }
  237. const inoutValidation = async () => {
  238. const formItem = await getFormItemExpose('field1')
  239. const inputEl: ComponentRef<typeof ElInput> = await getComponentExpose('field1')
  240. inputEl?.focus()
  241. formItem?.validate('focus', (val: boolean) => {
  242. console.log(val)
  243. })
  244. }
  245. </script>
  246. <template>
  247. <ContentWrap :title="`UseForm ${t('formDemo.operate')}`" style="margin-bottom: 20px">
  248. <ElButton @click="changeLabelWidth(150)">{{ t('formDemo.change') }} labelWidth</ElButton>
  249. <ElButton @click="changeLabelWidth('auto')">{{ t('formDemo.restore') }} labelWidth</ElButton>
  250. <ElButton @click="changeSize('large')">{{ t('formDemo.change') }} size</ElButton>
  251. <ElButton @click="changeSize('default')">{{ t('formDemo.restore') }} size</ElButton>
  252. <ElButton @click="changeDisabled(true)">{{ t('formDemo.disabled') }}</ElButton>
  253. <ElButton @click="changeDisabled(false)">{{ t('formDemo.disablement') }}</ElButton>
  254. <ElButton @click="changeSchema(true)">
  255. {{ t('formDemo.delete') }} {{ t('formDemo.select') }}
  256. </ElButton>
  257. <ElButton @click="changeSchema(false)">
  258. {{ t('formDemo.add') }} {{ t('formDemo.select') }}
  259. </ElButton>
  260. <ElButton @click="setValue(false)">{{ t('formDemo.setValue') }}</ElButton>
  261. <ElButton @click="setValue(true)">{{ t('formDemo.resetValue') }}</ElButton>
  262. <ElButton @click="setLabel">
  263. {{ t('formDemo.set') }} {{ t('formDemo.select') }} label
  264. </ElButton>
  265. <ElButton @click="addItem"> {{ t('formDemo.add') }} {{ t('formDemo.subitem') }} </ElButton>
  266. <ElButton @click="formValidation"> {{ t('formDemo.formValidation') }} </ElButton>
  267. <ElButton @click="verifyReset"> {{ t('formDemo.verifyReset') }} </ElButton>
  268. <ElButton @click="getDictOne">
  269. {{ t('searchDemo.dynamicOptions') }}
  270. </ElButton>
  271. <ElButton @click="inoutFocus">
  272. {{ `${t('formDemo.input')} ${t('formDemo.focus')}` }}
  273. </ElButton>
  274. <ElButton @click="inoutValidation">
  275. {{ `${t('formDemo.input')} ${t('formDemo.formValidation')}` }}
  276. </ElButton>
  277. </ContentWrap>
  278. <ContentWrap :title="`UseForm ${t('formDemo.example')}`">
  279. <Form :schema="schema" @register="register" />
  280. </ContentWrap>
  281. </template>
  282. <style lang="less" scoped>
  283. .el-button + .el-button {
  284. margin-top: 10px;
  285. }
  286. </style>