index.jsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import extraParas from '../../config'
  2. import zhCN from 'ant-design-vue/es/locale/zh_CN'
  3. import styles from './index.module.scss'
  4. import DynamicForm from '../DynamicForm'
  5. import StaticForm from '../StaticForm'
  6. export default {
  7. extraParas,
  8. name: 'CustomForm',
  9. components: {
  10. DynamicForm,
  11. StaticForm
  12. },
  13. props: {
  14. value: Array
  15. },
  16. data() {
  17. return {
  18. form: {},
  19. labelCol: { span: 4 },
  20. wrapperCol: { span: 14 },
  21. }
  22. },
  23. methods: {
  24. async submit() {
  25. return new Promise((reslove, reject) => {
  26. this.$refs.dynamic.$refs.form.validate(valid => {
  27. if (valid) {
  28. reslove({...this.form})
  29. } else {
  30. reject('表单校验失败')
  31. }
  32. })
  33. })
  34. },
  35. clearValidate() {
  36. this.$refs.dynamic.$refs.form.clearValidate()
  37. }
  38. },
  39. mounted() {
  40. const configured = this.value || []
  41. const configuredForm = configured.reduce((form, cur) => {
  42. form[cur.name] = cur.defaultValue
  43. return form
  44. }, {})
  45. extraParas.forEach(({ name, defaultValue, config = {} }) => {
  46. let val = (name in configuredForm) ? configuredForm[name] : defaultValue
  47. if (config.type === 'MultiSelect' || config.type === 'CheckBox') {
  48. let selected = val
  49. if (typeof selected === 'string') {
  50. try {
  51. selected = JSON.parse(val)
  52. } catch(e) {
  53. selected = []
  54. }
  55. }
  56. this.$set(this.form, name, selected)
  57. } else {
  58. this.$set(this.form, name, val)
  59. }
  60. })
  61. },
  62. render() {
  63. return (
  64. <a-config-provider locale={zhCN}>
  65. <div class={styles.customForm}>
  66. {/* 动态表单 */}
  67. <DynamicForm paras={extraParas} form={this.form} ref="dynamic" />
  68. {/* 静态表单,可手动定义表单样式 */}
  69. {/* <StaticForm form={this.form} ref="form" /> */}
  70. </div>
  71. </a-config-provider>
  72. )
  73. }
  74. }