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 focusOnOpen: PropTypes.bool,
30 const { value, format } = props;
32 str: value && value.format(format) || '',
38 if (this.props.focusOnOpen) {
39 // Wait one frame for the panel to be positioned before focusing
40 const requestAnimationFrame = (window.requestAnimationFrame || window.setTimeout);
41 requestAnimationFrame(() => {
42 this.refs.input.focus();
43 this.refs.input.select();
48 componentWillReceiveProps(nextProps) {
49 const { value, format } = nextProps;
51 str: value && value.format(format) || '',
56 onInputChange = (event) => {
57 const str = event.target.value;
62 format, hourOptions, minuteOptions, secondOptions,
63 disabledHours, disabledMinutes,
64 disabledSeconds, onChange, allowEmpty,
68 const originalValue = this.props.value;
69 const value = this.getProtoValue().clone();
70 const parsed = moment(str, format, true);
71 if (!parsed.isValid()) {
77 value.hour(parsed.hour()).minute(parsed.minute()).second(parsed.second());
79 // if time value not allowed, response warning.
81 hourOptions.indexOf(value.hour()) < 0 ||
82 minuteOptions.indexOf(value.minute()) < 0 ||
83 secondOptions.indexOf(value.second()) < 0
91 // if time value is disabled, response warning.
92 const disabledHourOptions = disabledHours();
93 const disabledMinuteOptions = disabledMinutes(value.hour());
94 const disabledSecondOptions = disabledSeconds(value.hour(), value.minute());
96 (disabledHourOptions && disabledHourOptions.indexOf(value.hour()) >= 0) ||
97 (disabledMinuteOptions && disabledMinuteOptions.indexOf(value.minute()) >= 0) ||
98 (disabledSecondOptions && disabledSecondOptions.indexOf(value.second()) >= 0)
108 originalValue.hour() !== value.hour() ||
109 originalValue.minute() !== value.minute() ||
110 originalValue.second() !== value.second()
112 // keep other fields for rc-calendar
113 const changedValue = originalValue.clone();
114 changedValue.hour(value.hour());
115 changedValue.minute(value.minute());
116 changedValue.second(value.second());
117 onChange(changedValue);
119 } else if (originalValue !== value) {
122 } else if (allowEmpty) {
137 if (e.keyCode === 27) {
143 this.setState({ str: '' });
144 this.props.onClear();
148 const { prefixCls, allowEmpty } = this.props;
153 className={`${prefixCls}-clear-btn`}
155 title={this.props.clearText}
156 onMouseDown={this.onClear}
161 return this.props.value || this.props.defaultOpenValue;
165 const { prefixCls, placeholder } = this.props;
166 const { invalid, str } = this.state;
167 const invalidClass = invalid ? `${prefixCls}-input-invalid` : '';
170 className={`${prefixCls}-input ${invalidClass}`}
172 onKeyDown={this.onKeyDown}
174 placeholder={placeholder}
175 onChange={this.onInputChange}
181 const { prefixCls } = this.props;
183 <div className={`${prefixCls}-input-wrap`}>
185 {this.getClearButton()}
191 export default Header;