index.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * @Description: 颜色选择器
  3. * @author liu.shiyi
  4. * @date 2023/3/31 10:31
  5. */
  6. <template>
  7. <el-select
  8. ref="colorSelect"
  9. v-model="myColor"
  10. value-key="value"
  11. class="bs-el-select select"
  12. popper-class="bs-el-select"
  13. placeholder=""
  14. style="width: 100%"
  15. @change="handleChange"
  16. >
  17. <el-option
  18. v-for="(item, index) in colorList"
  19. :key="index"
  20. :label="item.label"
  21. :value="item.value"
  22. >
  23. <span style="float: left">{{ item.label }}</span>
  24. <span style="float: right">
  25. <span
  26. v-for="(co, ind) in JSON.parse(item.value)"
  27. :key="ind"
  28. >
  29. <span :style="'float: left ;background-color:' + co + ';width:10px;border-radius:1px;display:inline-block;height:15px;margin-top:9px;'" />
  30. <span style="float: left">&nbsp; </span>
  31. </span>
  32. </span>
  33. </el-option>
  34. </el-select>
  35. </template>
  36. <script>
  37. export default {
  38. name: 'ColorSelect',
  39. model: {
  40. prop: 'color',
  41. event: 'update'
  42. },
  43. props: {
  44. // 父组件绑定的值
  45. color: {
  46. type: Array,
  47. default: undefined
  48. }
  49. },
  50. data () {
  51. return {
  52. colorList: [
  53. {
  54. label: '配色1',
  55. value: JSON.stringify(['#6b74e4', '#4391f4', '#38bbe5', '#69d6fd', '#36c6a0'])
  56. },
  57. {
  58. label: '配色2',
  59. value: JSON.stringify(['#1DAEFF', '#25A979', '#D19C4C', '#654FEA', '#C957CB'])
  60. },
  61. {
  62. label: '配色3',
  63. value: JSON.stringify(['#1DAEFF', '#15BCE0', '#1FD7CC', '#43D4A0', '#25A979'])
  64. }
  65. ],
  66. colorValue: []
  67. // myColor: undefined
  68. }
  69. },
  70. watch: {
  71. color: function (val) {
  72. this.init(val)
  73. }
  74. },
  75. computed: {
  76. myColor: {
  77. get () {
  78. return JSON.stringify(this.color) || JSON.stringify(['#6b74e4', '#4391f4', '#38bbe5', '#69d6fd', '#36c6a0'])
  79. },
  80. set (val) {
  81. }
  82. }
  83. },
  84. created () {
  85. },
  86. mounted () {
  87. this.init(this.color)
  88. },
  89. methods: {
  90. // 初始化colorList,当绑定的颜色跟预设的颜色不一致时
  91. init (color) {
  92. // ,当绑定的颜色跟预设的颜色是否一致
  93. const flag = this.colorList.some(co => co.value === JSON.stringify(color))
  94. // colorList是否存在自定义选项
  95. const f = this.colorList.some(co => co.label === '自定义')
  96. if (!flag) {
  97. if (f) {
  98. this.colorList = this.colorList.map(co => {
  99. if (co.label === '自定义') {
  100. return {
  101. label: '自定义',
  102. value: JSON.stringify(color)
  103. }
  104. } else {
  105. return co
  106. }
  107. })
  108. } else {
  109. this.colorList.push({
  110. label: '自定义',
  111. value: JSON.stringify(color)
  112. })
  113. }
  114. }
  115. },
  116. handleChange (val) {
  117. const colors = JSON.parse(val)
  118. // 触发update事件更新父组件绑定值
  119. this.$emit('update', colors)
  120. }
  121. }
  122. }
  123. </script>
  124. <style scoped>
  125. </style>