UseFormDemo.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <script setup lang="ts">
  2. import { Form } 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 } from 'element-plus'
  8. import { required } from '@/utils/formRules'
  9. const { t } = useI18n()
  10. const schema = reactive<FormSchema[]>([
  11. {
  12. field: 'field1',
  13. label: t('formDemo.input'),
  14. component: 'Input',
  15. formItemProps: {
  16. rules: [required]
  17. }
  18. },
  19. {
  20. field: 'field2',
  21. label: t('formDemo.select'),
  22. component: 'Select',
  23. componentProps: {
  24. options: [
  25. {
  26. label: 'option1',
  27. value: '1'
  28. },
  29. {
  30. label: 'option2',
  31. value: '2'
  32. }
  33. ]
  34. }
  35. },
  36. {
  37. field: 'field3',
  38. label: t('formDemo.radio'),
  39. component: 'Radio',
  40. componentProps: {
  41. options: [
  42. {
  43. label: 'option-1',
  44. value: '1'
  45. },
  46. {
  47. label: 'option-2',
  48. value: '2'
  49. }
  50. ]
  51. }
  52. },
  53. {
  54. field: 'field4',
  55. label: t('formDemo.checkbox'),
  56. component: 'Checkbox',
  57. value: [],
  58. componentProps: {
  59. options: [
  60. {
  61. label: 'option-1',
  62. value: '1'
  63. },
  64. {
  65. label: 'option-2',
  66. value: '2'
  67. },
  68. {
  69. label: 'option-3',
  70. value: '3'
  71. }
  72. ]
  73. }
  74. },
  75. {
  76. field: 'field5',
  77. component: 'DatePicker',
  78. label: t('formDemo.datePicker'),
  79. componentProps: {
  80. type: 'date'
  81. }
  82. },
  83. {
  84. field: 'field6',
  85. component: 'TimeSelect',
  86. label: t('formDemo.timeSelect')
  87. }
  88. ])
  89. const { register, methods, elFormRef } = useForm()
  90. const changeLabelWidth = (width: number | string) => {
  91. const { setProps } = methods
  92. setProps({
  93. labelWidth: width
  94. })
  95. }
  96. const changeSize = (size: string) => {
  97. const { setProps } = methods
  98. setProps({
  99. size
  100. })
  101. }
  102. const changeDisabled = (bool: boolean) => {
  103. const { setProps } = methods
  104. setProps({
  105. disabled: bool
  106. })
  107. }
  108. const changeSchema = (del: boolean) => {
  109. const { delSchema, addSchema } = methods
  110. if (del) {
  111. delSchema('field2')
  112. } else if (!del && schema[1].field !== 'field2') {
  113. addSchema(
  114. {
  115. field: 'field2',
  116. label: t('formDemo.select'),
  117. component: 'Select',
  118. componentProps: {
  119. options: [
  120. {
  121. label: 'option1',
  122. value: '1'
  123. },
  124. {
  125. label: 'option2',
  126. value: '2'
  127. }
  128. ]
  129. }
  130. },
  131. 1
  132. )
  133. }
  134. }
  135. const setValue = (reset: boolean) => {
  136. const { setValues } = methods
  137. if (reset) {
  138. unref(elFormRef)?.resetFields()
  139. } else {
  140. setValues({
  141. field1: 'field1',
  142. field2: '2',
  143. field3: '2',
  144. field4: ['1', '3'],
  145. field5: '2022-01-27',
  146. field6: '17:00'
  147. })
  148. }
  149. }
  150. const index = ref(7)
  151. const setLabel = () => {
  152. const { setSchema } = methods
  153. setSchema([
  154. {
  155. field: 'field2',
  156. path: 'label',
  157. value: `${t('formDemo.select')} ${index.value}`
  158. },
  159. {
  160. field: 'field2',
  161. path: 'componentProps.options',
  162. value: [
  163. {
  164. label: 'option-1',
  165. value: '1'
  166. },
  167. {
  168. label: 'option-2',
  169. value: '2'
  170. },
  171. {
  172. label: 'option-3',
  173. value: '3'
  174. }
  175. ]
  176. }
  177. ])
  178. index.value++
  179. }
  180. const addItem = () => {
  181. const { addSchema } = methods
  182. if (unref(index) % 2 === 0) {
  183. addSchema({
  184. field: `field${unref(index)}`,
  185. label: `${t('formDemo.input')}${unref(index)}`,
  186. component: 'Input'
  187. })
  188. } else {
  189. addSchema(
  190. {
  191. field: `field${unref(index)}`,
  192. label: `${t('formDemo.input')}${unref(index)}`,
  193. component: 'Input'
  194. },
  195. unref(index)
  196. )
  197. }
  198. index.value++
  199. }
  200. const formValidation = () => {
  201. unref(elFormRef)
  202. ?.validate()
  203. ?.catch(() => {})
  204. }
  205. const verifyReset = () => {
  206. unref(elFormRef)?.resetFields()
  207. }
  208. </script>
  209. <template>
  210. <ContentWrap :title="`useForm${t('formDemo.operate')}`">
  211. <ElButton @click="changeLabelWidth(150)">{{ t('formDemo.change') }} labelWidth</ElButton>
  212. <ElButton @click="changeLabelWidth('auto')">{{ t('formDemo.restore') }} labelWidth</ElButton>
  213. <ElButton @click="changeSize('large')">{{ t('formDemo.change') }} size</ElButton>
  214. <ElButton @click="changeSize('default')">{{ t('formDemo.restore') }} size</ElButton>
  215. <ElButton @click="changeDisabled(true)">{{ t('formDemo.disabled') }}</ElButton>
  216. <ElButton @click="changeDisabled(false)">{{ t('formDemo.disablement') }}</ElButton>
  217. <ElButton @click="changeSchema(true)">
  218. {{ t('formDemo.delete') }} {{ t('formDemo.select') }}
  219. </ElButton>
  220. <ElButton @click="changeSchema(false)">
  221. {{ t('formDemo.add') }} {{ t('formDemo.select') }}
  222. </ElButton>
  223. <ElButton @click="setValue(false)">{{ t('formDemo.setValue') }}</ElButton>
  224. <ElButton @click="setValue(true)">{{ t('formDemo.resetValue') }}</ElButton>
  225. <ElButton @click="setLabel">
  226. {{ t('formDemo.set') }} {{ t('formDemo.select') }} label
  227. </ElButton>
  228. <ElButton @click="addItem"> {{ t('formDemo.add') }} {{ t('formDemo.subitem') }} </ElButton>
  229. <ElButton @click="formValidation"> {{ t('formDemo.formValidation') }} </ElButton>
  230. <ElButton @click="verifyReset"> {{ t('formDemo.verifyReset') }} </ElButton>
  231. </ContentWrap>
  232. <ContentWrap :title="`useForm${t('formDemo.example')}`">
  233. <Form :schema="schema" @register="register" />
  234. </ContentWrap>
  235. </template>