]>
Commit | Line | Data |
---|---|---|
02de449a | 1 | import React, {PropTypes} from 'react'; |
182e9fcc | 2 | import createSelection from '../util/selection'; |
02de449a | 3 | |
4 | const 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, | |
16 | onChange: PropTypes.func, | |
17 | onClear: PropTypes.func, | |
8133e8cf | 18 | onEsc: PropTypes.func, |
19 | allowEmpty: PropTypes.bool, | |
182e9fcc | 20 | currentSelectPanel: PropTypes.string, |
02de449a | 21 | }, |
22 | ||
23 | getInitialState() { | |
24 | const value = this.props.value; | |
25 | return { | |
26 | str: value && this.props.formatter.format(value) || '', | |
27 | invalid: false, | |
28 | }; | |
29 | }, | |
30 | ||
8133e8cf | 31 | componentDidMount() { |
182e9fcc | 32 | this.timer = setTimeout(this.selectRange, 0); |
8133e8cf | 33 | }, |
34 | ||
02de449a | 35 | componentWillReceiveProps(nextProps) { |
78637ac0 | 36 | const value = nextProps.value; |
02de449a | 37 | this.setState({ |
38 | str: value && nextProps.formatter.format(value) || '', | |
39 | invalid: false, | |
40 | }); | |
41 | }, | |
42 | ||
182e9fcc | 43 | componentDidUpdate() { |
44 | this.timer = setTimeout(this.selectRange, 0); | |
45 | }, | |
46 | ||
8133e8cf | 47 | componentWillUnmount() { |
48 | clearTimeout(this.timer); | |
49 | }, | |
50 | ||
02de449a | 51 | onInputChange(event) { |
52 | const str = event.target.value; | |
53 | this.setState({ | |
54 | str, | |
55 | }); | |
56 | let value = null; | |
8133e8cf | 57 | const {formatter, gregorianCalendarLocale, hourOptions, minuteOptions, secondOptions, onChange, allowEmpty} = this.props; |
02de449a | 58 | |
59 | if (str) { | |
60 | const originalValue = this.props.value; | |
61 | try { | |
62 | value = formatter.parse(str, { | |
8133e8cf | 63 | locale: gregorianCalendarLocale, |
02de449a | 64 | obeyCount: true, |
65 | }); | |
02de449a | 66 | } catch (ex) { |
67 | this.setState({ | |
68 | invalid: true, | |
69 | }); | |
70 | return; | |
71 | } | |
72 | ||
73 | if (value) { | |
74 | if ( | |
11b97949 M |
75 | hourOptions.indexOf(value.getHourOfDay()) < 0 || |
76 | minuteOptions.indexOf(value.getMinutes()) < 0 || | |
77 | secondOptions.indexOf(value.getSeconds()) < 0 | |
02de449a | 78 | ) { |
79 | this.setState({ | |
80 | invalid: true, | |
81 | }); | |
82 | return; | |
83 | } | |
84 | ||
85 | if (originalValue && value) { | |
86 | if ( | |
11b97949 M |
87 | originalValue.getHourOfDay() !== value.getHourOfDay() || |
88 | originalValue.getMinutes() !== value.getMinutes() || | |
89 | originalValue.getSeconds() !== value.getSeconds() | |
02de449a | 90 | ) { |
91 | onChange(value); | |
92 | } | |
93 | } else if (originalValue !== value) { | |
94 | onChange(value); | |
95 | } | |
96 | } else { | |
97 | this.setState({ | |
98 | invalid: true, | |
99 | }); | |
100 | return; | |
101 | } | |
8133e8cf | 102 | } else if (allowEmpty) { |
02de449a | 103 | onChange(null); |
8133e8cf | 104 | } else { |
105 | this.setState({ | |
106 | invalid: true, | |
107 | }); | |
108 | return; | |
02de449a | 109 | } |
110 | ||
111 | this.setState({ | |
112 | invalid: false, | |
113 | }); | |
114 | }, | |
115 | ||
8133e8cf | 116 | onKeyDown(e) { |
117 | if (e.keyCode === 27) { | |
118 | this.props.onEsc(); | |
119 | } | |
120 | }, | |
121 | ||
02de449a | 122 | onClear() { |
123 | this.setState({str: ''}); | |
124 | this.props.onClear(); | |
125 | }, | |
126 | ||
127 | getClearButton() { | |
8133e8cf | 128 | const { locale, prefixCls, allowEmpty } = this.props; |
129 | if (!allowEmpty) { | |
02de449a | 130 | return null; |
131 | } | |
8133e8cf | 132 | return <a className={`${prefixCls}-clear-btn`} role="button" title={locale.clear} onMouseDown={this.onClear}/>; |
02de449a | 133 | }, |
134 | ||
135 | getInput() { | |
136 | const { prefixCls, placeholder } = this.props; | |
137 | const { invalid, str } = this.state; | |
138 | const invalidClass = invalid ? `${prefixCls}-input-invalid` : ''; | |
8133e8cf | 139 | return (<input className={`${prefixCls}-input ${invalidClass}`} |
140 | ref="input" | |
141 | onKeyDown={this.onKeyDown} | |
142 | value={str} | |
143 | placeholder={placeholder} onChange={this.onInputChange}/>); | |
02de449a | 144 | }, |
145 | ||
182e9fcc | 146 | selectRange() { |
147 | this.refs.input.focus(); | |
148 | if (this.props.currentSelectPanel && this.refs.input.value) { | |
149 | let selectionRangeStart = 0; | |
150 | let selectionRangeEnd = 0; | |
151 | if (this.props.currentSelectPanel === 'hour') { | |
152 | selectionRangeStart = 0; | |
153 | selectionRangeEnd = this.refs.input.value.indexOf(':'); | |
154 | } else if (this.props.currentSelectPanel === 'minute') { | |
155 | selectionRangeStart = this.refs.input.value.indexOf(':') + 1; | |
156 | selectionRangeEnd = this.refs.input.value.lastIndexOf(':'); | |
157 | } else if (this.props.currentSelectPanel === 'second') { | |
158 | selectionRangeStart = this.refs.input.value.lastIndexOf(':') + 1; | |
159 | selectionRangeEnd = this.refs.input.value.length; | |
160 | } | |
161 | if (selectionRangeEnd - selectionRangeStart === 2) { | |
162 | createSelection(this.refs.input, selectionRangeStart, selectionRangeEnd); | |
163 | } | |
164 | } | |
165 | }, | |
166 | ||
02de449a | 167 | render() { |
168 | const { prefixCls } = this.props; | |
169 | return ( | |
170 | <div className={`${prefixCls}-input-wrap`}> | |
171 | {this.getInput()} | |
172 | {this.getClearButton()} | |
173 | </div> | |
174 | ); | |
175 | }, | |
176 | }); | |
177 | ||
178 | export default Header; |