aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/module/Select.jsx
blob: 2b69623e278e7a22998cd27f55c7e6dd6038ba0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import React, {PropTypes} from 'react';
import ReactDom from 'react-dom';
import classnames from 'classnames';

const scrollTo = (element, to, duration) => {
  // jump to target if duration zero
  if (duration <= 0) {
    element.scrollTop = to;
    return;
  }
  const difference = to - element.scrollTop;
  const perTick = difference / duration * 10;

  setTimeout(() => {
    element.scrollTop = element.scrollTop + perTick;
    if (element.scrollTop === to) return;
    scrollTo(element, to, duration - 10);
  }, 10);
};

const Select = React.createClass({
  propTypes: {
    prefixCls: PropTypes.string,
    options: PropTypes.array,
    selectedIndex: PropTypes.number,
    type: PropTypes.string,
    onSelect: PropTypes.func,
  },

  componentDidMount() {
    // jump to selected option
    this.scrollToSelected(0);
  },

  componentDidUpdate() {
    // smooth scroll to selected option
    this.scrollToSelected(200);
  },

  onSelect(event) {
    // do nothing when select selected option
    if (event.target.getAttribute('class') === 'selected') {
      return;
    }
    // change combobox selection
    const { onSelect, type } = this.props;
    const value = parseInt(event.target.innerHTML, 10);
    onSelect(type, value);
  },

  getOptions() {
    const { options, selectedIndex } = this.props;
    return options.map((item, index) => {
      const cls = classnames({ selected: selectedIndex === index});
      const ref = selectedIndex === index ? 'selected' : null;
      return <li ref={ref} className={cls} key={index} onClick={this.onSelect}>{item}</li>;
    });
  },

  scrollToSelected(duration) {
    // move to selected item
    const select = ReactDom.findDOMNode(this);
    const list = ReactDom.findDOMNode(this.refs.list);
    let index = this.props.selectedIndex - 2;
    if (index < 0) {
      index = 0;
    }
    const topOption = list.children[index];
    const to = topOption.offsetTop - select.offsetTop;
    scrollTo(select, to, duration);
  },

  render() {
    if (this.props.options.length === 0) {
      return null;
    }

    const { prefixCls } = this.props;

    return (
      <div className={`${prefixCls}-select`}>
        <ul ref="list">{this.getOptions()}</ul>
      </div>
    );
  },
});

export default Select;