mixins.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { isObject } from '@/utils/types.js'
  2. export default {
  3. inheritAttrs: false,
  4. props: {
  5. // v-model/value 绑定值
  6. value: {
  7. default: null
  8. },
  9. // 字段信息
  10. field: {
  11. type: Object,
  12. required: true
  13. },
  14. // 字段展示名(不传则取默认值)
  15. label: {
  16. type: String,
  17. default: null
  18. },
  19. // 提示信息(不传则取默认值)
  20. placeholder: {
  21. type: String,
  22. default: null
  23. },
  24. // 是否禁止编辑
  25. disabled: {
  26. type: Boolean,
  27. default: false
  28. },
  29. // 私有配置参数
  30. config: {
  31. type: Object,
  32. default: null
  33. },
  34. index: {
  35. type: Number,
  36. default: 0
  37. }
  38. },
  39. data() {
  40. return {
  41. formValue: null
  42. }
  43. },
  44. mounted() {
  45. if (this.field.defaultValue && this.$isEmpty(this.formValue)) {
  46. // this.$emit('input', this.field.defaultValue)
  47. this.emitChangeEvt(this.formatDefaultValue())
  48. }
  49. console.log(this.field, 'this.field', true)
  50. },
  51. computed: {
  52. // 输入提示
  53. _placeholder() {
  54. if (this.field.autoGeneNumber == 1) return '根据编号规则自动生成,支持手动输入'
  55. return this.$isEmpty(this.placeholder) ? `请输入${this.field.name}` : this.placeholder
  56. },
  57. // 字段展示名
  58. _label() {
  59. if (this.label !== null) return this.label
  60. return this.field.name
  61. }
  62. },
  63. watch: {
  64. value: {
  65. handler(val) {
  66. // console.log('change value: ', val)
  67. this.formValue = this.value
  68. },
  69. deep: true,
  70. immediate: true
  71. },
  72. // #ifdef MP-WEIXIN
  73. // 小程序需要在这里重新赋值
  74. field: {
  75. handler() {
  76. this.formValue = this.field.value
  77. },
  78. deep: true,
  79. immediate: true
  80. }
  81. // #endif
  82. },
  83. methods: {
  84. emitChangeEvt(val) {
  85. this.$emit('input', val)
  86. this.$emit('change', {
  87. index: this.index,
  88. field: this.field,
  89. value: val
  90. })
  91. },
  92. /**
  93. * 格式化默认值
  94. */
  95. formatDefaultValue() {
  96. const defaultValue = this.field.defaultValue
  97. switch (this.field.formType) {
  98. case 'select':
  99. return this.getSelectDefaultVal(defaultValue)
  100. case 'checkbox':
  101. return this.getSelectDefaultVal(defaultValue)
  102. case 'detail_table':
  103. return [defaultValue]
  104. }
  105. return defaultValue
  106. },
  107. /**
  108. * 获取选项类型的默认值
  109. * @param {Object} data
  110. */
  111. getSelectDefaultVal(data) {
  112. return data.map(o => {
  113. if (isObject(o)) return o
  114. const setting = this.field.setting
  115. if (setting.includes('其他') && !setting.includes(o)) {
  116. return {
  117. label: '其他',
  118. value: o
  119. }
  120. }
  121. return {
  122. label: o,
  123. value: o
  124. }
  125. })
  126. }
  127. }
  128. }