index.vue 2.7 KB

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