StdFormItem.vue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <script setup lang="ts">
  2. import type { Rule } from 'ant-design-vue/es/form'
  3. import type { Column } from '@/components/StdDesign/types'
  4. import FormErrors from '@/constants/form_errors'
  5. const props = defineProps<Props>()
  6. export interface Props {
  7. dataIndex?: Column['dataIndex']
  8. label?: string
  9. extra?: string
  10. hint?: string | (() => string)
  11. error?: {
  12. [key: string]: string
  13. }
  14. required?: boolean
  15. noValidate?: boolean
  16. }
  17. const tag = computed(() => {
  18. return props.error?.[props.dataIndex!.toString()] ?? ''
  19. })
  20. const help = computed(() => {
  21. const rules = tag.value.split(',')
  22. for (const rule of rules) {
  23. if (FormErrors[rule])
  24. return FormErrors[rule]()
  25. }
  26. return props.hint
  27. })
  28. // eslint-disable-next-line ts/no-explicit-any
  29. async function validator(_: Rule, value: any): Promise<any> {
  30. return new Promise((resolve, reject) => {
  31. if (props.required && !props.noValidate && (!value && value !== 0)) {
  32. reject(help.value ?? $gettext('This field should not be empty'))
  33. return
  34. }
  35. resolve(true)
  36. })
  37. }
  38. </script>
  39. <template>
  40. <AFormItem
  41. :name="dataIndex as string"
  42. :label="label"
  43. :help="help"
  44. :rules="{ required, validator }"
  45. :validate-status="tag ? 'error' : undefined"
  46. :auto-link="false"
  47. >
  48. <slot />
  49. </AFormItem>
  50. </template>
  51. <style scoped lang="less">
  52. </style>