hejianhao
2025-04-16 dab2d210ca06c1faa514c6388fbd5de1ab355360
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<template>
  <div class="public-input-color-picker">
    <el-input v-model="value2" :placeholder="placeholder" @input="_change"></el-input>
    <el-color-picker
      v-model="value2"
      :show-alpha="showAlpha"
      :predefine="predefineColors"
      @change="_change"
    ></el-color-picker>
  </div>
</template>
<script>
/**
 * 输入框颜色选择器
 * ps:预定义颜色中,如果颜色不对,将无法选中
 *
 * @showAlpha 是否支持透明度 默认false
 * @predefineColors 预定义颜色
 * @value 默认颜色编码 示例:#ffffff
 * @placeholder 输入框提示文字
 */
export default {
  name: 'public-input-color-picker',
  data() {
    return {
      value2: ''
    }
  },
  props: {
    showAlpha: { type: Boolean, default: false },
    predefineColors: {
      type: Array,
      default: () => {
        return [
          '#ff4500',
          '#ff8c00',
          '#ffd700',
          '#90ee90',
          '#00ced1',
          '#1e90ff',
          '#c71585',
          '#FF0000'
        ]
      }
    },
    value: { type: String, default: '' },
    placeholder: { type: String, default: '请输入颜色编码' }
  },
  components: {},
  computed: {},
  created() {
  },
  onload() { },
  onShow() { },
  watch: {
    value: {
      handler: function (newVal, oldVal) {
        this.value2 = JSON.parse(JSON.stringify(newVal))
      },
      immediate: true,
      deep: true
    }
  },
  mounted() { },
  methods: {
    _change(val) {
      if (val) {
        this.$emit('change', val)
      }
    }
  }
}
</script>
<style lang="less" scoped>
@box-width: 200px;
.public-input-color-picker {
  // display: flex;
  // -webkit-box-align: center;
  // -webkit-align-items: center;
  // align-items: center;
  position: relative;
  width: @box-width;
  .el-input {
    width: @box-width;
  }
  .el-color-picker {
    position: absolute;
    right: 0;
    /deep/ .el-color-picker__trigger {
      border: 0;
    }
  }
}
</style>