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,
45 autoComplete: PropTypes.string,
46 show12Hours: PropTypes.bool,
52 prefixCls: 'rc-time-picker',
57 defaultOpenValue: moment(),
63 disabledMinutes: noop,
64 disabledSeconds: noop,
65 hideDisabledOptions: false,
66 placement: 'bottomLeft',
76 this.saveInputRef = refFn.bind(this, 'picker');
77 this.savePanelRef = refFn.bind(this, 'panelInstance');
78 const { defaultOpen, defaultValue, open = defaultOpen, value = defaultValue } = this.props;
85 componentWillReceiveProps(nextProps) {
86 const { value, open } = nextProps;
87 if ('value' in nextProps) {
92 if (open !== undefined) {
93 this.setState({ open });
97 onPanelChange(value) {
106 onVisibleChange(open) {
116 if (e.keyCode === 40) {
122 if (!('value' in this.props)) {
127 this.props.onChange(value);
131 const { format, showHour, showMinute, showSecond } = this.props;
136 showHour ? 'HH' : '',
137 showMinute ? 'mm' : '',
138 showSecond ? 'ss' : '',
139 ].filter(item => !!item).join(':');
144 prefixCls, placeholder, disabledHours,
145 disabledMinutes, disabledSeconds, hideDisabledOptions,
146 allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText,
151 clearText={clearText}
152 prefixCls={`${prefixCls}-panel`}
153 ref={this.savePanelRef}
154 value={this.state.value}
155 onChange={this.onPanelChange}
156 onClear={this.onPanelClear}
157 defaultOpenValue={defaultOpenValue}
159 showMinute={showMinute}
160 showSecond={showSecond}
162 allowEmpty={allowEmpty}
163 format={this.getFormat()}
164 placeholder={placeholder}
165 disabledHours={disabledHours}
166 disabledMinutes={disabledMinutes}
167 disabledSeconds={disabledSeconds}
168 hideDisabledOptions={hideDisabledOptions}
169 show12Hours={show12Hours}
176 const { onOpen, onClose } = this.props;
177 if (this.state.open !== open) {
178 if (!('open' in this.props)) {
179 this.setState({ open });
195 prefixCls, placeholder, placement, align,
196 disabled, transitionName, style, className, showHour,
197 showMinute, showSecond, getPopupContainer, name, autoComplete,
199 const { open, value } = this.state;
201 if (!showHour || !showMinute || !showSecond) {
202 popupClassName = `${prefixCls}-panel-narrow`;
206 prefixCls={`${prefixCls}-panel`}
207 popupClassName={popupClassName}
208 popup={this.getPanelElement()}
210 builtinPlacements={placements}
211 popupPlacement={placement}
212 action={disabled ? [] : ['click']}
214 getPopupContainer={getPopupContainer}
215 popupTransitionName={transitionName}
217 onPopupVisibleChange={this.onVisibleChange}
219 <span className={`${prefixCls} ${className}`} style={style}>
221 className={`${prefixCls}-input`}
222 ref={this.saveInputRef}
224 placeholder={placeholder}
227 onKeyDown={this.onKeyDown}
228 disabled={disabled} value={value && value.format(this.getFormat()) || ''}
229 autoComplete={autoComplete}
231 <span className={`${prefixCls}-icon`}/>
238 export default Picker;