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,
29 const { value, format } = props;
31 str: value && value.format(format) || '',
36 componentWillReceiveProps(nextProps) {
37 const { value, format } = nextProps;
39 str: value && value.format(format) || '',
44 onInputChange = (event) => {
45 const str = event.target.value;
50 format, hourOptions, minuteOptions, secondOptions,
51 disabledHours, disabledMinutes,
52 disabledSeconds, onChange, allowEmpty,
56 const originalValue = this.props.value;
57 const value = this.getProtoValue().clone();
58 const parsed = moment(str, format, true);
59 if (!parsed.isValid()) {
65 value.hour(parsed.hour()).minute(parsed.minute()).second(parsed.second());
67 // if time value not allowed, response warning.
69 hourOptions.indexOf(value.hour()) < 0 ||
70 minuteOptions.indexOf(value.minute()) < 0 ||
71 secondOptions.indexOf(value.second()) < 0
79 // if time value is disabled, response warning.
80 const disabledHourOptions = disabledHours();
81 const disabledMinuteOptions = disabledMinutes(value.hour());
82 const disabledSecondOptions = disabledSeconds(value.hour(), value.minute());
84 (disabledHourOptions && disabledHourOptions.indexOf(value.hour()) >= 0) ||
85 (disabledMinuteOptions && disabledMinuteOptions.indexOf(value.minute()) >= 0) ||
86 (disabledSecondOptions && disabledSecondOptions.indexOf(value.second()) >= 0)
96 originalValue.hour() !== value.hour() ||
97 originalValue.minute() !== value.minute() ||
98 originalValue.second() !== value.second()
100 // keep other fields for rc-calendar
101 const changedValue = originalValue.clone();
102 changedValue.hour(value.hour());
103 changedValue.minute(value.minute());
104 changedValue.second(value.second());
105 onChange(changedValue);
107 } else if (originalValue !== value) {
110 } else if (allowEmpty) {
125 if (e.keyCode === 27) {
131 this.setState({ str: '' });
132 this.props.onClear();
136 const { prefixCls, allowEmpty } = this.props;
141 className={`${prefixCls}-clear-btn`}
143 title={this.props.clearText}
144 onMouseDown={this.onClear}
149 return this.props.value || this.props.defaultOpenValue;
153 const { prefixCls, placeholder } = this.props;
154 const { invalid, str } = this.state;
155 const invalidClass = invalid ? `${prefixCls}-input-invalid` : '';
158 className={`${prefixCls}-input ${invalidClass}`}
160 onKeyDown={this.onKeyDown}
162 placeholder={placeholder}
163 onChange={this.onInputChange}
169 const { prefixCls } = this.props;
171 <div className={`${prefixCls}-input-wrap`}>
173 {this.getClearButton()}
179 export default Header;