forked from lq782655835/yi-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathu-select.vue
142 lines (130 loc) · 3.42 KB
/
u-select.vue
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
130
131
132
133
134
135
136
137
138
139
140
141
142
<template>
<div class="u-select" :disabled="disabled" tabIndex="0" @blur="open = false">
<div @click.stop="clickInput" class="label">
{{ selectedItem.label || '请选择' }} <i :class="['arrow', open ? 'up' : 'down']" />
</div>
<div v-if="open" class="options">
<div
v-for="(item, index) in formatList"
:key="index"
:title="item.label"
@click="selectItem(item)"
:class="['option', { seleced: item.label == selectedItem.label }]"
>
{{ item.label }}
</div>
</div>
</div>
</template>
<script>
export default {
name: 'u-select',
props: {
list: { type: Array, default: () => [] },
value: [String, Number],
labelField: { type: String, default: 'label' },
valueField: { type: String, default: 'value' },
disabled: { type: Boolean, default: false }
},
data() {
return {
open: false
}
},
computed: {
formatList() {
return this.list.map(item =>
Object.assign({}, item, {
label: item[this.labelField],
value: item[this.valueField]
})
)
},
selectedItem() {
if (!this.value) return {}
return this.formatList.find(item => item.value === this.value) || {}
}
},
methods: {
clickInput() {
if (this.disabled) return
this.open = !this.open
},
selectItem(item) {
this.$emit('select', item)
this.$emit('update:value', item.value) // allow sync api
this.$emit('input', item.value) // allow v-model api
this.open = false
}
}
}
</script>
<style lang="scss">
$max-height: 220px;
.u-select {
position: relative;
display: inline-block;
border: 1px solid #cccccc;
border-radius: 2px;
height: $component-height;
line-height: $component-height;
background: #fff;
color: #738298;
outline: none;
user-select: none;
&[disabled] {
cursor: not-allowed;
color: #ffffff;
background: #95a2b5;
}
&[size='l'] {
width: 140px;
}
&[size='s'] {
width: 120px;
}
.label {
position: relative;
box-sizing: border-box;
width: 100%;
height: 100%;
padding: 0 26px 0 10px;
.arrow {
position: absolute;
right: 8px;
top: 8px;
border: solid #738298;
border-width: 0 1px 1px 0;
padding: 4px;
transition: all 0.2s ease-in-out;
transform-origin: 10% 10%;
&.down {
transform: rotate(45deg);
}
&.up {
top: 20px;
transform: rotate(-135deg);
}
}
}
.options {
position: absolute;
z-index: 999;
margin-top: 1px;
box-sizing: border-box;
box-shadow: 0 0 6px 0 rgba(150, 150, 150, 0.5);
border-radius: 2px;
width: 100%;
height: auto;
max-height: $max-height;
overflow: auto;
padding: 0 16px;
background: #fff;
.option.selected,
.option:hover {
color: $primary-color;
cursor: pointer;
}
}
}
</style>