mock-data.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { defineComponent, ref, toRef } from 'vue'
  2. import Input from '@element-plus/components/input'
  3. import Button from '@element-plus/components/button'
  4. import Form from '../src/form.vue'
  5. import FormItem from '../src/form-item.vue'
  6. interface DomainItem {
  7. key: number
  8. value: string
  9. }
  10. const DynamicDomainForm = defineComponent({
  11. props: {
  12. onSuccess: Function,
  13. onError: Function,
  14. onSubmit: Function,
  15. model: Object,
  16. },
  17. setup(props, { slots }) {
  18. const propsModel = toRef(props, 'model')
  19. const model = ref({
  20. domains: [
  21. {
  22. key: 1,
  23. value: '',
  24. },
  25. ],
  26. })
  27. const formRef = ref<InstanceType<typeof Form>>()
  28. const removeDomain = (item: DomainItem) => {
  29. const index = model.value.domains.indexOf(item)
  30. if (index !== -1) {
  31. model.value.domains.splice(index, 1)
  32. }
  33. }
  34. const addDomain = () => {
  35. model.value.domains.push({
  36. key: Date.now(),
  37. value: '',
  38. })
  39. }
  40. const submitForm = async () => {
  41. if (!formRef.value) return
  42. try {
  43. const validate = props.onSubmit
  44. ? formRef.value.validate(props.onSubmit as any)
  45. : formRef.value.validate()
  46. await validate
  47. props.onSuccess?.()
  48. } catch (e) {
  49. props.onError?.(e)
  50. }
  51. }
  52. return () => (
  53. <Form
  54. ref={formRef}
  55. model={{ ...model.value, ...(propsModel.value || {}) }}
  56. >
  57. {model.value.domains.map((domain, index) => {
  58. return (
  59. <FormItem
  60. class="domain-item"
  61. key={domain.key}
  62. label={`Domain${index}`}
  63. prop={`domains.${index}.value`}
  64. rules={{
  65. required: true,
  66. message: 'domain can not be null',
  67. trigger: 'blur',
  68. }}
  69. >
  70. <Input v-model={domain.value} />
  71. <Button
  72. class={`delete-domain ${index}`}
  73. onClick={(e) => {
  74. e.preventDefault()
  75. removeDomain(domain)
  76. }}
  77. >
  78. Delete
  79. </Button>
  80. </FormItem>
  81. )
  82. })}
  83. {slots.default?.()}
  84. <FormItem>
  85. <Button class="submit" type="primary" onClick={submitForm}>
  86. Submit
  87. </Button>
  88. <Button class="add-domain" onClick={addDomain}>
  89. New domain
  90. </Button>
  91. </FormItem>
  92. </Form>
  93. )
  94. },
  95. })
  96. export default DynamicDomainForm
  97. export const formatDomainError = (count: number) => {
  98. return Array.from({ length: count }).reduce((prev: any, _, idx) => {
  99. const key = `domains.${idx}.value`
  100. return {
  101. ...prev,
  102. [key]: [
  103. {
  104. field: key,
  105. fieldValue: '',
  106. message: 'domain can not be null',
  107. },
  108. ],
  109. }
  110. }, {})
  111. }