]> git.immae.eu Git - github/fretlink/time-picker.git/blame - src/Select.jsx
Merge branch 'shaleynikov-master'
[github/fretlink/time-picker.git] / src / Select.jsx
CommitLineData
4984ed85 1import React, { PropTypes } from 'react';
02de449a 2import ReactDom from 'react-dom';
3import classnames from 'classnames';
4
5const scrollTo = (element, to, duration) => {
0b9ef41f 6 const requestAnimationFrame = window.requestAnimationFrame ||
7 function requestAnimationFrameTimeout() {
8 return setTimeout(arguments[0], 10);
9 };
02de449a 10 // jump to target if duration zero
11 if (duration <= 0) {
12 element.scrollTop = to;
13 return;
14 }
15 const difference = to - element.scrollTop;
16 const perTick = difference / duration * 10;
17
0b9ef41f 18 requestAnimationFrame(() => {
02de449a 19 element.scrollTop = element.scrollTop + perTick;
20 if (element.scrollTop === to) return;
21 scrollTo(element, to, duration - 10);
0b9ef41f 22 });
02de449a 23};
24
25const Select = React.createClass({
26 propTypes: {
27 prefixCls: PropTypes.string,
28 options: PropTypes.array,
29 selectedIndex: PropTypes.number,
30 type: PropTypes.string,
31 onSelect: PropTypes.func,
182e9fcc 32 onMouseEnter: PropTypes.func,
02de449a 33 },
34
35 componentDidMount() {
36 // jump to selected option
37 this.scrollToSelected(0);
38 },
39
1cd769a6 40 componentDidUpdate(prevProps) {
02de449a 41 // smooth scroll to selected option
1cd769a6 42 if (prevProps.selectedIndex !== this.props.selectedIndex) {
43 this.scrollToSelected(120);
44 }
02de449a 45 },
46
8133e8cf 47 onSelect(value) {
02de449a 48 const { onSelect, type } = this.props;
02de449a 49 onSelect(type, value);
50 },
51
52 getOptions() {
8133e8cf 53 const { options, selectedIndex, prefixCls } = this.props;
02de449a 54 return options.map((item, index) => {
8133e8cf 55 const cls = classnames({
518b852e
M
56 [`${prefixCls}-select-option-selected`]: selectedIndex === index,
57 [`${prefixCls}-select-option-disabled`]: item.disabled,
8133e8cf 58 });
518b852e
M
59 let onclick = null;
60 if (!item.disabled) {
95699887 61 onclick = this.onSelect.bind(this, item.value);
518b852e 62 }
4984ed85 63 return (<li
64 className={cls}
65 key={index}
66 onClick={onclick}
67 disabled={item.disabled}
68 >
69 {item.value}
70 </li>);
02de449a 71 });
72 },
73
74 scrollToSelected(duration) {
75 // move to selected item
76 const select = ReactDom.findDOMNode(this);
77 const list = ReactDom.findDOMNode(this.refs.list);
cb5a445c 78 if (!list) {
79 return;
80 }
5fcf22e0 81 let index = this.props.selectedIndex;
02de449a 82 if (index < 0) {
83 index = 0;
84 }
85 const topOption = list.children[index];
428cfa8d 86 const to = topOption.offsetTop;
85d09ad3 87 scrollTo(select, to, duration);
02de449a 88 },
89
90 render() {
91 if (this.props.options.length === 0) {
92 return null;
93 }
94
95 const { prefixCls } = this.props;
96
97 return (
4984ed85 98 <div
99 className={`${prefixCls}-select`}
100 onMouseEnter={this.props.onMouseEnter}
101 >
02de449a 102 <ul ref="list">{this.getOptions()}</ul>
103 </div>
104 );
105 },
106});
107
108export default Select;