1 import React, { Component } from 'react';
2 import PropTypes from 'prop-types';
3 import moment from 'moment';
5 class Header extends Component {
7 format: PropTypes.string,
8 prefixCls: PropTypes.string,
9 disabledDate: PropTypes.func,
10 placeholder: PropTypes.string,
11 clearText: PropTypes.string,
12 value: PropTypes.object,
13 hourOptions: PropTypes.array,
14 minuteOptions: PropTypes.array,
15 secondOptions: PropTypes.array,
16 disabledHours: PropTypes.func,
17 disabledMinutes: PropTypes.func,
18 disabledSeconds: PropTypes.func,
19 onChange: PropTypes.func,
20 onClear: PropTypes.func,
21 onEsc: PropTypes.func,
22 allowEmpty: PropTypes.bool,
23 defaultOpenValue: PropTypes.object,
24 currentSelectPanel: PropTypes.string,
25 onKeyDown: PropTypes.func,
30 const { value, format } = props;
32 str: value && value.format(format) || '',
37 componentWillReceiveProps(nextProps) {
38 const { value, format } = nextProps;
40 str: value && value.format(format) || '',
45 onInputChange = (event) => {
46 const str = event.target.value;
51 format, hourOptions, minuteOptions, secondOptions,
52 disabledHours, disabledMinutes,
53 disabledSeconds, onChange, allowEmpty,
57 const originalValue = this.props.value;
58 const value = this.getProtoValue().clone();
59 const parsed = moment(str, format, true);
60 if (!parsed.isValid()) {
66 value.hour(parsed.hour()).minute(parsed.minute()).second(parsed.second());
68 // if time value not allowed, response warning.
70 hourOptions.indexOf(value.hour()) < 0 ||
71 minuteOptions.indexOf(value.minute()) < 0 ||
72 secondOptions.indexOf(value.second()) < 0
80 // if time value is disabled, response warning.
81 const disabledHourOptions = disabledHours();
82 const disabledMinuteOptions = disabledMinutes(value.hour());
83 const disabledSecondOptions = disabledSeconds(value.hour(), value.minute());
85 (disabledHourOptions && disabledHourOptions.indexOf(value.hour()) >= 0) ||
86 (disabledMinuteOptions && disabledMinuteOptions.indexOf(value.minute()) >= 0) ||
87 (disabledSecondOptions && disabledSecondOptions.indexOf(value.second()) >= 0)
97 originalValue.hour() !== value.hour() ||
98 originalValue.minute() !== value.minute() ||
99 originalValue.second() !== value.second()
101 // keep other fields for rc-calendar
102 const changedValue = originalValue.clone();
103 changedValue.hour(value.hour());
104 changedValue.minute(value.minute());
105 changedValue.second(value.second());
106 onChange(changedValue);
108 } else if (originalValue !== value) {
111 } else if (allowEmpty) {
126 const { onEsc, onKeyDown } = this.props;
127 if (e.keyCode === 27) {
135 this.setState({ str: '' });
136 this.props.onClear();
140 const { prefixCls, allowEmpty } = this.props;
145 className={`${prefixCls}-clear-btn`}
147 title={this.props.clearText}
148 onMouseDown={this.onClear}
153 return this.props.value || this.props.defaultOpenValue;
157 const { prefixCls, placeholder } = this.props;
158 const { invalid, str } = this.state;
159 const invalidClass = invalid ? `${prefixCls}-input-invalid` : '';
162 className={`${prefixCls}-input ${invalidClass}`}
164 onKeyDown={this.onKeyDown}
166 placeholder={placeholder}
167 onChange={this.onInputChange}
173 const { prefixCls } = this.props;
175 <div className={`${prefixCls}-input-wrap`}>
177 {this.getClearButton()}
183 export default Header;