Descriptions.vue 4.6 KB

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