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.saveInputRef = refFn.bind(this, 'picker');
74 this.savePanelRef = refFn.bind(this, 'panelInstance');
75 const { defaultOpen, defaultValue, open = defaultOpen, value = defaultValue } = this.props;
82 componentWillReceiveProps(nextProps) {
83 const { value, open } = nextProps;
84 if ('value' in nextProps) {
89 if (open !== undefined) {
90 this.setState({ open });
94 onPanelChange(value) {
103 onVisibleChange(open) {
113 if (e.keyCode === 40) {
119 if (!('value' in this.props)) {
124 this.props.onChange(value);
128 const { format, showHour, showMinute, showSecond } = this.props;
133 showHour ? 'HH' : '',
134 showMinute ? 'mm' : '',
135 showSecond ? 'ss' : '',
136 ].filter(item => !!item).join(':');
141 prefixCls, placeholder, disabledHours,
142 disabledMinutes, disabledSeconds, hideDisabledOptions,
143 allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText,
148 clearText={clearText}
149 prefixCls={`${prefixCls}-panel`}
150 ref={this.savePanelRef}
151 value={this.state.value}
152 onChange={this.onPanelChange}
153 onClear={this.onPanelClear}
154 defaultOpenValue={defaultOpenValue}
156 showMinute={showMinute}
157 showSecond={showSecond}
159 allowEmpty={allowEmpty}
160 format={this.getFormat()}
161 placeholder={placeholder}
162 disabledHours={disabledHours}
163 disabledMinutes={disabledMinutes}
164 disabledSeconds={disabledSeconds}
165 hideDisabledOptions={hideDisabledOptions}
171 setOpen(open, callback) {
172 const { onOpen, onClose } = this.props;
173 if (this.state.open !== open) {
194 prefixCls, placeholder, placement, align,
195 disabled, transitionName, style, className, showHour,
196 showMinute, showSecond, getPopupContainer, name,
198 const { open, value } = this.state;
200 if (!showHour || !showMinute || !showSecond) {
201 popupClassName = `${prefixCls}-panel-narrow`;
205 prefixCls={`${prefixCls}-panel`}
206 popupClassName={popupClassName}
207 popup={this.getPanelElement()}
209 builtinPlacements={placements}
210 popupPlacement={placement}
211 action={disabled ? [] : ['click']}
213 getPopupContainer={getPopupContainer}
214 popupTransitionName={transitionName}
216 onPopupVisibleChange={this.onVisibleChange}
218 <span className={`${prefixCls} ${className}`} style={style}>
220 className={`${prefixCls}-input`}
221 ref={this.saveInputRef}
223 placeholder={placeholder}
226 onKeyDown={this.onKeyDown}
227 disabled={disabled} value={value && value.format(this.getFormat()) || ''}
229 <span className={`${prefixCls}-icon`}/>
236 export default Picker;