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,
44 name: PropTypes.string,
50 prefixCls: 'rc-time-picker',
55 defaultOpenValue: moment(),
61 disabledMinutes: noop,
62 disabledSeconds: noop,
63 hideDisabledOptions: false,
64 placement: 'bottomLeft',
73 this.savePanelRef = refFn.bind(this, 'panelInstance');
74 const { defaultOpen, defaultValue, open = defaultOpen, value = defaultValue } = this.props;
81 componentWillReceiveProps(nextProps) {
82 const { value, open } = nextProps;
83 if ('value' in nextProps) {
88 if (open !== undefined) {
89 this.setState({ open });
93 onPanelChange(value) {
102 onVisibleChange(open) {
108 this.refs.picker.focus();
112 if (e.keyCode === 40) {
118 if (!('value' in this.props)) {
123 this.props.onChange(value);
127 const { format, showHour, showMinute, showSecond } = this.props;
132 showHour ? 'HH' : '',
133 showMinute ? 'mm' : '',
134 showSecond ? 'ss' : '',
135 ].filter(item => !!item).join(':');
140 prefixCls, placeholder, disabledHours,
141 disabledMinutes, disabledSeconds, hideDisabledOptions,
142 allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText,
147 clearText={clearText}
148 prefixCls={`${prefixCls}-panel`}
149 ref={this.savePanelRef}
150 value={this.state.value}
151 onChange={this.onPanelChange}
152 onClear={this.onPanelClear}
153 defaultOpenValue={defaultOpenValue}
155 showMinute={showMinute}
156 showSecond={showSecond}
158 allowEmpty={allowEmpty}
159 format={this.getFormat()}
160 placeholder={placeholder}
161 disabledHours={disabledHours}
162 disabledMinutes={disabledMinutes}
163 disabledSeconds={disabledSeconds}
164 hideDisabledOptions={hideDisabledOptions}
170 setOpen(open, callback) {
171 const { onOpen, onClose } = this.props;
172 if (this.state.open !== open) {
189 prefixCls, placeholder, placement, align,
190 disabled, transitionName, style, className, showHour,
191 showMinute, showSecond, getPopupContainer, name,
193 const { open, value } = this.state;
195 if (!showHour || !showMinute || !showSecond) {
196 popupClassName = `${prefixCls}-panel-narrow`;
200 prefixCls={`${prefixCls}-panel`}
201 popupClassName={popupClassName}
202 popup={this.getPanelElement()}
204 builtinPlacements={placements}
205 popupPlacement={placement}
206 action={disabled ? [] : ['click']}
208 getPopupContainer={getPopupContainer}
209 popupTransitionName={transitionName}
211 onPopupVisibleChange={this.onVisibleChange}
213 <span className={`${prefixCls} ${className}`} style={style}>
215 className={`${prefixCls}-input`}
216 ref="picker" type="text" placeholder={placeholder}
219 onKeyDown={this.onKeyDown}
220 disabled={disabled} value={value && value.format(this.getFormat()) || ''}
222 <span className={`${prefixCls}-icon`}/>
229 export default Picker;