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,
51 static defaultProps = {
53 prefixCls: 'rc-time-picker',
59 defaultOpenValue: moment(),
65 disabledMinutes: noop,
66 disabledSeconds: noop,
67 hideDisabledOptions: false,
68 placement: 'bottomLeft',
78 this.saveInputRef = refFn.bind(this, 'picker');
79 this.savePanelRef = refFn.bind(this, 'panelInstance');
80 const { defaultOpen, defaultValue, open = defaultOpen, value = defaultValue } = props;
87 componentWillReceiveProps(nextProps) {
88 const { value, open } = nextProps;
89 if ('value' in nextProps) {
94 if (open !== undefined) {
95 this.setState({ open });
99 onPanelChange = (value) => {
100 this.setValue(value);
103 onPanelClear = () => {
108 onVisibleChange = (open) => {
118 if (e.keyCode === 40) {
124 if (!('value' in this.props)) {
129 this.props.onChange(value);
133 const { format, showHour, showMinute, showSecond, use12Hours } = this.props;
141 showMinute ? 'mm' : '',
142 showSecond ? 'ss' : '',
143 ].filter(item => !!item).join(':'));
145 return fmtString.concat(' a');
149 showHour ? 'HH' : '',
150 showMinute ? 'mm' : '',
151 showSecond ? 'ss' : '',
152 ].filter(item => !!item).join(':');
157 prefixCls, placeholder, disabledHours,
158 disabledMinutes, disabledSeconds, hideDisabledOptions,
159 allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText,
164 clearText={clearText}
165 prefixCls={`${prefixCls}-panel`}
166 ref={this.savePanelRef}
167 value={this.state.value}
168 onChange={this.onPanelChange}
169 onClear={this.onPanelClear}
170 defaultOpenValue={defaultOpenValue}
172 showMinute={showMinute}
173 showSecond={showSecond}
175 allowEmpty={allowEmpty}
176 format={this.getFormat()}
177 placeholder={placeholder}
178 disabledHours={disabledHours}
179 disabledMinutes={disabledMinutes}
180 disabledSeconds={disabledSeconds}
181 hideDisabledOptions={hideDisabledOptions}
182 use12Hours={use12Hours}
188 getPopupClassName() {
189 const { showHour, showMinute, showSecond, use12Hours, prefixCls } = this.props;
190 let popupClassName = this.props.popupClassName;
191 // Keep it for old compatibility
192 if ((!showHour || !showMinute || !showSecond) && !use12Hours) {
193 popupClassName += ` ${prefixCls}-panel-narrow`;
195 let selectColumnCount = 0;
197 selectColumnCount += 1;
200 selectColumnCount += 1;
203 selectColumnCount += 1;
206 selectColumnCount += 1;
208 popupClassName += ` ${prefixCls}-panel-column-${selectColumnCount}`;
209 return popupClassName;
213 const { onOpen, onClose } = this.props;
214 if (this.state.open !== open) {
215 if (!('open' in this.props)) {
216 this.setState({ open });
232 prefixCls, placeholder, placement, align,
233 disabled, transitionName, style, className, getPopupContainer, name, autoComplete,
235 const { open, value } = this.state;
236 const popupClassName = this.getPopupClassName();
239 prefixCls={`${prefixCls}-panel`}
240 popupClassName={popupClassName}
241 popup={this.getPanelElement()}
243 builtinPlacements={placements}
244 popupPlacement={placement}
245 action={disabled ? [] : ['click']}
247 getPopupContainer={getPopupContainer}
248 popupTransitionName={transitionName}
250 onPopupVisibleChange={this.onVisibleChange}
252 <span className={`${prefixCls} ${className}`} style={style}>
254 className={`${prefixCls}-input`}
255 ref={this.saveInputRef}
257 placeholder={placeholder}
260 onKeyDown={this.onKeyDown}
261 disabled={disabled} value={value && value.format(this.getFormat()) || ''}
262 autoComplete={autoComplete}
264 <span className={`${prefixCls}-icon`}/>