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 focusOnOpen: PropTypes.bool,
52 static defaultProps = {
54 prefixCls: 'rc-time-picker',
60 defaultOpenValue: moment(),
66 disabledMinutes: noop,
67 disabledSeconds: noop,
68 hideDisabledOptions: false,
69 placement: 'bottomLeft',
80 this.saveInputRef = refFn.bind(this, 'picker');
81 this.savePanelRef = refFn.bind(this, 'panelInstance');
82 const { defaultOpen, defaultValue, open = defaultOpen, value = defaultValue } = props;
89 componentWillReceiveProps(nextProps) {
90 const { value, open } = nextProps;
91 if ('value' in nextProps) {
96 if (open !== undefined) {
97 this.setState({ open });
101 onPanelChange = (value) => {
102 this.setValue(value);
105 onPanelClear = () => {
110 onVisibleChange = (open) => {
120 if (e.keyCode === 40) {
126 if (!('value' in this.props)) {
131 this.props.onChange(value);
135 const { format, showHour, showMinute, showSecond, use12Hours } = this.props;
143 showMinute ? 'mm' : '',
144 showSecond ? 'ss' : '',
145 ].filter(item => !!item).join(':'));
147 return fmtString.concat(' a');
151 showHour ? 'HH' : '',
152 showMinute ? 'mm' : '',
153 showSecond ? 'ss' : '',
154 ].filter(item => !!item).join(':');
159 prefixCls, placeholder, disabledHours,
160 disabledMinutes, disabledSeconds, hideDisabledOptions,
161 allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText,
162 addon, use12Hours, focusOnOpen,
166 clearText={clearText}
167 prefixCls={`${prefixCls}-panel`}
168 ref={this.savePanelRef}
169 value={this.state.value}
170 onChange={this.onPanelChange}
171 onClear={this.onPanelClear}
172 defaultOpenValue={defaultOpenValue}
174 showMinute={showMinute}
175 showSecond={showSecond}
177 allowEmpty={allowEmpty}
178 format={this.getFormat()}
179 placeholder={placeholder}
180 disabledHours={disabledHours}
181 disabledMinutes={disabledMinutes}
182 disabledSeconds={disabledSeconds}
183 hideDisabledOptions={hideDisabledOptions}
184 use12Hours={use12Hours}
186 focusOnOpen={focusOnOpen}
191 getPopupClassName() {
192 const { showHour, showMinute, showSecond, use12Hours, prefixCls } = this.props;
193 let popupClassName = this.props.popupClassName;
194 // Keep it for old compatibility
195 if ((!showHour || !showMinute || !showSecond) && !use12Hours) {
196 popupClassName += ` ${prefixCls}-panel-narrow`;
198 let selectColumnCount = 0;
200 selectColumnCount += 1;
203 selectColumnCount += 1;
206 selectColumnCount += 1;
209 selectColumnCount += 1;
211 popupClassName += ` ${prefixCls}-panel-column-${selectColumnCount}`;
212 return popupClassName;
216 const { onOpen, onClose } = this.props;
217 if (this.state.open !== open) {
218 if (!('open' in this.props)) {
219 this.setState({ open });
235 prefixCls, placeholder, placement, align,
236 disabled, transitionName, style, className, getPopupContainer, name, autoComplete,
238 const { open, value } = this.state;
239 const popupClassName = this.getPopupClassName();
242 prefixCls={`${prefixCls}-panel`}
243 popupClassName={popupClassName}
244 popup={this.getPanelElement()}
246 builtinPlacements={placements}
247 popupPlacement={placement}
248 action={disabled ? [] : ['click']}
250 getPopupContainer={getPopupContainer}
251 popupTransitionName={transitionName}
253 onPopupVisibleChange={this.onVisibleChange}
255 <span className={`${prefixCls} ${className}`} style={style}>
257 className={`${prefixCls}-input`}
258 ref={this.saveInputRef}
260 placeholder={placeholder}
263 onKeyDown={this.onKeyDown}
264 disabled={disabled} value={value && value.format(this.getFormat()) || ''}
265 autoComplete={autoComplete}
267 <span className={`${prefixCls}-icon`}/>