Dialog.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 } from 'vue'
  7. import { Form, FormSchema } from '@/components/Form'
  8. import { useValidator } from '@/hooks/web/useValidator'
  9. import { getDictOneApi } from '@/api/common'
  10. import { useForm } from '@/hooks/web/useForm'
  11. const { required } = useValidator()
  12. const { t } = useI18n()
  13. const dialogVisible = ref(false)
  14. const dialogVisible2 = ref(false)
  15. const { formRegister, formMethods } = useForm()
  16. const { getElFormExpose } = formMethods
  17. const schema = reactive<FormSchema[]>([
  18. {
  19. field: 'field1',
  20. label: t('formDemo.input'),
  21. component: 'Input',
  22. formItemProps: {
  23. rules: [required()]
  24. }
  25. },
  26. {
  27. field: 'field2',
  28. label: t('formDemo.select'),
  29. component: 'Select',
  30. // componentProps: {
  31. // options: []
  32. // },
  33. optionApi: async () => {
  34. const res = await getDictOneApi()
  35. return res.data
  36. }
  37. },
  38. {
  39. field: 'field3',
  40. label: t('formDemo.radio'),
  41. component: 'RadioGroup',
  42. componentProps: {
  43. options: [
  44. {
  45. label: 'option-1',
  46. value: '1'
  47. },
  48. {
  49. label: 'option-2',
  50. value: '2'
  51. }
  52. ]
  53. }
  54. },
  55. {
  56. field: 'field4',
  57. label: t('formDemo.checkbox'),
  58. component: 'CheckboxGroup',
  59. value: [],
  60. componentProps: {
  61. options: [
  62. {
  63. label: 'option-1',
  64. value: '1'
  65. },
  66. {
  67. label: 'option-2',
  68. value: '2'
  69. }
  70. ]
  71. }
  72. },
  73. {
  74. field: 'field5',
  75. component: 'DatePicker',
  76. label: t('formDemo.datePicker'),
  77. componentProps: {
  78. type: 'date'
  79. }
  80. },
  81. {
  82. field: 'field6',
  83. component: 'TimeSelect',
  84. label: t('formDemo.timeSelect')
  85. }
  86. ])
  87. const formSubmit = async () => {
  88. const elFormExpose = await getElFormExpose()
  89. elFormExpose?.validate((valid) => {
  90. if (valid) {
  91. console.log('submit success')
  92. } else {
  93. console.log('submit fail')
  94. }
  95. })
  96. }
  97. </script>
  98. <template>
  99. <ContentWrap :title="t('dialogDemo.dialog')" :message="t('dialogDemo.dialogDes')">
  100. <ElButton type="primary" @click="dialogVisible = !dialogVisible">
  101. {{ t('dialogDemo.open') }}
  102. </ElButton>
  103. <ElButton type="primary" @click="dialogVisible2 = !dialogVisible2">
  104. {{ t('dialogDemo.combineWithForm') }}
  105. </ElButton>
  106. <Dialog v-model="dialogVisible" :title="t('dialogDemo.dialog')">
  107. <div v-for="v in 10000" :key="v">{{ v }}</div>
  108. <template #footer>
  109. <ElButton @click="dialogVisible = false">{{ t('dialogDemo.close') }}</ElButton>
  110. </template>
  111. </Dialog>
  112. <Dialog v-model="dialogVisible2" :title="t('dialogDemo.dialog')">
  113. <Form :schema="schema" @register="formRegister" />
  114. <template #footer>
  115. <ElButton type="primary" @click="formSubmit">{{ t('dialogDemo.submit') }}</ElButton>
  116. <ElButton @click="dialogVisible2 = false">{{ t('dialogDemo.close') }}</ElButton>
  117. </template>
  118. </Dialog>
  119. </ContentWrap>
  120. </template>