]>
Commit | Line | Data |
---|---|---|
4984ed85 | 1 | import React, { PropTypes } from 'react'; |
02de449a | 2 | import ReactDom from 'react-dom'; |
3 | import classnames from 'classnames'; | |
4 | ||
5 | const 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 | ||
25 | const 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 | ||
d093074a | 35 | getInitialState() { |
36 | return { | |
37 | active: false, | |
38 | }; | |
39 | }, | |
40 | ||
02de449a | 41 | componentDidMount() { |
42 | // jump to selected option | |
43 | this.scrollToSelected(0); | |
44 | }, | |
45 | ||
1cd769a6 | 46 | componentDidUpdate(prevProps) { |
02de449a | 47 | // smooth scroll to selected option |
1cd769a6 | 48 | if (prevProps.selectedIndex !== this.props.selectedIndex) { |
49 | this.scrollToSelected(120); | |
50 | } | |
02de449a | 51 | }, |
52 | ||
8133e8cf | 53 | onSelect(value) { |
02de449a | 54 | const { onSelect, type } = this.props; |
02de449a | 55 | onSelect(type, value); |
56 | }, | |
57 | ||
58 | getOptions() { | |
8133e8cf | 59 | const { options, selectedIndex, prefixCls } = this.props; |
02de449a | 60 | return options.map((item, index) => { |
8133e8cf | 61 | const cls = classnames({ |
518b852e M |
62 | [`${prefixCls}-select-option-selected`]: selectedIndex === index, |
63 | [`${prefixCls}-select-option-disabled`]: item.disabled, | |
8133e8cf | 64 | }); |
518b852e M |
65 | let onclick = null; |
66 | if (!item.disabled) { | |
95699887 | 67 | onclick = this.onSelect.bind(this, item.value); |
518b852e | 68 | } |
4984ed85 | 69 | return (<li |
70 | className={cls} | |
71 | key={index} | |
72 | onClick={onclick} | |
73 | disabled={item.disabled} | |
74 | > | |
75 | {item.value} | |
76 | </li>); | |
02de449a | 77 | }); |
78 | }, | |
79 | ||
80 | scrollToSelected(duration) { | |
81 | // move to selected item | |
82 | const select = ReactDom.findDOMNode(this); | |
83 | const list = ReactDom.findDOMNode(this.refs.list); | |
cb5a445c | 84 | if (!list) { |
85 | return; | |
86 | } | |
5fcf22e0 | 87 | let index = this.props.selectedIndex; |
02de449a | 88 | if (index < 0) { |
89 | index = 0; | |
90 | } | |
91 | const topOption = list.children[index]; | |
428cfa8d | 92 | const to = topOption.offsetTop; |
85d09ad3 | 93 | scrollTo(select, to, duration); |
02de449a | 94 | }, |
95 | ||
d093074a | 96 | handleMouseEnter(e) { |
97 | this.setState({ active: true }); | |
98 | this.props.onMouseEnter(e); | |
99 | }, | |
100 | ||
101 | handleMouseLeave() { | |
102 | this.setState({ active: false }); | |
103 | }, | |
104 | ||
02de449a | 105 | render() { |
106 | if (this.props.options.length === 0) { | |
107 | return null; | |
108 | } | |
109 | ||
110 | const { prefixCls } = this.props; | |
d093074a | 111 | const cls = classnames({ |
112 | [`${prefixCls}-select`]: 1, | |
113 | [`${prefixCls}-select-active`]: this.state.active, | |
114 | }); | |
02de449a | 115 | |
116 | return ( | |
4984ed85 | 117 | <div |
d093074a | 118 | className={cls} |
119 | onMouseEnter={this.handleMouseEnter} | |
120 | onMouseLeave={this.handleMouseLeave} | |
4984ed85 | 121 | > |
02de449a | 122 | <ul ref="list">{this.getOptions()}</ul> |
123 | </div> | |
124 | ); | |
125 | }, | |
126 | }); | |
127 | ||
128 | export default Select; |