import _defineProperty from"babel-runtime/helpers/defineProperty";import _classCallCheck from"babel-runtime/helpers/classCallCheck";import _createClass from"babel-runtime/helpers/createClass";import _possibleConstructorReturn from"babel-runtime/helpers/possibleConstructorReturn";import _inherits from"babel-runtime/helpers/inherits";import*as React from"react";import*as PropTypes from"prop-types";import*as ReactDom from"react-dom";import classnames from"classnames";var scrollTo=function e(t,s,o){var r=window.requestAnimationFrame||function(){return setTimeout(arguments[0],10)};if(o<=0)t.scrollTop=s;else{var n=(s-t.scrollTop)/o*10;r(function(){t.scrollTop=t.scrollTop+n,t.scrollTop!==s&&e(t,s,o-10)})}},Select=function(e){function t(){_classCallCheck(this,t);var e=_possibleConstructorReturn(this,(t.__proto__||Object.getPrototypeOf(t)).apply(this,arguments));return e.state={active:!1},e.onSelect=function(t){var s=e.props;(0,s.onSelect)(s.type,t)},e.handleMouseEnter=function(t){e.setState({active:!0}),e.props.onMouseEnter(t)},e.handleMouseLeave=function(){e.setState({active:!1})},e.saveList=function(t){e.list=t},e}return _inherits(t,e),_createClass(t,[{key:"componentDidMount",value:function(){this.scrollToSelected(0)}},{key:"componentDidUpdate",value:function(e){e.selectedIndex!==this.props.selectedIndex&&this.scrollToSelected(120)}},{key:"getOptions",value:function(){var e=this,t=this.props,s=t.options,o=t.selectedIndex,r=t.prefixCls;return s.map(function(t,s){var n,l=classnames((_defineProperty(n={},r+"-select-option-selected",o===s),_defineProperty(n,r+"-select-option-disabled",t.disabled),n)),i=null;return t.disabled||(i=e.onSelect.bind(e,t.value)),React.createElement("li",{className:l,key:s,onClick:i},t.value)})}},{key:"scrollToSelected",value:function(e){var t=ReactDom.findDOMNode(this),s=ReactDom.findDOMNode(this.list);if(s){var o=this.props.selectedIndex;o<0&&(o=0);var r=s.children[o].offsetTop;scrollTo(t,r,e)}}},{key:"render",value:function(){var e;if(0===this.props.options.length)return null;var t=this.props.prefixCls,s=classnames((_defineProperty(e={},t+"-select",1),_defineProperty(e,t+"-select-active",this.state.active),e));return React.createElement("div",{className:s,onMouseEnter:this.handleMouseEnter,onMouseLeave:this.handleMouseLeave},React.createElement("ul",{ref:this.saveList},this.getOptions()))}}]),t}(React.Component);Select.propTypes={prefixCls:PropTypes.string,options:PropTypes.array,selectedIndex:PropTypes.number,type:PropTypes.string,onSelect:PropTypes.func,onMouseEnter:PropTypes.func};export default Select;