]> git.immae.eu Git - github/fretlink/time-picker.git/blame - src/module/Header.jsx
update test case and fix bugs
[github/fretlink/time-picker.git] / src / module / Header.jsx
CommitLineData
02de449a 1import React, {PropTypes} from 'react';
182e9fcc 2import createSelection from '../util/selection';
02de449a 3
4const Header = React.createClass({
5 propTypes: {
6 formatter: PropTypes.object,
7 prefixCls: PropTypes.string,
8133e8cf 8 gregorianCalendarLocale: PropTypes.object,
02de449a 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,
518b852e
M
16 disabledHours: PropTypes.array,
17 disabledMinutes: PropTypes.array,
18 disabledSeconds: PropTypes.array,
02de449a 19 onChange: PropTypes.func,
20 onClear: PropTypes.func,
8133e8cf 21 onEsc: PropTypes.func,
22 allowEmpty: PropTypes.bool,
182e9fcc 23 currentSelectPanel: PropTypes.string,
02de449a 24 },
25
26 getInitialState() {
27 const value = this.props.value;
28 return {
29 str: value && this.props.formatter.format(value) || '',
30 invalid: false,
31 };
32 },
33
8133e8cf 34 componentDidMount() {
182e9fcc 35 this.timer = setTimeout(this.selectRange, 0);
8133e8cf 36 },
37
02de449a 38 componentWillReceiveProps(nextProps) {
78637ac0 39 const value = nextProps.value;
02de449a 40 this.setState({
41 str: value && nextProps.formatter.format(value) || '',
42 invalid: false,
43 });
44 },
45
182e9fcc 46 componentDidUpdate() {
47 this.timer = setTimeout(this.selectRange, 0);
48 },
49
8133e8cf 50 componentWillUnmount() {
51 clearTimeout(this.timer);
52 },
53
02de449a 54 onInputChange(event) {
55 const str = event.target.value;
56 this.setState({
57 str,
58 });
59 let value = null;
518b852e 60 const {formatter, gregorianCalendarLocale, hourOptions, minuteOptions, secondOptions, disabledHours, disabledMinutes, disabledSeconds, onChange, allowEmpty} = this.props;
02de449a 61
62 if (str) {
63 const originalValue = this.props.value;
64 try {
65 value = formatter.parse(str, {
8133e8cf 66 locale: gregorianCalendarLocale,
02de449a 67 obeyCount: true,
68 });
02de449a 69 } catch (ex) {
70 this.setState({
71 invalid: true,
72 });
73 return;
74 }
75
76 if (value) {
518b852e 77 // if time value not allowed, response warning.
02de449a 78 if (
11b97949
M
79 hourOptions.indexOf(value.getHourOfDay()) < 0 ||
80 minuteOptions.indexOf(value.getMinutes()) < 0 ||
81 secondOptions.indexOf(value.getSeconds()) < 0
02de449a 82 ) {
83 this.setState({
84 invalid: true,
85 });
86 return;
87 }
88
518b852e
M
89 // if time value is disabled, response warning.
90 if (
0e62bf0b
M
91 (disabledHours && disabledHours.indexOf(value.getHourOfDay()) >= 0) ||
92 (disabledMinutes && disabledMinutes.indexOf(value.getMinutes()) >= 0) ||
93 (disabledSeconds && disabledSeconds.indexOf(value.getSeconds()) >= 0)
518b852e
M
94 ) {
95 this.setState({
96 invalid: true,
97 });
98 return;
99 }
100
02de449a 101 if (originalValue && value) {
102 if (
11b97949
M
103 originalValue.getHourOfDay() !== value.getHourOfDay() ||
104 originalValue.getMinutes() !== value.getMinutes() ||
105 originalValue.getSeconds() !== value.getSeconds()
02de449a 106 ) {
107 onChange(value);
108 }
109 } else if (originalValue !== value) {
110 onChange(value);
111 }
112 } else {
113 this.setState({
114 invalid: true,
115 });
116 return;
117 }
8133e8cf 118 } else if (allowEmpty) {
02de449a 119 onChange(null);
8133e8cf 120 } else {
121 this.setState({
122 invalid: true,
123 });
124 return;
02de449a 125 }
126
127 this.setState({
128 invalid: false,
129 });
130 },
131
8133e8cf 132 onKeyDown(e) {
133 if (e.keyCode === 27) {
134 this.props.onEsc();
135 }
136 },
137
02de449a 138 onClear() {
139 this.setState({str: ''});
140 this.props.onClear();
141 },
142
143 getClearButton() {
8133e8cf 144 const { locale, prefixCls, allowEmpty } = this.props;
145 if (!allowEmpty) {
02de449a 146 return null;
147 }
8133e8cf 148 return <a className={`${prefixCls}-clear-btn`} role="button" title={locale.clear} onMouseDown={this.onClear}/>;
02de449a 149 },
150
151 getInput() {
152 const { prefixCls, placeholder } = this.props;
153 const { invalid, str } = this.state;
154 const invalidClass = invalid ? `${prefixCls}-input-invalid` : '';
8133e8cf 155 return (<input className={`${prefixCls}-input ${invalidClass}`}
156 ref="input"
157 onKeyDown={this.onKeyDown}
158 value={str}
159 placeholder={placeholder} onChange={this.onInputChange}/>);
02de449a 160 },
161
182e9fcc 162 selectRange() {
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;
176 }
177 if (selectionRangeEnd - selectionRangeStart === 2) {
178 createSelection(this.refs.input, selectionRangeStart, selectionRangeEnd);
179 }
180 }
181 },
182
02de449a 183 render() {
184 const { prefixCls } = this.props;
185 return (
186 <div className={`${prefixCls}-input-wrap`}>
187 {this.getInput()}
188 {this.getClearButton()}
189 </div>
190 );
191 },
192});
193
194export default Header;