Descriptions.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <script setup lang="ts">
  2. import { Descriptions } from '@/components/Descriptions'
  3. import { useI18n } from '@/hooks/web/useI18n'
  4. import { reactive, unref } from 'vue'
  5. import { Form } from '@/components/Form'
  6. import { ElFormItem, ElInput, ElButton } from 'element-plus'
  7. import { useValidator } from '@/hooks/web/useValidator'
  8. import { useForm } from '@/hooks/web/useForm'
  9. import { DescriptionsSchema } from '@/types/descriptions'
  10. const { required } = useValidator()
  11. const { t } = useI18n()
  12. const data = reactive({
  13. username: 'chenkl',
  14. nickName: '梦似花落。',
  15. age: 26,
  16. phone: '13655971xxxx',
  17. email: '502431556@qq.com',
  18. addr: '这是一个很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长的地址',
  19. sex: '男',
  20. certy: '3505831994xxxxxxxx'
  21. })
  22. const schema = reactive<DescriptionsSchema[]>([
  23. {
  24. field: 'username',
  25. label: t('descriptionsDemo.username')
  26. },
  27. {
  28. field: 'nickName',
  29. label: t('descriptionsDemo.nickName')
  30. },
  31. {
  32. field: 'phone',
  33. label: t('descriptionsDemo.phone')
  34. },
  35. {
  36. field: 'email',
  37. label: t('descriptionsDemo.email')
  38. },
  39. {
  40. field: 'addr',
  41. label: t('descriptionsDemo.addr'),
  42. span: 24
  43. }
  44. ])
  45. const form = reactive({
  46. username: '',
  47. nickName: '',
  48. phone: '',
  49. email: '',
  50. addr: ''
  51. })
  52. const rules = reactive({
  53. username: [required()],
  54. nickName: [required()],
  55. phone: [required()],
  56. email: [required()],
  57. addr: [required()]
  58. })
  59. const { register, elFormRef } = useForm()
  60. const formValidation = () => {
  61. unref(elFormRef)!.validate((isValid) => {
  62. console.log(isValid)
  63. })
  64. }
  65. </script>
  66. <template>
  67. <Descriptions
  68. :title="t('descriptionsDemo.descriptions')"
  69. :message="t('descriptionsDemo.descriptionsDes')"
  70. :data="data"
  71. :schema="schema"
  72. />
  73. <Form is-custom :model="form" :rules="rules" @register="register">
  74. <Descriptions :title="t('descriptionsDemo.form')" :data="data" :schema="schema" class="mt-20px">
  75. <template #username-label="scope">
  76. <span class="is-required--item">{{ scope.label }}</span>
  77. </template>
  78. <template #nickName-label="scope">
  79. <span class="is-required--item">{{ scope.label }}</span>
  80. </template>
  81. <template #phone-label="scope">
  82. <span class="is-required--item">{{ scope.label }}</span>
  83. </template>
  84. <template #email-label="scope">
  85. <span class="is-required--item">{{ scope.label }}</span>
  86. </template>
  87. <template #addr-label="scope">
  88. <span class="is-required--item">{{ scope.label }}</span>
  89. </template>
  90. <template #username>
  91. <ElFormItem prop="username">
  92. <ElInput v-model="form.username" />
  93. </ElFormItem>
  94. </template>
  95. <template #nickName>
  96. <ElFormItem prop="nickName">
  97. <ElInput v-model="form.nickName" />
  98. </ElFormItem>
  99. </template>
  100. <template #phone>
  101. <ElFormItem prop="phone">
  102. <ElInput v-model="form.phone" />
  103. </ElFormItem>
  104. </template>
  105. <template #email>
  106. <ElFormItem prop="email">
  107. <ElInput v-model="form.email" />
  108. </ElFormItem>
  109. </template>
  110. <template #addr>
  111. <ElFormItem prop="addr">
  112. <ElInput v-model="form.addr" />
  113. </ElFormItem>
  114. </template>
  115. </Descriptions>
  116. <div class="text-center mt-10px">
  117. <ElButton @click="formValidation"> {{ t('formDemo.formValidation') }} </ElButton>
  118. </div>
  119. </Form>
  120. </template>
  121. <style lang="less" scoped>
  122. .is-required--item {
  123. position: relative;
  124. &::before {
  125. margin-right: 4px;
  126. color: var(--el-color-danger);
  127. content: '*';
  128. }
  129. }
  130. </style>