UNPKG

4.57 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _vue = require("vue");
9
10var _vueTypes = _interopRequireDefault(require("../_util/vue-types"));
11
12var _BaseMixin = _interopRequireDefault(require("../_util/BaseMixin"));
13
14var _classNames = _interopRequireDefault(require("../_util/classNames"));
15
16var _propsUtil = require("../_util/props-util");
17
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
20function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
21
22function noop() {}
23
24var scrollTo = function scrollTo(element, to, duration) {
25 // jump to target if duration zero
26 if (duration <= 0) {
27 requestAnimationFrame(function () {
28 element.scrollTop = to;
29 });
30 return;
31 }
32
33 var difference = to - element.scrollTop;
34 var perTick = difference / duration * 10;
35 requestAnimationFrame(function () {
36 element.scrollTop += perTick;
37 if (element.scrollTop === to) return;
38 scrollTo(element, to, duration - 10);
39 });
40};
41
42var Select = {
43 name: 'Select',
44 mixins: [_BaseMixin.default],
45 inheritAttrs: false,
46 props: {
47 prefixCls: _vueTypes.default.string,
48 options: _vueTypes.default.array,
49 selectedIndex: _vueTypes.default.number,
50 type: _vueTypes.default.string
51 },
52 data: function data() {
53 return {
54 active: false
55 };
56 },
57 mounted: function mounted() {
58 var _this = this;
59
60 this.$nextTick(function () {
61 // jump to selected option
62 _this.scrollToSelected(0);
63 });
64 },
65 watch: {
66 selectedIndex: function selectedIndex() {
67 var _this2 = this;
68
69 this.$nextTick(function () {
70 // smooth scroll to selected option
71 _this2.scrollToSelected(120);
72 });
73 }
74 },
75 methods: {
76 onSelect: function onSelect(value) {
77 var type = this.type;
78
79 this.__emit('select', type, value);
80 },
81 onEsc: function onEsc(e) {
82 this.__emit('esc', e);
83 },
84 getOptions: function getOptions() {
85 var _this3 = this;
86
87 var options = this.options,
88 selectedIndex = this.selectedIndex,
89 prefixCls = this.prefixCls;
90 return options.map(function (item, index) {
91 var _classnames;
92
93 var cls = (0, _classNames.default)((_classnames = {}, _defineProperty(_classnames, "".concat(prefixCls, "-select-option-selected"), selectedIndex === index), _defineProperty(_classnames, "".concat(prefixCls, "-select-option-disabled"), item.disabled), _classnames));
94 var onClick = item.disabled ? noop : function () {
95 _this3.onSelect(item.value);
96 };
97
98 var onKeyDown = function onKeyDown(e) {
99 if (e.keyCode === 13) onClick();else if (e.keyCode === 27) _this3.onEsc();
100 };
101
102 return (0, _vue.createVNode)("li", {
103 "role": "button",
104 "onClick": onClick,
105 "class": cls,
106 "key": index,
107 "disabled": item.disabled,
108 "tabindex": "0",
109 "onKeydown": onKeyDown
110 }, [item.value]);
111 });
112 },
113 handleMouseEnter: function handleMouseEnter(e) {
114 this.setState({
115 active: true
116 });
117
118 this.__emit('mouseenter', e);
119 },
120 handleMouseLeave: function handleMouseLeave() {
121 this.setState({
122 active: false
123 });
124 },
125 scrollToSelected: function scrollToSelected(duration) {
126 // move to selected item
127 var select = (0, _propsUtil.findDOMNode)(this);
128 var list = this.$refs.list;
129
130 if (!list) {
131 return;
132 }
133
134 var index = this.selectedIndex;
135
136 if (index < 0) {
137 index = 0;
138 }
139
140 var topOption = list.children[index];
141 var to = topOption.offsetTop;
142 scrollTo(select, to, duration);
143 }
144 },
145 render: function render() {
146 var _cls;
147
148 var prefixCls = this.prefixCls,
149 options = this.options,
150 active = this.active;
151
152 if (options.length === 0) {
153 return null;
154 }
155
156 var cls = (_cls = {}, _defineProperty(_cls, "".concat(prefixCls, "-select"), 1), _defineProperty(_cls, "".concat(prefixCls, "-select-active"), active), _cls);
157 return (0, _vue.createVNode)("div", {
158 "class": cls,
159 "onMouseenter": this.handleMouseEnter,
160 "onMouseleave": this.handleMouseLeave
161 }, [(0, _vue.createVNode)("ul", {
162 "ref": "list"
163 }, [this.getOptions()])]);
164 }
165};
166var _default = Select;
167exports.default = _default;
\No newline at end of file