RefForm.vue 5.5 KB

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