ColorSelect.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <template>
  2. <el-dropdown
  3. trigger="click"
  4. class="color-select custom-select"
  5. placement="bottom-start"
  6. @command="select"
  7. >
  8. <div class="select-preview">
  9. <div class="select-preview-main">
  10. <div
  11. class="color-item"
  12. :style="{ backgroundColor: selected }"
  13. v-if="selected"
  14. ></div>
  15. </div>
  16. <div class="select-preview-icon">
  17. <i class="el-icon-arrow-down"></i>
  18. </div>
  19. </div>
  20. <el-dropdown-menu slot="dropdown" class="color-menu">
  21. <el-dropdown-item command="" v-if="showEmpty">无</el-dropdown-item>
  22. <el-dropdown-item
  23. v-for="(option, index) in optionList"
  24. :key="index"
  25. :command="option"
  26. >
  27. <div class="color-item" :style="{ backgroundColor: option }"></div>
  28. </el-dropdown-item>
  29. </el-dropdown-menu>
  30. </el-dropdown>
  31. </template>
  32. <script>
  33. const PREDEFINE_OPTIONS = ["#000000", "#666666", "#999999", "#ffffff"];
  34. export default {
  35. name: "color-select",
  36. props: {
  37. value: String,
  38. predefine: Array,
  39. showEmpty: {
  40. type: Boolean,
  41. default: false,
  42. },
  43. },
  44. data() {
  45. return {
  46. optionList: [],
  47. selected: "",
  48. };
  49. },
  50. watch: {
  51. value: {
  52. immediate: true,
  53. handler(val) {
  54. this.selected = val;
  55. },
  56. },
  57. },
  58. created() {
  59. this.optionList =
  60. this.predefine && this.predefine.length
  61. ? this.predefine
  62. : PREDEFINE_OPTIONS;
  63. },
  64. methods: {
  65. select(option) {
  66. this.selected = option;
  67. this.$emit("input", this.selected);
  68. this.$emit("change", this.selected);
  69. },
  70. },
  71. };
  72. </script>