]>
Commit | Line | Data |
---|---|---|
3ab3a128 | 1 | import React, { Component } from 'react'; |
2 | import PropTypes from 'prop-types'; | |
02de449a | 3 | import Trigger from 'rc-trigger'; |
4984ed85 | 4 | import Panel from './Panel'; |
5 | import placements from './placements'; | |
6 | import moment from 'moment'; | |
02de449a | 7 | |
8 | function noop() { | |
9 | } | |
10 | ||
11 | function refFn(field, component) { | |
12 | this[field] = component; | |
13 | } | |
14 | ||
cfe8e36c | 15 | export default class Picker extends Component { |
3ab3a128 | 16 | static propTypes = { |
02de449a | 17 | prefixCls: PropTypes.string, |
4984ed85 | 18 | clearText: PropTypes.string, |
9f9f39e4 | 19 | value: PropTypes.object, |
4984ed85 | 20 | defaultOpenValue: PropTypes.object, |
4e70ea0d | 21 | inputReadOnly: PropTypes.bool, |
02de449a | 22 | disabled: PropTypes.bool, |
8133e8cf | 23 | allowEmpty: PropTypes.bool, |
63541ed7 | 24 | defaultValue: PropTypes.object, |
02de449a | 25 | open: PropTypes.bool, |
8133e8cf | 26 | defaultOpen: PropTypes.bool, |
02de449a | 27 | align: PropTypes.object, |
28 | placement: PropTypes.any, | |
29 | transitionName: PropTypes.string, | |
8133e8cf | 30 | getPopupContainer: PropTypes.func, |
4acbf95c | 31 | placeholder: PropTypes.string, |
4984ed85 | 32 | format: PropTypes.string, |
8133e8cf | 33 | showHour: PropTypes.bool, |
37c36c09 | 34 | showMinute: PropTypes.bool, |
3b3350d5 | 35 | showSecond: PropTypes.bool, |
8133e8cf | 36 | style: PropTypes.object, |
96d366af | 37 | className: PropTypes.string, |
a7c6e7bb | 38 | popupClassName: PropTypes.string, |
71bd9bc1 M |
39 | disabledHours: PropTypes.func, |
40 | disabledMinutes: PropTypes.func, | |
41 | disabledSeconds: PropTypes.func, | |
518b852e | 42 | hideDisabledOptions: PropTypes.bool, |
02de449a | 43 | onChange: PropTypes.func, |
44 | onOpen: PropTypes.func, | |
45 | onClose: PropTypes.func, | |
e30387ab CS |
46 | onFocus: PropTypes.func, |
47 | onBlur: PropTypes.func, | |
b86fe1d1 | 48 | addon: PropTypes.func, |
6d1ad104 | 49 | name: PropTypes.string, |
73e6cf78 | 50 | autoComplete: PropTypes.string, |
dd275f7d | 51 | use12Hours: PropTypes.bool, |
5827568e MP |
52 | hourStep: PropTypes.number, |
53 | minuteStep: PropTypes.number, | |
54 | secondStep: PropTypes.number, | |
d18ecfb6 | 55 | focusOnOpen: PropTypes.bool, |
0e4fd162 | 56 | onKeyDown: PropTypes.func, |
d9a9b691 | 57 | autoFocus: PropTypes.bool, |
3ab3a128 | 58 | }; |
59 | ||
60 | static defaultProps = { | |
61 | clearText: 'clear', | |
62 | prefixCls: 'rc-time-picker', | |
63 | defaultOpen: false, | |
4e70ea0d | 64 | inputReadOnly: false, |
3ab3a128 | 65 | style: {}, |
66 | className: '', | |
a7c6e7bb | 67 | popupClassName: '', |
3ab3a128 | 68 | align: {}, |
69 | defaultOpenValue: moment(), | |
70 | allowEmpty: true, | |
71 | showHour: true, | |
72 | showMinute: true, | |
73 | showSecond: true, | |
74 | disabledHours: noop, | |
75 | disabledMinutes: noop, | |
76 | disabledSeconds: noop, | |
77 | hideDisabledOptions: false, | |
78 | placement: 'bottomLeft', | |
79 | onChange: noop, | |
80 | onOpen: noop, | |
81 | onClose: noop, | |
e30387ab CS |
82 | onFocus: noop, |
83 | onBlur: noop, | |
3ab3a128 | 84 | addon: noop, |
85 | use12Hours: false, | |
d18ecfb6 | 86 | focusOnOpen: false, |
0e4fd162 | 87 | onKeyDown: noop, |
3ab3a128 | 88 | }; |
89 | ||
90 | constructor(props) { | |
91 | super(props); | |
fa912931 | 92 | this.saveInputRef = refFn.bind(this, 'picker'); |
02de449a | 93 | this.savePanelRef = refFn.bind(this, 'panelInstance'); |
3ab3a128 | 94 | const { defaultOpen, defaultValue, open = defaultOpen, value = defaultValue } = props; |
95 | this.state = { | |
8133e8cf | 96 | open, |
97 | value, | |
e75ed0c6 | 98 | }; |
3ab3a128 | 99 | } |
02de449a | 100 | |
02de449a | 101 | componentWillReceiveProps(nextProps) { |
9f9f39e4 | 102 | const { value, open } = nextProps; |
6fc4e0e8 | 103 | if ('value' in nextProps) { |
9f9f39e4 | 104 | this.setState({ |
105 | value, | |
106 | }); | |
02de449a | 107 | } |
108 | if (open !== undefined) { | |
1882f74d | 109 | this.setState({ open }); |
02de449a | 110 | } |
3ab3a128 | 111 | } |
02de449a | 112 | |
3ab3a128 | 113 | onPanelChange = (value) => { |
9f9f39e4 | 114 | this.setValue(value); |
3ab3a128 | 115 | } |
02de449a | 116 | |
3ab3a128 | 117 | onPanelClear = () => { |
7702bb67 | 118 | this.setValue(null); |
63541ed7 | 119 | this.setOpen(false); |
3ab3a128 | 120 | } |
02de449a | 121 | |
3ab3a128 | 122 | onVisibleChange = (open) => { |
8133e8cf | 123 | this.setOpen(open); |
3ab3a128 | 124 | } |
8133e8cf | 125 | |
3ab3a128 | 126 | onEsc = () => { |
8133e8cf | 127 | this.setOpen(false); |
ed849606 | 128 | this.focus(); |
3ab3a128 | 129 | } |
8133e8cf | 130 | |
3ab3a128 | 131 | onKeyDown = (e) => { |
8133e8cf | 132 | if (e.keyCode === 40) { |
133 | this.setOpen(true); | |
134 | } | |
3ab3a128 | 135 | } |
02de449a | 136 | |
9f9f39e4 | 137 | setValue(value) { |
138 | if (!('value' in this.props)) { | |
139 | this.setState({ | |
140 | value, | |
141 | }); | |
142 | } | |
143 | this.props.onChange(value); | |
3ab3a128 | 144 | } |
9f9f39e4 | 145 | |
4984ed85 | 146 | getFormat() { |
dd2f6abd | 147 | const { format, showHour, showMinute, showSecond, use12Hours } = this.props; |
4984ed85 | 148 | if (format) { |
149 | return format; | |
8133e8cf | 150 | } |
dd2f6abd AS |
151 | |
152 | if (use12Hours) { | |
153 | const fmtString = ([ | |
154 | showHour ? 'h' : '', | |
155 | showMinute ? 'mm' : '', | |
156 | showSecond ? 'ss' : '', | |
157 | ].filter(item => !!item).join(':')); | |
158 | ||
159 | return fmtString.concat(' a'); | |
160 | } | |
161 | ||
37c36c09 | 162 | return [ |
163 | showHour ? 'HH' : '', | |
164 | showMinute ? 'mm' : '', | |
165 | showSecond ? 'ss' : '', | |
166 | ].filter(item => !!item).join(':'); | |
3ab3a128 | 167 | } |
8133e8cf | 168 | |
169 | getPanelElement() { | |
1882f74d | 170 | const { |
4984ed85 | 171 | prefixCls, placeholder, disabledHours, |
4e70ea0d | 172 | disabledMinutes, disabledSeconds, hideDisabledOptions, inputReadOnly, |
37c36c09 | 173 | allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText, |
f0ac29c6 | 174 | addon, use12Hours, focusOnOpen, onKeyDown, hourStep, minuteStep, secondStep, |
1882f74d | 175 | } = this.props; |
4acbf95c M |
176 | return ( |
177 | <Panel | |
4984ed85 | 178 | clearText={clearText} |
8133e8cf | 179 | prefixCls={`${prefixCls}-panel`} |
180 | ref={this.savePanelRef} | |
181 | value={this.state.value} | |
4e70ea0d | 182 | inputReadOnly={inputReadOnly} |
8133e8cf | 183 | onChange={this.onPanelChange} |
8133e8cf | 184 | onClear={this.onPanelClear} |
4984ed85 | 185 | defaultOpenValue={defaultOpenValue} |
8133e8cf | 186 | showHour={showHour} |
37c36c09 | 187 | showMinute={showMinute} |
8133e8cf | 188 | showSecond={showSecond} |
3b3350d5 | 189 | onEsc={this.onEsc} |
8133e8cf | 190 | allowEmpty={allowEmpty} |
4984ed85 | 191 | format={this.getFormat()} |
4acbf95c | 192 | placeholder={placeholder} |
518b852e M |
193 | disabledHours={disabledHours} |
194 | disabledMinutes={disabledMinutes} | |
195 | disabledSeconds={disabledSeconds} | |
196 | hideDisabledOptions={hideDisabledOptions} | |
dd275f7d | 197 | use12Hours={use12Hours} |
5827568e MP |
198 | hourStep={hourStep} |
199 | minuteStep={minuteStep} | |
200 | secondStep={secondStep} | |
b86fe1d1 | 201 | addon={addon} |
d18ecfb6 | 202 | focusOnOpen={focusOnOpen} |
0e4fd162 | 203 | onKeyDown={onKeyDown} |
4acbf95c M |
204 | /> |
205 | ); | |
3ab3a128 | 206 | } |
4acbf95c | 207 | |
a7c6e7bb | 208 | getPopupClassName() { |
209 | const { showHour, showMinute, showSecond, use12Hours, prefixCls } = this.props; | |
210 | let popupClassName = this.props.popupClassName; | |
211 | // Keep it for old compatibility | |
212 | if ((!showHour || !showMinute || !showSecond) && !use12Hours) { | |
213 | popupClassName += ` ${prefixCls}-panel-narrow`; | |
214 | } | |
215 | let selectColumnCount = 0; | |
216 | if (showHour) { | |
217 | selectColumnCount += 1; | |
218 | } | |
219 | if (showMinute) { | |
220 | selectColumnCount += 1; | |
221 | } | |
222 | if (showSecond) { | |
223 | selectColumnCount += 1; | |
224 | } | |
225 | if (use12Hours) { | |
226 | selectColumnCount += 1; | |
227 | } | |
228 | popupClassName += ` ${prefixCls}-panel-column-${selectColumnCount}`; | |
229 | return popupClassName; | |
230 | } | |
231 | ||
71c3a196 | 232 | setOpen(open) { |
1882f74d | 233 | const { onOpen, onClose } = this.props; |
02de449a | 234 | if (this.state.open !== open) { |
71c3a196 | 235 | if (!('open' in this.props)) { |
236 | this.setState({ open }); | |
237 | } | |
02de449a | 238 | if (open) { |
71c3a196 | 239 | onOpen({ open }); |
02de449a | 240 | } else { |
71c3a196 | 241 | onClose({ open }); |
02de449a | 242 | } |
243 | } | |
3ab3a128 | 244 | } |
02de449a | 245 | |
ed849606 | 246 | focus() { |
dd63e6e7 | 247 | this.picker.focus(); |
3ab3a128 | 248 | } |
ed849606 | 249 | |
d9a9b691 WZ |
250 | blur() { |
251 | this.picker.blur(); | |
252 | } | |
253 | ||
02de449a | 254 | render() { |
4984ed85 | 255 | const { |
256 | prefixCls, placeholder, placement, align, | |
a7c6e7bb | 257 | disabled, transitionName, style, className, getPopupContainer, name, autoComplete, |
543a5211 | 258 | onFocus, onBlur, autoFocus, inputReadOnly, |
4984ed85 | 259 | } = this.props; |
4acbf95c | 260 | const { open, value } = this.state; |
a7c6e7bb | 261 | const popupClassName = this.getPopupClassName(); |
02de449a | 262 | return ( |
263 | <Trigger | |
96a4cefc | 264 | prefixCls={`${prefixCls}-panel`} |
8133e8cf | 265 | popupClassName={popupClassName} |
02de449a | 266 | popup={this.getPanelElement()} |
267 | popupAlign={align} | |
268 | builtinPlacements={placements} | |
269 | popupPlacement={placement} | |
270 | action={disabled ? [] : ['click']} | |
271 | destroyPopupOnHide | |
8133e8cf | 272 | getPopupContainer={getPopupContainer} |
02de449a | 273 | popupTransitionName={transitionName} |
4acbf95c | 274 | popupVisible={open} |
02de449a | 275 | onPopupVisibleChange={this.onVisibleChange} |
276 | > | |
96d366af | 277 | <span className={`${prefixCls} ${className}`} style={style}> |
1882f74d | 278 | <input |
279 | className={`${prefixCls}-input`} | |
66c7bc36 | 280 | ref={this.saveInputRef} |
281 | type="text" | |
282 | placeholder={placeholder} | |
6d1ad104 | 283 | name={name} |
1882f74d | 284 | onKeyDown={this.onKeyDown} |
d9a9b691 WZ |
285 | disabled={disabled} |
286 | value={value && value.format(this.getFormat()) || ''} | |
73e6cf78 | 287 | autoComplete={autoComplete} |
e30387ab CS |
288 | onFocus={onFocus} |
289 | onBlur={onBlur} | |
d9a9b691 | 290 | autoFocus={autoFocus} |
fdead5ab | 291 | onChange={noop} |
b6a1d52b | 292 | readOnly={!!inputReadOnly} |
543a5211 | 293 | /> |
9f9f39e4 | 294 | <span className={`${prefixCls}-icon`}/> |
02de449a | 295 | </span> |
296 | </Trigger> | |
297 | ); | |
3ab3a128 | 298 | } |
299 | } |