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
143
144
145
import _defineProperty from 'babel-runtime/helpers/defineProperty';
import PropTypes from '../_util/vue-types';
import BaseMixin from '../_util/BaseMixin';
import classnames from 'classnames';
function noop() {}
var scrollTo = function scrollTo(element, to, duration) {
var requestAnimationFrame = window.requestAnimationFrame || function requestAnimationFrameTimeout() {
return setTimeout(arguments[0], 10);
};
// jump to target if duration zero
if (duration <= 0) {
element.scrollTop = to;
return;
}
var difference = to - element.scrollTop;
var perTick = difference / duration * 10;
requestAnimationFrame(function () {
element.scrollTop = element.scrollTop + perTick;
if (element.scrollTop === to) return;
scrollTo(element, to, duration - 10);
});
};
var Select = {
mixins: [BaseMixin],
props: {
prefixCls: PropTypes.string,
options: PropTypes.array,
selectedIndex: PropTypes.number,
type: PropTypes.string
// onSelect: PropTypes.func,
// onMouseEnter: PropTypes.func,
},
data: function data() {
return {
active: false
};
},
mounted: function mounted() {
var _this = this;
this.$nextTick(function () {
// jump to selected option
_this.scrollToSelected(0);
});
},
watch: {
selectedIndex: function selectedIndex() {
var _this2 = this;
this.$nextTick(function () {
// smooth scroll to selected option
_this2.scrollToSelected(120);
});
}
},
methods: {
onSelect: function onSelect(value) {
var type = this.type;
this.__emit('select', type, value);
},
getOptions: function getOptions() {
var _this3 = this;
var h = this.$createElement;
var options = this.options,
selectedIndex = this.selectedIndex,
prefixCls = this.prefixCls;
return options.map(function (item, index) {
var _classnames;
var cls = classnames((_classnames = {}, _defineProperty(_classnames, prefixCls + '-select-option-selected', selectedIndex === index), _defineProperty(_classnames, prefixCls + '-select-option-disabled', item.disabled), _classnames));
var onClick = noop;
if (!item.disabled) {
onClick = _this3.onSelect.bind(_this3, item.value);
}
return h(
'li',
{ 'class': cls, key: index, on: {
'click': onClick
},
attrs: { disabled: item.disabled }
},
[item.value]
);
});
},
scrollToSelected: function scrollToSelected(duration) {
// move to selected item
var select = this.$el;
var list = this.$refs.list;
if (!list) {
return;
}
var index = this.selectedIndex;
if (index < 0) {
index = 0;
}
var topOption = list.children[index];
var to = topOption.offsetTop;
scrollTo(select, to, duration);
},
handleMouseEnter: function handleMouseEnter(e) {
this.setState({ active: true });
this.__emit('mouseenter', e);
},
handleMouseLeave: function handleMouseLeave() {
this.setState({ active: false });
}
},
render: function render() {
var _cls;
var h = arguments[0];
if (this.options.length === 0) {
return null;
}
var prefixCls = this.prefixCls;
var cls = (_cls = {}, _defineProperty(_cls, prefixCls + '-select', 1), _defineProperty(_cls, prefixCls + '-select-active', this.active), _cls);
return h(
'div',
{ 'class': cls, on: {
'mouseenter': this.handleMouseEnter,
'mouseleave': this.handleMouseLeave
}
},
[h(
'ul',
{ ref: 'list' },
[this.getOptions()]
)]
);
}
};
export default Select;