1 import React, {PropTypes} from 'react';
2 import createSelection from '../util/selection';
4 const Header = React.createClass({
6 formatter: PropTypes.object,
7 prefixCls: PropTypes.string,
8 gregorianCalendarLocale: PropTypes.object,
9 locale: PropTypes.object,
10 disabledDate: PropTypes.func,
11 placeholder: PropTypes.string,
12 value: PropTypes.object,
13 hourOptions: PropTypes.array,
14 minuteOptions: PropTypes.array,
15 secondOptions: PropTypes.array,
16 disabledHours: PropTypes.array,
17 disabledMinutes: PropTypes.array,
18 disabledSeconds: PropTypes.array,
19 onChange: PropTypes.func,
20 onClear: PropTypes.func,
21 onEsc: PropTypes.func,
22 allowEmpty: PropTypes.bool,
23 currentSelectPanel: PropTypes.string,
27 const value = this.props.value;
29 str: value && this.props.formatter.format(value) || '',
35 this.timer = setTimeout(this.selectRange, 0);
38 componentWillReceiveProps(nextProps) {
39 const value = nextProps.value;
41 str: value && nextProps.formatter.format(value) || '',
46 componentDidUpdate() {
47 this.timer = setTimeout(this.selectRange, 0);
50 componentWillUnmount() {
51 clearTimeout(this.timer);
54 onInputChange(event) {
55 const str = event.target.value;
60 const {formatter, gregorianCalendarLocale, hourOptions, minuteOptions, secondOptions, disabledHours, disabledMinutes, disabledSeconds, onChange, allowEmpty} = this.props;
63 const originalValue = this.props.value;
65 value = formatter.parse(str, {
66 locale: gregorianCalendarLocale,
77 // if time value not allowed, response warning.
79 hourOptions.indexOf(value.getHourOfDay()) < 0 ||
80 minuteOptions.indexOf(value.getMinutes()) < 0 ||
81 secondOptions.indexOf(value.getSeconds()) < 0
89 // if time value is disabled, response warning.
91 disabledHours.indexOf(value.getHourOfDay()) >= 0 ||
92 disabledMinutes.indexOf(value.getMinutes()) >= 0 ||
93 disabledSeconds.indexOf(value.getSeconds()) >= 0
101 if (originalValue && value) {
103 originalValue.getHourOfDay() !== value.getHourOfDay() ||
104 originalValue.getMinutes() !== value.getMinutes() ||
105 originalValue.getSeconds() !== value.getSeconds()
109 } else if (originalValue !== value) {
118 } else if (allowEmpty) {
133 if (e.keyCode === 27) {
139 this.setState({str: ''});
140 this.props.onClear();
144 const { locale, prefixCls, allowEmpty } = this.props;
148 return <a className={`${prefixCls}-clear-btn`} role="button" title={locale.clear} onMouseDown={this.onClear}/>;
152 const { prefixCls, placeholder } = this.props;
153 const { invalid, str } = this.state;
154 const invalidClass = invalid ? `${prefixCls}-input-invalid` : '';
155 return (<input className={`${prefixCls}-input ${invalidClass}`}
157 onKeyDown={this.onKeyDown}
159 placeholder={placeholder} onChange={this.onInputChange}/>);
163 this.refs.input.focus();
164 if (this.props.currentSelectPanel && this.refs.input.value) {
165 let selectionRangeStart = 0;
166 let selectionRangeEnd = 0;
167 if (this.props.currentSelectPanel === 'hour') {
168 selectionRangeStart = 0;
169 selectionRangeEnd = this.refs.input.value.indexOf(':');
170 } else if (this.props.currentSelectPanel === 'minute') {
171 selectionRangeStart = this.refs.input.value.indexOf(':') + 1;
172 selectionRangeEnd = this.refs.input.value.lastIndexOf(':');
173 } else if (this.props.currentSelectPanel === 'second') {
174 selectionRangeStart = this.refs.input.value.lastIndexOf(':') + 1;
175 selectionRangeEnd = this.refs.input.value.length;
177 if (selectionRangeEnd - selectionRangeStart === 2) {
178 createSelection(this.refs.input, selectionRangeStart, selectionRangeEnd);
184 const { prefixCls } = this.props;
186 <div className={`${prefixCls}-input-wrap`}>
188 {this.getClearButton()}
194 export default Header;