index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. class="bs-el-select select"
  11. popper-class="bs-el-select"
  12. placeholder=""
  13. style="width: 100%"
  14. @change="handleChange"
  15. >
  16. <div slot="prefix">
  17. <span style="float: left">&nbsp; </span>
  18. <span
  19. v-for="(item, index) in colorValue"
  20. :key="index"
  21. >
  22. <span
  23. v-if="index <= 14"
  24. :style="'float: left ;background-color:' + item + ';width:15px;border-radius:1px;display:inline-block;height:15px;margin-top:8px;'"
  25. />
  26. <span
  27. v-if="index <= 14"
  28. style="float: left"
  29. >
  30. &nbsp;
  31. </span>
  32. <span
  33. v-if="index === 15"
  34. style="float: left"
  35. >
  36. &nbsp;...
  37. </span>
  38. </span>
  39. </div>
  40. <el-option
  41. v-for="(item, index) in colorList"
  42. :key="index"
  43. label=" "
  44. :value="item"
  45. >
  46. <span
  47. v-for="(co, ind) in item"
  48. :key="ind"
  49. >
  50. <span :style="'float: left ;background-color:' + co + ';width:15px;border-radius:1px;display:inline-block;height:15px;margin-top:9px;'" />
  51. <span style="float: left">&nbsp; </span>
  52. </span>
  53. </el-option>
  54. </el-select>
  55. </template>
  56. <script>
  57. export default {
  58. name: 'ColorSelect',
  59. model: {
  60. prop: 'color',
  61. event: 'update'
  62. },
  63. props: {
  64. // 颜色数组
  65. colorList: {
  66. type: Array,
  67. default: () => {
  68. return [
  69. ['#5B8FF9', '#61DDAA', '#5D7092', '#F6BD16', '#6F5EF9', '#6DC8EC', '#945FB9', '#FF9845', '#1E9493', '#FF99C3'],
  70. ['#FF6B3B', '#626681', '#FFC100', '#9FB40F', '#76523B', '#DAD5B5', '#0E8E89', '#E19348', '#F383A2', '#247FEA'],
  71. ['#025DF4', '#DB6BCF', '#2498D1', '#BBBDE6', '#4045B2', '#21A97A', '#FF745A', '#007E99', '#FFA8A8', '#2391FF'],
  72. ['#FF4500', '#1AAF8B', '#406C85', '#F6BD16', '#B40F0F', '#2FB8FC', '#4435FF', '#FF5CA2', '#BBE800', '#FE8A26']
  73. ]
  74. }
  75. },
  76. // 父组件绑定的值
  77. color: {
  78. type: Array,
  79. default: undefined
  80. }
  81. },
  82. data () {
  83. return {
  84. colorValue: [],
  85. myColor: undefined
  86. }
  87. },
  88. watch: {
  89. color: function (val) {
  90. this.setSelectColor(val)
  91. }
  92. },
  93. created () {
  94. if (this.color) {
  95. this.myColor = this.color
  96. this.setSelectColor(this.color)
  97. }
  98. },
  99. methods: {
  100. // 设置颜色选择框中颜色
  101. setSelectColor (color) {
  102. this.$nextTick(() => {
  103. this.colorValue = this.color
  104. })
  105. },
  106. handleChange (val) {
  107. this.setSelectColor(val)
  108. // 触发update事件更新父组件绑定值
  109. this.$emit('update', val)
  110. }
  111. }
  112. }
  113. </script>
  114. <style scoped>
  115. </style>