1 import React, { PropTypes } from 'react';
2 import Trigger from 'rc-trigger';
3 import Panel from './Panel';
4 import placements from './placements';
5 import moment from 'moment';
10 function refFn(field, component) {
11 this[field] = component;
14 const Picker = React.createClass({
16 prefixCls: PropTypes.string,
17 clearText: PropTypes.string,
18 value: PropTypes.object,
19 defaultOpenValue: PropTypes.object,
20 disabled: PropTypes.bool,
21 allowEmpty: PropTypes.bool,
22 defaultValue: PropTypes.object,
24 defaultOpen: PropTypes.bool,
25 align: PropTypes.object,
26 placement: PropTypes.any,
27 transitionName: PropTypes.string,
28 getPopupContainer: PropTypes.func,
29 placeholder: PropTypes.string,
30 format: PropTypes.string,
31 showHour: PropTypes.bool,
32 showMinute: PropTypes.bool,
33 showSecond: PropTypes.bool,
34 style: PropTypes.object,
35 className: PropTypes.string,
36 disabledHours: PropTypes.func,
37 disabledMinutes: PropTypes.func,
38 disabledSeconds: PropTypes.func,
39 hideDisabledOptions: PropTypes.bool,
40 onChange: PropTypes.func,
41 onOpen: PropTypes.func,
42 onClose: PropTypes.func,
43 addon: PropTypes.func,
49 prefixCls: 'rc-time-picker',
54 defaultOpenValue: moment(),
60 disabledMinutes: noop,
61 disabledSeconds: noop,
62 hideDisabledOptions: false,
63 placement: 'bottomLeft',
72 this.savePanelRef = refFn.bind(this, 'panelInstance');
73 const { defaultOpen, defaultValue, open = defaultOpen, value = defaultValue } = this.props;
80 componentWillReceiveProps(nextProps) {
81 const { value, open } = nextProps;
82 if ('value' in nextProps) {
87 if (open !== undefined) {
88 this.setState({ open });
92 onPanelChange(value) {
101 onVisibleChange(open) {
107 this.refs.picker.focus();
111 if (e.keyCode === 40) {
117 if (!('value' in this.props)) {
122 this.props.onChange(value);
126 const { format, showHour, showMinute, showSecond } = this.props;
131 showHour ? 'HH' : '',
132 showMinute ? 'mm' : '',
133 showSecond ? 'ss' : '',
134 ].filter(item => !!item).join(':');
139 prefixCls, placeholder, disabledHours,
140 disabledMinutes, disabledSeconds, hideDisabledOptions,
141 allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText,
146 clearText={clearText}
147 prefixCls={`${prefixCls}-panel`}
148 ref={this.savePanelRef}
149 value={this.state.value}
150 onChange={this.onPanelChange}
151 onClear={this.onPanelClear}
152 defaultOpenValue={defaultOpenValue}
154 showMinute={showMinute}
155 showSecond={showSecond}
157 allowEmpty={allowEmpty}
158 format={this.getFormat()}
159 placeholder={placeholder}
160 disabledHours={disabledHours}
161 disabledMinutes={disabledMinutes}
162 disabledSeconds={disabledSeconds}
163 hideDisabledOptions={hideDisabledOptions}
169 setOpen(open, callback) {
170 const { onOpen, onClose } = this.props;
171 if (this.state.open !== open) {
188 prefixCls, placeholder, placement, align,
189 disabled, transitionName, style, className, showHour,
190 showSecond, getPopupContainer,
192 const { open, value } = this.state;
194 if (!showHour || !showSecond) {
195 popupClassName = `${prefixCls}-panel-narrow`;
199 prefixCls={`${prefixCls}-panel`}
200 popupClassName={popupClassName}
201 popup={this.getPanelElement()}
203 builtinPlacements={placements}
204 popupPlacement={placement}
205 action={disabled ? [] : ['click']}
207 getPopupContainer={getPopupContainer}
208 popupTransitionName={transitionName}
210 onPopupVisibleChange={this.onVisibleChange}
212 <span className={`${prefixCls} ${className}`} style={style}>
214 className={`${prefixCls}-input`}
215 ref="picker" type="text" placeholder={placeholder}
217 onKeyDown={this.onKeyDown}
218 disabled={disabled} value={value && value.format(this.getFormat()) || ''}
220 <span className={`${prefixCls}-icon`}/>
227 export default Picker;