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(() => this.refs.input.focus());
45 componentWillReceiveProps(nextProps) {
46 const { value, format } = nextProps;
48 str: value && value.format(format) || '',
53 onInputChange = (event) => {
54 const str = event.target.value;
59 format, hourOptions, minuteOptions, secondOptions,
60 disabledHours, disabledMinutes,
61 disabledSeconds, onChange, allowEmpty,
65 const originalValue = this.props.value;
66 const value = this.getProtoValue().clone();
67 const parsed = moment(str, format, true);
68 if (!parsed.isValid()) {
74 value.hour(parsed.hour()).minute(parsed.minute()).second(parsed.second());
76 // if time value not allowed, response warning.
78 hourOptions.indexOf(value.hour()) < 0 ||
79 minuteOptions.indexOf(value.minute()) < 0 ||
80 secondOptions.indexOf(value.second()) < 0
88 // if time value is disabled, response warning.
89 const disabledHourOptions = disabledHours();
90 const disabledMinuteOptions = disabledMinutes(value.hour());
91 const disabledSecondOptions = disabledSeconds(value.hour(), value.minute());
93 (disabledHourOptions && disabledHourOptions.indexOf(value.hour()) >= 0) ||
94 (disabledMinuteOptions && disabledMinuteOptions.indexOf(value.minute()) >= 0) ||
95 (disabledSecondOptions && disabledSecondOptions.indexOf(value.second()) >= 0)
105 originalValue.hour() !== value.hour() ||
106 originalValue.minute() !== value.minute() ||
107 originalValue.second() !== value.second()
109 // keep other fields for rc-calendar
110 const changedValue = originalValue.clone();
111 changedValue.hour(value.hour());
112 changedValue.minute(value.minute());
113 changedValue.second(value.second());
114 onChange(changedValue);
116 } else if (originalValue !== value) {
119 } else if (allowEmpty) {
134 if (e.keyCode === 27) {
140 this.setState({ str: '' });
141 this.props.onClear();
145 const { prefixCls, allowEmpty } = this.props;
150 className={`${prefixCls}-clear-btn`}
152 title={this.props.clearText}
153 onMouseDown={this.onClear}
158 return this.props.value || this.props.defaultOpenValue;
162 const { prefixCls, placeholder } = this.props;
163 const { invalid, str } = this.state;
164 const invalidClass = invalid ? `${prefixCls}-input-invalid` : '';
167 className={`${prefixCls}-input ${invalidClass}`}
169 onKeyDown={this.onKeyDown}
171 placeholder={placeholder}
172 onChange={this.onInputChange}
178 const { prefixCls } = this.props;
180 <div className={`${prefixCls}-input-wrap`}>
182 {this.getClearButton()}
188 export default Header;