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,
51 onKeyDown: PropTypes.func,
54 static defaultProps = {
56 prefixCls: 'rc-time-picker',
62 defaultOpenValue: moment(),
68 disabledMinutes: noop,
69 disabledSeconds: noop,
70 hideDisabledOptions: false,
71 placement: 'bottomLeft',
84 this.saveInputRef = refFn.bind(this, 'picker');
85 this.savePanelRef = refFn.bind(this, 'panelInstance');
86 const { defaultOpen, defaultValue, open = defaultOpen, value = defaultValue } = props;
93 componentWillReceiveProps(nextProps) {
94 const { value, open } = nextProps;
95 if ('value' in nextProps) {
100 if (open !== undefined) {
101 this.setState({ open });
105 onPanelChange = (value) => {
106 this.setValue(value);
109 onPanelClear = () => {
114 onVisibleChange = (open) => {
124 if (e.keyCode === 40) {
130 if (!('value' in this.props)) {
135 this.props.onChange(value);
139 const { format, showHour, showMinute, showSecond, use12Hours } = this.props;
147 showMinute ? 'mm' : '',
148 showSecond ? 'ss' : '',
149 ].filter(item => !!item).join(':'));
151 return fmtString.concat(' a');
155 showHour ? 'HH' : '',
156 showMinute ? 'mm' : '',
157 showSecond ? 'ss' : '',
158 ].filter(item => !!item).join(':');
163 prefixCls, placeholder, disabledHours,
164 disabledMinutes, disabledSeconds, hideDisabledOptions,
165 allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText,
166 addon, use12Hours, onKeyDown,
170 clearText={clearText}
171 prefixCls={`${prefixCls}-panel`}
172 ref={this.savePanelRef}
173 value={this.state.value}
174 onChange={this.onPanelChange}
175 onClear={this.onPanelClear}
176 defaultOpenValue={defaultOpenValue}
178 showMinute={showMinute}
179 showSecond={showSecond}
181 allowEmpty={allowEmpty}
182 format={this.getFormat()}
183 placeholder={placeholder}
184 disabledHours={disabledHours}
185 disabledMinutes={disabledMinutes}
186 disabledSeconds={disabledSeconds}
187 hideDisabledOptions={hideDisabledOptions}
188 use12Hours={use12Hours}
190 onKeyDown={onKeyDown}
195 getPopupClassName() {
196 const { showHour, showMinute, showSecond, use12Hours, prefixCls } = this.props;
197 let popupClassName = this.props.popupClassName;
198 // Keep it for old compatibility
199 if ((!showHour || !showMinute || !showSecond) && !use12Hours) {
200 popupClassName += ` ${prefixCls}-panel-narrow`;
202 let selectColumnCount = 0;
204 selectColumnCount += 1;
207 selectColumnCount += 1;
210 selectColumnCount += 1;
213 selectColumnCount += 1;
215 popupClassName += ` ${prefixCls}-panel-column-${selectColumnCount}`;
216 return popupClassName;
220 const { onOpen, onClose } = this.props;
221 if (this.state.open !== open) {
222 if (!('open' in this.props)) {
223 this.setState({ open });
239 prefixCls, placeholder, placement, align,
240 disabled, transitionName, style, className, getPopupContainer, name, autoComplete,
243 const { open, value } = this.state;
244 const popupClassName = this.getPopupClassName();
247 prefixCls={`${prefixCls}-panel`}
248 popupClassName={popupClassName}
249 popup={this.getPanelElement()}
251 builtinPlacements={placements}
252 popupPlacement={placement}
253 action={disabled ? [] : ['click']}
255 getPopupContainer={getPopupContainer}
256 popupTransitionName={transitionName}
258 onPopupVisibleChange={this.onVisibleChange}
260 <span className={`${prefixCls} ${className}`} style={style}>
262 className={`${prefixCls}-input`}
263 ref={this.saveInputRef}
265 placeholder={placeholder}
268 onKeyDown={this.onKeyDown}
269 disabled={disabled} value={value && value.format(this.getFormat()) || ''}
270 autoComplete={autoComplete}
274 <span className={`${prefixCls}-icon`}/>