1 import React, { Component } from 'react';
2 import PropTypes from 'prop-types';
3 import Trigger from 'rc-trigger';
4 import Panel from './Panel';
5 import placements from './placements';
6 import moment from 'moment';
11 function refFn(field, component) {
12 this[field] = component;
15 export default class Picker extends Component {
17 prefixCls: PropTypes.string,
18 clearText: PropTypes.string,
19 value: PropTypes.object,
20 defaultOpenValue: PropTypes.object,
21 disabled: PropTypes.bool,
22 allowEmpty: PropTypes.bool,
23 defaultValue: PropTypes.object,
25 defaultOpen: PropTypes.bool,
26 align: PropTypes.object,
27 placement: PropTypes.any,
28 transitionName: PropTypes.string,
29 getPopupContainer: PropTypes.func,
30 placeholder: PropTypes.string,
31 format: PropTypes.string,
32 showHour: PropTypes.bool,
33 showMinute: PropTypes.bool,
34 showSecond: PropTypes.bool,
35 style: PropTypes.object,
36 className: PropTypes.string,
37 popupClassName: PropTypes.string,
38 disabledHours: PropTypes.func,
39 disabledMinutes: PropTypes.func,
40 disabledSeconds: PropTypes.func,
41 hideDisabledOptions: PropTypes.bool,
42 onChange: PropTypes.func,
43 onOpen: PropTypes.func,
44 onClose: PropTypes.func,
45 addon: PropTypes.func,
46 name: PropTypes.string,
47 autoComplete: PropTypes.string,
48 use12Hours: PropTypes.bool,
49 hourStep: PropTypes.number,
50 minuteStep: PropTypes.number,
51 secondStep: PropTypes.number,
54 static defaultProps = {
56 prefixCls: 'rc-time-picker',
62 defaultOpenValue: moment(),
68 disabledMinutes: noop,
69 disabledSeconds: noop,
70 hideDisabledOptions: false,
71 placement: 'bottomLeft',
81 this.saveInputRef = refFn.bind(this, 'picker');
82 this.savePanelRef = refFn.bind(this, 'panelInstance');
83 const { defaultOpen, defaultValue, open = defaultOpen, value = defaultValue } = props;
90 componentWillReceiveProps(nextProps) {
91 const { value, open } = nextProps;
92 if ('value' in nextProps) {
97 if (open !== undefined) {
98 this.setState({ open });
102 onPanelChange = (value) => {
103 this.setValue(value);
106 onPanelClear = () => {
111 onVisibleChange = (open) => {
121 if (e.keyCode === 40) {
127 if (!('value' in this.props)) {
132 this.props.onChange(value);
136 const { format, showHour, showMinute, showSecond, use12Hours } = this.props;
144 showMinute ? 'mm' : '',
145 showSecond ? 'ss' : '',
146 ].filter(item => !!item).join(':'));
148 return fmtString.concat(' a');
152 showHour ? 'HH' : '',
153 showMinute ? 'mm' : '',
154 showSecond ? 'ss' : '',
155 ].filter(item => !!item).join(':');
160 prefixCls, placeholder, disabledHours,
161 disabledMinutes, disabledSeconds, hideDisabledOptions,
162 allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText,
163 addon, use12Hours, hourStep, minuteStep, secondStep,
167 clearText={clearText}
168 prefixCls={`${prefixCls}-panel`}
169 ref={this.savePanelRef}
170 value={this.state.value}
171 onChange={this.onPanelChange}
172 onClear={this.onPanelClear}
173 defaultOpenValue={defaultOpenValue}
175 showMinute={showMinute}
176 showSecond={showSecond}
178 allowEmpty={allowEmpty}
179 format={this.getFormat()}
180 placeholder={placeholder}
181 disabledHours={disabledHours}
182 disabledMinutes={disabledMinutes}
183 disabledSeconds={disabledSeconds}
184 hideDisabledOptions={hideDisabledOptions}
185 use12Hours={use12Hours}
187 minuteStep={minuteStep}
188 secondStep={secondStep}
194 getPopupClassName() {
195 const { showHour, showMinute, showSecond, use12Hours, prefixCls } = this.props;
196 let popupClassName = this.props.popupClassName;
197 // Keep it for old compatibility
198 if ((!showHour || !showMinute || !showSecond) && !use12Hours) {
199 popupClassName += ` ${prefixCls}-panel-narrow`;
201 let selectColumnCount = 0;
203 selectColumnCount += 1;
206 selectColumnCount += 1;
209 selectColumnCount += 1;
212 selectColumnCount += 1;
214 popupClassName += ` ${prefixCls}-panel-column-${selectColumnCount}`;
215 return popupClassName;
219 const { onOpen, onClose } = this.props;
220 if (this.state.open !== open) {
221 if (!('open' in this.props)) {
222 this.setState({ open });
238 prefixCls, placeholder, placement, align,
239 disabled, transitionName, style, className, getPopupContainer, name, autoComplete,
241 const { open, value } = this.state;
242 const popupClassName = this.getPopupClassName();
245 prefixCls={`${prefixCls}-panel`}
246 popupClassName={popupClassName}
247 popup={this.getPanelElement()}
249 builtinPlacements={placements}
250 popupPlacement={placement}
251 action={disabled ? [] : ['click']}
253 getPopupContainer={getPopupContainer}
254 popupTransitionName={transitionName}
256 onPopupVisibleChange={this.onVisibleChange}
258 <span className={`${prefixCls} ${className}`} style={style}>
260 className={`${prefixCls}-input`}
261 ref={this.saveInputRef}
263 placeholder={placeholder}
266 onKeyDown={this.onKeyDown}
267 disabled={disabled} value={value && value.format(this.getFormat()) || ''}
268 autoComplete={autoComplete}
270 <span className={`${prefixCls}-icon`}/>