1 import React, { PropTypes } from 'react';
2 import moment from 'moment';
4 const Header = React.createClass({
6 format: PropTypes.string,
7 prefixCls: PropTypes.string,
8 disabledDate: PropTypes.func,
9 placeholder: PropTypes.string,
10 clearText: PropTypes.string,
11 value: PropTypes.object,
12 hourOptions: PropTypes.array,
13 minuteOptions: PropTypes.array,
14 secondOptions: PropTypes.array,
15 disabledHours: PropTypes.func,
16 disabledMinutes: PropTypes.func,
17 disabledSeconds: PropTypes.func,
18 onChange: PropTypes.func,
19 onClear: PropTypes.func,
20 onEsc: PropTypes.func,
21 allowEmpty: PropTypes.bool,
22 defaultOpenValue: PropTypes.object,
23 currentSelectPanel: PropTypes.string,
27 const { value, format } = this.props;
29 str: value && value.format(format) || '',
34 componentWillReceiveProps(nextProps) {
35 const { value, format } = nextProps;
37 str: value && value.format(format) || '',
42 onInputChange(event) {
43 const str = event.target.value;
48 format, hourOptions, minuteOptions, secondOptions,
49 disabledHours, disabledMinutes,
50 disabledSeconds, onChange, allowEmpty,
54 const originalValue = this.props.value;
55 const value = this.getProtoValue().clone();
56 const parsed = moment(str, format, true);
57 if (!parsed.isValid()) {
63 value.hour(parsed.hour()).minute(parsed.minute()).second(parsed.second());
65 // if time value not allowed, response warning.
67 hourOptions.indexOf(value.hour()) < 0 ||
68 minuteOptions.indexOf(value.minute()) < 0 ||
69 secondOptions.indexOf(value.second()) < 0
77 // if time value is disabled, response warning.
78 const disabledHourOptions = disabledHours();
79 const disabledMinuteOptions = disabledMinutes(value.hour());
80 const disabledSecondOptions = disabledSeconds(value.hour(), value.minute());
82 (disabledHourOptions && disabledHourOptions.indexOf(value.hour()) >= 0) ||
83 (disabledMinuteOptions && disabledMinuteOptions.indexOf(value.minute()) >= 0) ||
84 (disabledSecondOptions && disabledSecondOptions.indexOf(value.second()) >= 0)
94 originalValue.hour() !== value.hour() ||
95 originalValue.minute() !== value.minute() ||
96 originalValue.second() !== value.second()
98 // keep other fields for rc-calendar
99 const changedValue = originalValue.clone();
100 changedValue.hour(value.hour());
101 changedValue.minute(value.minute());
102 changedValue.second(value.second());
103 onChange(changedValue);
105 } else if (originalValue !== value) {
108 } else if (allowEmpty) {
123 if (e.keyCode === 27) {
129 this.setState({ str: '' });
130 this.props.onClear();
134 const { prefixCls, allowEmpty } = this.props;
139 className={`${prefixCls}-clear-btn`}
141 title={this.props.clearText}
142 onMouseDown={this.onClear}
147 return this.props.value || this.props.defaultOpenValue;
151 const { prefixCls, placeholder } = this.props;
152 const { invalid, str } = this.state;
153 const invalidClass = invalid ? `${prefixCls}-input-invalid` : '';
155 className={`${prefixCls}-input ${invalidClass}`}
157 onKeyDown={this.onKeyDown}
159 placeholder={placeholder}
160 onChange={this.onInputChange}
165 const { prefixCls } = this.props;
167 <div className={`${prefixCls}-input-wrap`}>
169 {this.getClearButton()}
175 export default Header;