custom-app
Version:
ITIMS��Ʒ�鿪��ר��React���,�Dz��ý��ּ�dhcc-app���������
202 lines (162 loc) • 7.12 kB
JavaScript
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
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); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
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; }
/* eslint jsx-a11y/no-noninteractive-element-to-interactive-role: 0 */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import ReactDom from 'react-dom';
import classNames from 'classnames';
import raf from 'raf';
var scrollTo = function scrollTo(element, to, duration) {
// jump to target if duration zero
if (duration <= 0) {
raf(function () {
element.scrollTop = to;
});
return;
}
var difference = to - element.scrollTop;
var perTick = difference / duration * 10;
raf(function () {
element.scrollTop += perTick;
if (element.scrollTop === to) return;
scrollTo(element, to, duration - 10);
});
};
var Select =
/*#__PURE__*/
function (_Component) {
_inherits(Select, _Component);
function Select() {
var _getPrototypeOf2;
var _this;
_classCallCheck(this, Select);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Select)).call.apply(_getPrototypeOf2, [this].concat(args)));
_defineProperty(_assertThisInitialized(_this), "state", {
active: false
});
_defineProperty(_assertThisInitialized(_this), "onSelect", function (value) {
var _this$props = _this.props,
onSelect = _this$props.onSelect,
type = _this$props.type;
onSelect(type, value);
});
_defineProperty(_assertThisInitialized(_this), "handleMouseEnter", function (e) {
var onMouseEnter = _this.props.onMouseEnter;
_this.setState({
active: true
});
onMouseEnter(e);
});
_defineProperty(_assertThisInitialized(_this), "handleMouseLeave", function () {
_this.setState({
active: false
});
});
_defineProperty(_assertThisInitialized(_this), "saveList", function (node) {
_this.list = node;
});
return _this;
}
_createClass(Select, [{
key: "componentDidMount",
value: function componentDidMount() {
// jump to selected option
this.scrollToSelected(0);
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var selectedIndex = this.props.selectedIndex; // smooth scroll to selected option
if (prevProps.selectedIndex !== selectedIndex) {
this.scrollToSelected(120);
}
}
}, {
key: "getOptions",
value: function getOptions() {
var _this2 = this;
var _this$props2 = this.props,
options = _this$props2.options,
selectedIndex = _this$props2.selectedIndex,
prefixCls = _this$props2.prefixCls,
onEsc = _this$props2.onEsc;
return options.map(function (item, index) {
var _classNames;
var cls = classNames((_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-select-option-selected"), selectedIndex === index), _defineProperty(_classNames, "".concat(prefixCls, "-select-option-disabled"), item.disabled), _classNames));
var onClick = item.disabled ? undefined : function () {
_this2.onSelect(item.value);
};
var onKeyDown = function onKeyDown(e) {
if (e.keyCode === 13) onClick();else if (e.keyCode === 27) onEsc();
};
return React.createElement("li", {
role: "button",
onClick: onClick,
className: cls,
key: index,
disabled: item.disabled,
tabIndex: "0",
onKeyDown: onKeyDown
}, item.value);
});
}
}, {
key: "scrollToSelected",
value: function scrollToSelected(duration) {
// move to selected item
var selectedIndex = this.props.selectedIndex;
var select = ReactDom.findDOMNode(this);
var list = ReactDom.findDOMNode(this.list);
if (!list) {
return;
}
var index = selectedIndex;
if (index < 0) {
index = 0;
}
var topOption = list.children[index];
var to = topOption.offsetTop;
scrollTo(select, to, duration);
}
}, {
key: "render",
value: function render() {
var _this$props3 = this.props,
prefixCls = _this$props3.prefixCls,
options = _this$props3.options;
var active = this.state.active;
if (options.length === 0) {
return null;
}
var cls = classNames("".concat(prefixCls, "-select"), _defineProperty({}, "".concat(prefixCls, "-select-active"), active));
return React.createElement("div", {
className: cls,
onMouseEnter: this.handleMouseEnter,
onMouseLeave: this.handleMouseLeave
}, React.createElement("ul", {
ref: this.saveList
}, this.getOptions()));
}
}]);
return Select;
}(Component);
_defineProperty(Select, "propTypes", {
prefixCls: PropTypes.string,
options: PropTypes.array,
selectedIndex: PropTypes.number,
type: PropTypes.string,
onSelect: PropTypes.func,
onMouseEnter: PropTypes.func,
onEsc: PropTypes.func
});
export default Select;