Descriptions.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <script setup lang="tsx">
  2. import { Descriptions } from '@/components/Descriptions'
  3. import { useI18n } from '@/hooks/web/useI18n'
  4. import { reactive } 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 '@/components/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. }
  43. ])
  44. const schema2 = reactive<DescriptionsSchema[]>([
  45. {
  46. field: 'username',
  47. label: t('descriptionsDemo.username'),
  48. slots: {
  49. label: (row) => {
  50. return <span class="is-required--item">{row.label}</span>
  51. },
  52. default: () => {
  53. return (
  54. <ElFormItem prop="username">
  55. <ElInput v-model={form.username} />
  56. </ElFormItem>
  57. )
  58. }
  59. }
  60. },
  61. {
  62. field: 'nickName',
  63. label: t('descriptionsDemo.nickName'),
  64. slots: {
  65. label: (row) => {
  66. return <span class="is-required--item">{row.label}</span>
  67. },
  68. default: () => {
  69. return (
  70. <ElFormItem prop="nickName">
  71. <ElInput v-model={form.nickName} />
  72. </ElFormItem>
  73. )
  74. }
  75. }
  76. },
  77. {
  78. field: 'phone',
  79. label: t('descriptionsDemo.phone'),
  80. slots: {
  81. label: (row) => {
  82. return <span class="is-required--item">{row.label}</span>
  83. },
  84. default: () => {
  85. return (
  86. <ElFormItem prop="phone">
  87. <ElInput v-model={form.phone} />
  88. </ElFormItem>
  89. )
  90. }
  91. }
  92. },
  93. {
  94. field: 'email',
  95. label: t('descriptionsDemo.email'),
  96. slots: {
  97. label: (row) => {
  98. return <span class="is-required--item">{row.label}</span>
  99. },
  100. default: () => {
  101. return (
  102. <ElFormItem prop="email">
  103. <ElInput v-model={form.email} />
  104. </ElFormItem>
  105. )
  106. }
  107. }
  108. },
  109. {
  110. field: 'addr',
  111. label: t('descriptionsDemo.addr'),
  112. slots: {
  113. label: (row) => {
  114. return <span class="is-required--item">{row.label}</span>
  115. },
  116. default: () => {
  117. return (
  118. <ElFormItem prop="addr">
  119. <ElInput v-model={form.addr} />
  120. </ElFormItem>
  121. )
  122. }
  123. }
  124. }
  125. ])
  126. const form = reactive({
  127. username: '',
  128. nickName: '',
  129. phone: '',
  130. email: '',
  131. addr: ''
  132. })
  133. const rules = reactive({
  134. username: [required()],
  135. nickName: [required()],
  136. phone: [required()],
  137. email: [required()],
  138. addr: [required()]
  139. })
  140. const { formRegister, formMethods } = useForm()
  141. const { getElFormExpose } = formMethods
  142. const formValidation = async () => {
  143. const elFormExpose = await getElFormExpose()
  144. elFormExpose?.validate((isValid) => {
  145. console.log(isValid)
  146. })
  147. }
  148. </script>
  149. <template>
  150. <Descriptions
  151. :title="t('descriptionsDemo.descriptions')"
  152. :message="t('descriptionsDemo.descriptionsDes')"
  153. :data="data"
  154. :schema="schema"
  155. />
  156. <Form is-custom :model="form" :rules="rules" @register="formRegister">
  157. <Descriptions
  158. :title="t('descriptionsDemo.form')"
  159. :data="data"
  160. :schema="schema2"
  161. class="mt-20px"
  162. />
  163. <div class="text-center mt-10px">
  164. <ElButton @click="formValidation"> {{ t('formDemo.formValidation') }} </ElButton>
  165. </div>
  166. </Form>
  167. </template>
  168. <style lang="less" scoped>
  169. :deep(.is-required--item) {
  170. position: relative;
  171. &::before {
  172. margin-right: 4px;
  173. color: var(--el-color-danger);
  174. content: '*';
  175. }
  176. }
  177. </style>