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 onFocus: PropTypes.func,
46 onBlur: PropTypes.func,
47 addon: PropTypes.func,
48 name: PropTypes.string,
49 autoComplete: PropTypes.string,
50 use12Hours: PropTypes.bool,
53 static defaultProps = {
55 prefixCls: 'rc-time-picker',
61 defaultOpenValue: moment(),
67 disabledMinutes: noop,
68 disabledSeconds: noop,
69 hideDisabledOptions: false,
70 placement: 'bottomLeft',
82 this.saveInputRef = refFn.bind(this, 'picker');
83 this.savePanelRef = refFn.bind(this, 'panelInstance');
84 const { defaultOpen, defaultValue, open = defaultOpen, value = defaultValue } = props;
91 componentWillReceiveProps(nextProps) {
92 const { value, open } = nextProps;
93 if ('value' in nextProps) {
98 if (open !== undefined) {
99 this.setState({ open });
103 onPanelChange = (value) => {
104 this.setValue(value);
107 onPanelClear = () => {
112 onVisibleChange = (open) => {
122 if (e.keyCode === 40) {
128 if (!('value' in this.props)) {
133 this.props.onChange(value);
137 const { format, showHour, showMinute, showSecond, use12Hours } = this.props;
145 showMinute ? 'mm' : '',
146 showSecond ? 'ss' : '',
147 ].filter(item => !!item).join(':'));
149 return fmtString.concat(' a');
153 showHour ? 'HH' : '',
154 showMinute ? 'mm' : '',
155 showSecond ? 'ss' : '',
156 ].filter(item => !!item).join(':');
161 prefixCls, placeholder, disabledHours,
162 disabledMinutes, disabledSeconds, hideDisabledOptions,
163 allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText,
168 clearText={clearText}
169 prefixCls={`${prefixCls}-panel`}
170 ref={this.savePanelRef}
171 value={this.state.value}
172 onChange={this.onPanelChange}
173 onClear={this.onPanelClear}
174 defaultOpenValue={defaultOpenValue}
176 showMinute={showMinute}
177 showSecond={showSecond}
179 allowEmpty={allowEmpty}
180 format={this.getFormat()}
181 placeholder={placeholder}
182 disabledHours={disabledHours}
183 disabledMinutes={disabledMinutes}
184 disabledSeconds={disabledSeconds}
185 hideDisabledOptions={hideDisabledOptions}
186 use12Hours={use12Hours}
192 getPopupClassName() {
193 const { showHour, showMinute, showSecond, use12Hours, prefixCls } = this.props;
194 let popupClassName = this.props.popupClassName;
195 // Keep it for old compatibility
196 if ((!showHour || !showMinute || !showSecond) && !use12Hours) {
197 popupClassName += ` ${prefixCls}-panel-narrow`;
199 let selectColumnCount = 0;
201 selectColumnCount += 1;
204 selectColumnCount += 1;
207 selectColumnCount += 1;
210 selectColumnCount += 1;
212 popupClassName += ` ${prefixCls}-panel-column-${selectColumnCount}`;
213 return popupClassName;
217 const { onOpen, onClose } = this.props;
218 if (this.state.open !== open) {
219 if (!('open' in this.props)) {
220 this.setState({ open });
236 prefixCls, placeholder, placement, align,
237 disabled, transitionName, style, className, getPopupContainer, name, autoComplete,
240 const { open, value } = this.state;
241 const popupClassName = this.getPopupClassName();
244 prefixCls={`${prefixCls}-panel`}
245 popupClassName={popupClassName}
246 popup={this.getPanelElement()}
248 builtinPlacements={placements}
249 popupPlacement={placement}
250 action={disabled ? [] : ['click']}
252 getPopupContainer={getPopupContainer}
253 popupTransitionName={transitionName}
255 onPopupVisibleChange={this.onVisibleChange}
257 <span className={`${prefixCls} ${className}`} style={style}>
259 className={`${prefixCls}-input`}
260 ref={this.saveInputRef}
262 placeholder={placeholder}
265 onKeyDown={this.onKeyDown}
266 disabled={disabled} value={value && value.format(this.getFormat()) || ''}
267 autoComplete={autoComplete}
271 <span className={`${prefixCls}-icon`}/>