index.vue 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template>
  2. <div class="wk-checkbox">
  3. <template v-if="valueIsObject">
  4. <el-select
  5. v-if="showType === 'default'"
  6. v-model="dataValue.select"
  7. :disabled="disabled"
  8. :clearable="clearable"
  9. :placeholder="placeholder"
  10. style="width: 100%;"
  11. multiple
  12. @change="valueChange">
  13. <el-option
  14. v-for="(item, index) in options"
  15. :key="index"
  16. :label="!isEmptyValue(item.value) ? item.label || item.name : item "
  17. :value="getValue(item)"/>
  18. </el-select>
  19. <el-checkbox-group
  20. v-else-if="showType === 'tiled'"
  21. v-model="dataValue.select"
  22. :disabled="disabled"
  23. @change="valueChange">
  24. <el-checkbox
  25. v-for="(item, index) in options"
  26. :key="index"
  27. :label="getValue(item)">
  28. {{ !isEmptyValue(item.value) ? item.label || item.name : item }}
  29. </el-checkbox>
  30. </el-checkbox-group>
  31. <el-input
  32. v-if="dataValue.select.includes('其他')"
  33. v-model="dataValue.otherValue"
  34. :disabled="disabled"
  35. :maxlength="100"
  36. placeholder="其他选项需填写,否则为无效选项"
  37. @blur="inputBlur"/>
  38. </template>
  39. </div>
  40. </template>
  41. <script>
  42. import { isObject, isEmpty, isArray } from '@/utils/types'
  43. import { valueEquals } from 'element-ui/lib/utils/util'
  44. import Emitter from 'element-ui/lib/mixins/emitter'
  45. export default {
  46. // 自定义字段库 多选
  47. name: 'WkCheckbox',
  48. components: {},
  49. mixins: [Emitter],
  50. props: {
  51. // eslint-disable-next-line vue/require-prop-types
  52. value: {},
  53. // 选择其他展示input输入框
  54. otherShowInput: {
  55. type: Boolean,
  56. default: false
  57. },
  58. disabled: Boolean,
  59. clearable: Boolean,
  60. placeholder: String,
  61. options: {
  62. type: Array,
  63. default: () => {
  64. return []
  65. }
  66. },
  67. showType: {
  68. type: String,
  69. default: 'default' // 下拉 default 平铺 tiled
  70. }
  71. },
  72. data() {
  73. return {
  74. dataValue: {
  75. select: [],
  76. otherValue: ''
  77. }
  78. }
  79. },
  80. computed: {
  81. valueIsObject() {
  82. return isObject(this.dataValue)
  83. }
  84. },
  85. watch: {
  86. value: {
  87. handler(newVal, oldVal) {
  88. this.validValue()
  89. },
  90. immediate: true
  91. },
  92. options: {
  93. handler() {
  94. this.validValue()
  95. },
  96. deep: true
  97. }
  98. },
  99. created() {
  100. },
  101. mounted() {},
  102. beforeDestroy() {},
  103. methods: {
  104. /**
  105. * 验证值
  106. */
  107. validValue() {
  108. if (isEmpty(this.value)) {
  109. this.dataValue = {
  110. select: [],
  111. otherValue: ''
  112. }
  113. } else {
  114. if (this.otherShowInput) {
  115. const value = isArray(this.value) ? this.value : []
  116. const otherItem = value.filter((name) => !this.options.includes(name))
  117. if (otherItem.length > 0) {
  118. const newValue = value.filter((name) => !otherItem.includes(name))
  119. newValue.push('其他')
  120. this.dataValue = {
  121. select: newValue,
  122. otherValue: otherItem[otherItem.length - 1]
  123. }
  124. } else {
  125. if (!valueEquals(value, this.dataValue.select)) {
  126. this.dataValue = {
  127. select: value,
  128. otherValue: ''
  129. }
  130. }
  131. }
  132. } else {
  133. this.dataValue = {
  134. select: this.value,
  135. otherValue: ''
  136. }
  137. }
  138. }
  139. },
  140. /**
  141. * 选项值
  142. */
  143. getValue(item) {
  144. return !this.isEmptyValue(item.value) ? item.value : item
  145. },
  146. /**
  147. * 判断是空值
  148. */
  149. isEmptyValue(value) {
  150. return value === null || value == undefined
  151. },
  152. /**
  153. * 值更新
  154. */
  155. valueChange() {
  156. if (this.dataValue.select.includes('其他')) {
  157. const newValue = this.dataValue.select.filter(item => item !== '其他')
  158. newValue.push(this.dataValue.otherValue.trim())
  159. this.$emit('input', newValue)
  160. this.$emit('change', newValue)
  161. this.dispatch('ElFormItem', 'el.form.change', newValue)
  162. } else {
  163. this.dataValue.otherValue = ''
  164. this.$emit('input', this.dataValue.select)
  165. this.$emit('change', this.dataValue.select)
  166. this.dispatch('ElFormItem', 'el.form.change', this.dataValue.select)
  167. }
  168. },
  169. /**
  170. * 失去焦点
  171. */
  172. inputBlur() {
  173. this.valueChange()
  174. // const value = this.dataValue.otherValue
  175. // const eIsObject = this.options.length > 0 && !this.isEmptyValue(this.options[0].value)
  176. // const has = this.options.find(item => {
  177. // if (eIsObject) {
  178. // return item.value === value.trim()
  179. // } else {
  180. // return item === value.trim()
  181. // }
  182. // })
  183. // if (has) {
  184. // this.dataValue.otherValue = ''
  185. // }
  186. // this.$emit('change', this.dataValue.select)
  187. // this.$emit('input', this.dataValue.select)
  188. }
  189. }
  190. }
  191. </script>
  192. <style lang="scss" scoped>
  193. .wk-checkbox {
  194. .el-input {
  195. margin-top: 5px;
  196. }
  197. .el-checkbox-group {
  198. line-height: 1.5;
  199. }
  200. }
  201. </style>