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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
| /**
| * @Description: 颜色选择器
| * @author liu.shiyi
| * @date 2023/3/31 10:31
| */
| <template>
| <el-select
| ref="colorSelect"
| v-model="myColor"
| value-key="value"
| class="bs-el-select select"
| popper-class="bs-el-select"
| placeholder=""
| style="width: 100%"
| @change="handleChange"
| >
| <el-option
| v-for="(item, index) in colorList"
| :key="index"
| :label="item.label"
| :value="item.value"
| >
| <span style="float: left">{{ item.label }}</span>
| <span style="float: right">
| <span
| v-for="(co, ind) in JSON.parse(item.value)"
| :key="ind"
| >
| <span :style="'float: left ;background-color:' + co + ';width:10px;border-radius:1px;display:inline-block;height:15px;margin-top:9px;'" />
| <span style="float: left"> </span>
| </span>
| </span>
| </el-option>
| </el-select>
| </template>
|
| <script>
| export default {
| name: 'ColorSelect',
| model: {
| prop: 'color',
| event: 'update'
| },
| props: {
| // 父组件绑定的值
| color: {
| type: Array,
| default: undefined
| }
| },
| data () {
| return {
| colorList: [
| {
| label: '配色1',
| value: JSON.stringify(['#6b74e4', '#4391f4', '#38bbe5', '#69d6fd', '#36c6a0'])
| },
| {
| label: '配色2',
| value: JSON.stringify(['#1DAEFF', '#25A979', '#D19C4C', '#654FEA', '#C957CB'])
| },
| {
| label: '配色3',
| value: JSON.stringify(['#1DAEFF', '#15BCE0', '#1FD7CC', '#43D4A0', '#25A979'])
| }
| ],
| colorValue: []
| // myColor: undefined
| }
| },
| watch: {
| color: function (val) {
| this.init(val)
| }
| },
| computed: {
| myColor: {
| get () {
| return JSON.stringify(this.color) || JSON.stringify(['#6b74e4', '#4391f4', '#38bbe5', '#69d6fd', '#36c6a0'])
| },
| set (val) {
|
| }
|
| }
| },
| created () {
| },
| mounted () {
| this.init(this.color)
| },
| methods: {
| // 初始化colorList,当绑定的颜色跟预设的颜色不一致时
| init (color) {
| // ,当绑定的颜色跟预设的颜色是否一致
| const flag = this.colorList.some(co => co.value === JSON.stringify(color))
| // colorList是否存在自定义选项
| const f = this.colorList.some(co => co.label === '自定义')
| if (!flag) {
| if (f) {
| this.colorList = this.colorList.map(co => {
| if (co.label === '自定义') {
| return {
| label: '自定义',
| value: JSON.stringify(color)
| }
| } else {
| return co
| }
| })
| } else {
| this.colorList.push({
| label: '自定义',
| value: JSON.stringify(color)
| })
| }
| }
| },
| handleChange (val) {
| const colors = JSON.parse(val)
| // 触发update事件更新父组件绑定值
| this.$emit('update', colors)
| }
| }
| }
| </script>
|
| <style scoped>
| </style>
|
|