index.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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(['#FF6B3B', '#626681', '#FFC100', '#9FB40F', '#76523B'])
  60. },
  61. {
  62. label: '配色3',
  63. value: JSON.stringify(['#025DF4', '#DB6BCF', '#2498D1', '#BBBDE6', '#4045B2'])
  64. },
  65. {
  66. label: '配色4',
  67. value: JSON.stringify(['#FF4500', '#1AAF8B', '#406C85', '#F6BD16', '#B40F0F'])
  68. }
  69. ],
  70. colorValue: []
  71. // myColor: undefined
  72. }
  73. },
  74. watch: {
  75. color: function (val) {
  76. this.init(val)
  77. }
  78. },
  79. computed: {
  80. myColor: {
  81. get () {
  82. return JSON.stringify(this.color) || JSON.stringify(['#5B8FF9', '#61DDAA', '#5D7092', '#F6BD16', '#6F5EF9', '#6DC8EC', '#945FB9', '#FF9845', '#1E9493', '#FF99C3'])
  83. },
  84. set (val) {
  85. }
  86. }
  87. },
  88. created () {
  89. },
  90. mounted () {
  91. this.init(this.color)
  92. },
  93. methods: {
  94. // 初始化colorList,当绑定的颜色跟预设的颜色不一致时
  95. init (color) {
  96. // ,当绑定的颜色跟预设的颜色是否一致
  97. const flag = this.colorList.some(co => co.value === JSON.stringify(color))
  98. // colorList是否存在自定义选项
  99. const f = this.colorList.some(co => co.label === '自定义')
  100. if (!flag) {
  101. if (f) {
  102. this.colorList = this.colorList.map(co => {
  103. if (co.label === '自定义') {
  104. return {
  105. label: '自定义',
  106. value: JSON.stringify(color)
  107. }
  108. } else {
  109. return co
  110. }
  111. })
  112. } else {
  113. this.colorList.push({
  114. label: '自定义',
  115. value: JSON.stringify(color)
  116. })
  117. }
  118. }
  119. },
  120. handleChange (val) {
  121. const colors = JSON.parse(val)
  122. // 触发update事件更新父组件绑定值
  123. this.$emit('update', colors)
  124. }
  125. }
  126. }
  127. </script>
  128. <style scoped>
  129. </style>