1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports["default"] = void 0;
|
7 |
|
8 | var _react = _interopRequireWildcard(require("react"));
|
9 |
|
10 | var _propTypes = _interopRequireDefault(require("prop-types"));
|
11 |
|
12 | var _reactDom = _interopRequireDefault(require("react-dom"));
|
13 |
|
14 | var _classnames = _interopRequireDefault(require("classnames"));
|
15 |
|
16 | var _raf = _interopRequireDefault(require("raf"));
|
17 |
|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
19 |
|
20 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }
|
21 |
|
22 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
23 |
|
24 | function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
25 |
|
26 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
27 |
|
28 | function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
|
29 |
|
30 | function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
31 |
|
32 | function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
33 |
|
34 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
35 |
|
36 | function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
37 |
|
38 | function _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; }
|
39 |
|
40 | var scrollTo = function scrollTo(element, to, duration) {
|
41 |
|
42 | if (duration <= 0) {
|
43 | (0, _raf["default"])(function () {
|
44 | element.scrollTop = to;
|
45 | });
|
46 | return;
|
47 | }
|
48 |
|
49 | var difference = to - element.scrollTop;
|
50 | var perTick = difference / duration * 10;
|
51 | (0, _raf["default"])(function () {
|
52 | element.scrollTop += perTick;
|
53 | if (element.scrollTop === to) return;
|
54 | scrollTo(element, to, duration - 10);
|
55 | });
|
56 | };
|
57 |
|
58 | var Select =
|
59 |
|
60 | function (_Component) {
|
61 | _inherits(Select, _Component);
|
62 |
|
63 | function Select() {
|
64 | var _getPrototypeOf2;
|
65 |
|
66 | var _this;
|
67 |
|
68 | _classCallCheck(this, Select);
|
69 |
|
70 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
71 | args[_key] = arguments[_key];
|
72 | }
|
73 |
|
74 | _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Select)).call.apply(_getPrototypeOf2, [this].concat(args)));
|
75 |
|
76 | _defineProperty(_assertThisInitialized(_this), "state", {
|
77 | active: false
|
78 | });
|
79 |
|
80 | _defineProperty(_assertThisInitialized(_this), "onSelect", function (value) {
|
81 | var _this$props = _this.props,
|
82 | onSelect = _this$props.onSelect,
|
83 | type = _this$props.type;
|
84 | onSelect(type, value);
|
85 | });
|
86 |
|
87 | _defineProperty(_assertThisInitialized(_this), "handleMouseEnter", function (e) {
|
88 | var onMouseEnter = _this.props.onMouseEnter;
|
89 |
|
90 | _this.setState({
|
91 | active: true
|
92 | });
|
93 |
|
94 | onMouseEnter(e);
|
95 | });
|
96 |
|
97 | _defineProperty(_assertThisInitialized(_this), "handleMouseLeave", function () {
|
98 | _this.setState({
|
99 | active: false
|
100 | });
|
101 | });
|
102 |
|
103 | _defineProperty(_assertThisInitialized(_this), "saveList", function (node) {
|
104 | _this.list = node;
|
105 | });
|
106 |
|
107 | return _this;
|
108 | }
|
109 |
|
110 | _createClass(Select, [{
|
111 | key: "componentDidMount",
|
112 | value: function componentDidMount() {
|
113 |
|
114 | this.scrollToSelected(0);
|
115 | }
|
116 | }, {
|
117 | key: "componentDidUpdate",
|
118 | value: function componentDidUpdate(prevProps) {
|
119 | var selectedIndex = this.props.selectedIndex;
|
120 |
|
121 | if (prevProps.selectedIndex !== selectedIndex) {
|
122 | this.scrollToSelected(120);
|
123 | }
|
124 | }
|
125 | }, {
|
126 | key: "getOptions",
|
127 | value: function getOptions() {
|
128 | var _this2 = this;
|
129 |
|
130 | var _this$props2 = this.props,
|
131 | options = _this$props2.options,
|
132 | selectedIndex = _this$props2.selectedIndex,
|
133 | prefixCls = _this$props2.prefixCls,
|
134 | onEsc = _this$props2.onEsc;
|
135 | return options.map(function (item, index) {
|
136 | var _classNames;
|
137 |
|
138 | 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));
|
139 | var onClick = item.disabled ? undefined : function () {
|
140 | _this2.onSelect(item.value);
|
141 | };
|
142 |
|
143 | var onKeyDown = function onKeyDown(e) {
|
144 | if (e.keyCode === 13) onClick();else if (e.keyCode === 27) onEsc();
|
145 | };
|
146 |
|
147 | return _react["default"].createElement("li", {
|
148 | role: "button",
|
149 | onClick: onClick,
|
150 | className: cls,
|
151 | key: index,
|
152 | disabled: item.disabled,
|
153 | tabIndex: "0",
|
154 | onKeyDown: onKeyDown
|
155 | }, item.value);
|
156 | });
|
157 | }
|
158 | }, {
|
159 | key: "scrollToSelected",
|
160 | value: function scrollToSelected(duration) {
|
161 |
|
162 | var selectedIndex = this.props.selectedIndex;
|
163 |
|
164 | var select = _reactDom["default"].findDOMNode(this);
|
165 |
|
166 | var list = _reactDom["default"].findDOMNode(this.list);
|
167 |
|
168 | if (!list) {
|
169 | return;
|
170 | }
|
171 |
|
172 | var index = selectedIndex;
|
173 |
|
174 | if (index < 0) {
|
175 | index = 0;
|
176 | }
|
177 |
|
178 | var topOption = list.children[index];
|
179 | var to = topOption.offsetTop;
|
180 | scrollTo(select, to, duration);
|
181 | }
|
182 | }, {
|
183 | key: "render",
|
184 | value: function render() {
|
185 | var _this$props3 = this.props,
|
186 | prefixCls = _this$props3.prefixCls,
|
187 | options = _this$props3.options;
|
188 | var active = this.state.active;
|
189 |
|
190 | if (options.length === 0) {
|
191 | return null;
|
192 | }
|
193 |
|
194 | var cls = (0, _classnames["default"])("".concat(prefixCls, "-select"), _defineProperty({}, "".concat(prefixCls, "-select-active"), active));
|
195 | return _react["default"].createElement("div", {
|
196 | className: cls,
|
197 | onMouseEnter: this.handleMouseEnter,
|
198 | onMouseLeave: this.handleMouseLeave
|
199 | }, _react["default"].createElement("ul", {
|
200 | ref: this.saveList
|
201 | }, this.getOptions()));
|
202 | }
|
203 | }]);
|
204 |
|
205 | return Select;
|
206 | }(_react.Component);
|
207 |
|
208 | _defineProperty(Select, "propTypes", {
|
209 | prefixCls: _propTypes["default"].string,
|
210 | options: _propTypes["default"].array,
|
211 | selectedIndex: _propTypes["default"].number,
|
212 | type: _propTypes["default"].string,
|
213 | onSelect: _propTypes["default"].func,
|
214 | onMouseEnter: _propTypes["default"].func,
|
215 | onEsc: _propTypes["default"].func
|
216 | });
|
217 |
|
218 | var _default = Select;
|
219 | exports["default"] = _default; |
\ | No newline at end of file |