Dialog.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <script setup lang="ts">
  2. import { ContentWrap } from '@/components/ContentWrap'
  3. import { Dialog } from '@/components/Dialog'
  4. import { ElButton } from 'element-plus'
  5. import { useI18n } from '@/hooks/web/useI18n'
  6. import { ref, reactive, unref } from 'vue'
  7. import { Form, FormExpose } from '@/components/Form'
  8. import { useValidator } from '@/hooks/web/useValidator'
  9. import { getDictOneApi } from '@/api/common'
  10. import { FormSchema } from '@/types/form'
  11. const { required } = useValidator()
  12. const { t } = useI18n()
  13. const dialogVisible = ref(false)
  14. const dialogVisible2 = ref(false)
  15. const schema = reactive<FormSchema[]>([
  16. {
  17. field: 'field1',
  18. label: t('formDemo.input'),
  19. component: 'Input',
  20. formItemProps: {
  21. rules: [required()]
  22. }
  23. },
  24. {
  25. field: 'field2',
  26. label: t('formDemo.select'),
  27. component: 'Select',
  28. componentProps: {
  29. options: [
  30. {
  31. label: 'option1',
  32. value: '1'
  33. },
  34. {
  35. label: 'option2',
  36. value: '2'
  37. }
  38. ]
  39. }
  40. },
  41. {
  42. field: 'field3',
  43. label: t('formDemo.radio'),
  44. component: 'Radio',
  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: 'Checkbox',
  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 getDictOne = async () => {
  95. const res = await getDictOneApi()
  96. if (res) {
  97. schema[1].componentProps!.options = res.data
  98. }
  99. }
  100. getDictOne()
  101. const formRef = ref<FormExpose>()
  102. const formSubmit = () => {
  103. unref(formRef)
  104. ?.getElFormRef()
  105. ?.validate((valid) => {
  106. if (valid) {
  107. console.log('submit success')
  108. } else {
  109. console.log('submit fail')
  110. }
  111. })
  112. }
  113. </script>
  114. <template>
  115. <ContentWrap :title="t('dialogDemo.dialog')" :message="t('dialogDemo.dialogDes')">
  116. <ElButton type="primary" @click="dialogVisible = !dialogVisible">
  117. {{ t('dialogDemo.open') }}
  118. </ElButton>
  119. <ElButton type="primary" @click="dialogVisible2 = !dialogVisible2">
  120. {{ t('dialogDemo.combineWithForm') }}
  121. </ElButton>
  122. <Dialog v-model="dialogVisible" :title="t('dialogDemo.dialog')">
  123. <div v-for="v in 10000" :key="v">{{ v }}</div>
  124. <template #footer>
  125. <ElButton @click="dialogVisible = false">{{ t('dialogDemo.close') }}</ElButton>
  126. </template>
  127. </Dialog>
  128. <Dialog v-model="dialogVisible2" :title="t('dialogDemo.dialog')">
  129. <Form ref="formRef" :schema="schema" />
  130. <template #footer>
  131. <ElButton type="primary" @click="formSubmit">{{ t('dialogDemo.submit') }}</ElButton>
  132. <ElButton @click="dialogVisible2 = false">{{ t('dialogDemo.close') }}</ElButton>
  133. </template>
  134. </Dialog>
  135. </ContentWrap>
  136. </template>