aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/module/Panel.jsx
blob: 4220da86f72a232749f9e9eb56f2dc430748f9dd (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import React, {PropTypes} from 'react';
import classnames from 'classnames';
import GregorianCalendar from 'gregorian-calendar';
import zhCn from 'gregorian-calendar/lib/locale/zh_CN';

import CommonMixin from '../mixin/CommonMixin';
import Header from './Header';
import Combobox from './Combobox';

function noop() {
}

function generateOptions(length) {
  return Array.apply(null, {length: length}).map((item, index) => {
    return index;
  });
}

const Panel = React.createClass({
  propTypes: {
    prefixCls: PropTypes.string,
    defaultValue: PropTypes.object,
    locale: PropTypes.object,
    placeholder: PropTypes.string,
    formatter: PropTypes.object,
    hourOptions: PropTypes.array,
    minuteOptions: PropTypes.array,
    secondOptions: PropTypes.array,
    onChange: PropTypes.func,
    onClear: PropTypes.func,
  },

  mixins: [CommonMixin],

  getDefaultProps() {
    return {
      hourOptions: generateOptions(24),
      minuteOptions: generateOptions(60),
      secondOptions: generateOptions(60),
      onChange: noop,
      onClear: noop,
    };
  },

  getInitialState() {
    let defaultValue = this.props.defaultValue;
    if (!defaultValue) {
      defaultValue = new GregorianCalendar(zhCn);
      defaultValue.setTime(Date.now());
    }
    return {
      value: defaultValue,
    };
  },

  componentWillMount() {
    const formatter = this.props.formatter;
    const pattern = formatter.originalPattern;
    if (pattern === 'HH:mm') {
      this.showSecond = false;
    } else if (pattern === 'mm:ss') {
      this.showHour = false;
    }
  },

  onChange(newValue) {
    this.setState({ value: newValue });
    this.props.onChange(newValue);
  },

  onClear() {
    this.props.onClear();
  },

  getPlaceholder(placeholder) {
    if (placeholder) {
      return placeholder;
    }

    const { locale } = this.props;
    if (!this.showHour) {
      return locale.placeholdermmss;
    } else if (!this.showSecond) {
      return locale.placeholderHHmm;
    }
    return locale.placeholderHHmmss;
  },

  showHour: true,
  showSecond: true,

  render() {
    const { locale, prefixCls, defaultValue, placeholder, hourOptions, minuteOptions, secondOptions } = this.props;
    const value = this.state.value || defaultValue;
    const cls = classnames({ 'narrow': !this.showHour || !this.showSecond });

    return (
      <div className={`${prefixCls}-panel ${cls}`}>
        <Header
          prefixCls={prefixCls}
          gregorianTimePickerLocale={value.locale}
          locale={locale}
          value={value}
          formatter={this.getFormatter()}
          placeholder={this.getPlaceholder(placeholder)}
          hourOptions={hourOptions}
          minuteOptions={minuteOptions}
          secondOptions={secondOptions}
          onChange={this.onChange}
          onClear={this.onClear}
          showClear
        />
        <Combobox
          prefixCls={prefixCls}
          value={value}
          formatter={this.getFormatter()}
          onChange={this.onChange}
          showHour={this.showHour}
          showSecond={this.showSecond}
          hourOptions={hourOptions}
          minuteOptions={minuteOptions}
          secondOptions={secondOptions}
        />
      </div>
    );
  },
});

export default Panel;