1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
import React, { PropTypes } from 'react';
import moment from 'moment';
const Header = React.createClass({
propTypes: {
format: PropTypes.string,
prefixCls: PropTypes.string,
disabledDate: PropTypes.func,
placeholder: PropTypes.string,
clearText: PropTypes.string,
value: PropTypes.object,
hourOptions: PropTypes.array,
minuteOptions: PropTypes.array,
secondOptions: PropTypes.array,
disabledHours: PropTypes.func,
disabledMinutes: PropTypes.func,
disabledSeconds: PropTypes.func,
onChange: PropTypes.func,
onClear: PropTypes.func,
onEsc: PropTypes.func,
allowEmpty: PropTypes.bool,
defaultOpenValue: PropTypes.object,
currentSelectPanel: PropTypes.string,
},
getInitialState() {
const { value, format } = this.props;
return {
str: value && value.format(format) || '',
invalid: false,
};
},
componentWillReceiveProps(nextProps) {
const { value, format } = nextProps;
this.setState({
str: value && value.format(format) || '',
invalid: false,
});
},
onInputChange(event) {
const str = event.target.value;
this.setState({
str,
});
const {
format, hourOptions, minuteOptions, secondOptions,
disabledHours, disabledMinutes,
disabledSeconds, onChange, allowEmpty,
} = this.props;
if (str) {
const originalValue = this.props.value;
const value = this.getProtoValue().clone();
const parsed = moment(str, format, true);
if (!parsed.isValid()) {
this.setState({
invalid: true,
});
return;
}
value.hour(parsed.hour()).minute(parsed.minute()).second(parsed.second());
// if time value not allowed, response warning.
if (
hourOptions.indexOf(value.hour()) < 0 ||
minuteOptions.indexOf(value.minute()) < 0 ||
secondOptions.indexOf(value.second()) < 0
) {
this.setState({
invalid: true,
});
return;
}
// if time value is disabled, response warning.
const disabledHourOptions = disabledHours();
const disabledMinuteOptions = disabledMinutes(value.hour());
const disabledSecondOptions = disabledSeconds(value.hour(), value.minute());
if (
(disabledHourOptions && disabledHourOptions.indexOf(value.hour()) >= 0) ||
(disabledMinuteOptions && disabledMinuteOptions.indexOf(value.minute()) >= 0) ||
(disabledSecondOptions && disabledSecondOptions.indexOf(value.second()) >= 0)
) {
this.setState({
invalid: true,
});
return;
}
if (originalValue) {
if (
originalValue.hour() !== value.hour() ||
originalValue.minute() !== value.minute() ||
originalValue.second() !== value.second()
) {
// keep other fields for rc-calendar
const changedValue = originalValue.clone();
changedValue.hour(value.hour());
changedValue.minute(value.minute());
changedValue.second(value.second());
onChange(changedValue);
}
} else if (originalValue !== value) {
onChange(value);
}
} else if (allowEmpty) {
onChange(null);
} else {
this.setState({
invalid: true,
});
return;
}
this.setState({
invalid: false,
});
},
onKeyDown(e) {
if (e.keyCode === 27) {
this.props.onEsc();
}
},
onClear() {
this.setState({ str: '' });
this.props.onClear();
},
getClearButton() {
const { prefixCls, allowEmpty } = this.props;
if (!allowEmpty) {
return null;
}
return (<a
className={`${prefixCls}-clear-btn`}
role="button"
title={this.props.clearText}
onMouseDown={this.onClear}
/>);
},
getProtoValue() {
return this.props.value || this.props.defaultOpenValue;
},
getInput() {
const { prefixCls, placeholder } = this.props;
const { invalid, str } = this.state;
const invalidClass = invalid ? `${prefixCls}-input-invalid` : '';
return (<input
className={`${prefixCls}-input ${invalidClass}`}
ref="input"
onKeyDown={this.onKeyDown}
value={str}
placeholder={placeholder}
onChange={this.onInputChange}
/>);
},
render() {
const { prefixCls } = this.props;
return (
<div className={`${prefixCls}-input-wrap`}>
{this.getInput()}
{this.getClearButton()}
</div>
);
},
});
export default Header;
|