1 import React, { PropTypes } from 'react';
2 import Trigger from 'rc-trigger';
3 import Panel from './module/Panel';
4 import placements from './util/placements';
5 import CommonMixin from './mixin/CommonMixin';
6 import { getFormatter } from './util/index';
11 function refFn(field, component) {
12 this[field] = component;
15 const Picker = React.createClass({
17 prefixCls: PropTypes.string,
18 locale: PropTypes.object,
19 value: 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 formatter: PropTypes.any,
31 showHour: PropTypes.bool,
32 style: PropTypes.object,
33 className: PropTypes.string,
34 showSecond: PropTypes.bool,
35 disabledHours: PropTypes.func,
36 disabledMinutes: PropTypes.func,
37 disabledSeconds: PropTypes.func,
38 hideDisabledOptions: PropTypes.bool,
39 onChange: PropTypes.func,
40 onOpen: PropTypes.func,
41 onClose: PropTypes.func,
44 mixins: [CommonMixin],
48 prefixCls: 'rc-time-picker',
57 disabledMinutes: noop,
58 disabledSeconds: noop,
59 hideDisabledOptions: false,
60 placement: 'bottomLeft',
68 this.savePanelRef = refFn.bind(this, 'panelInstance');
69 const { defaultOpen, defaultValue, open = defaultOpen, value = defaultValue } = this.props;
76 componentWillReceiveProps(nextProps) {
77 const { value, open } = nextProps;
78 if ('value' in nextProps) {
83 if (open !== undefined) {
84 this.setState({ open });
88 onPanelChange(value) {
97 onVisibleChange(open) {
103 this.refs.picker.focus();
107 if (e.keyCode === 40) {
113 if (!('value' in this.props)) {
118 this.props.onChange(value);
122 const formatter = this.props.formatter;
123 const locale = this.props.locale;
125 if (formatter === this.lastFormatter) {
126 return this.normalFormatter;
128 this.normalFormatter = getFormatter(formatter, locale);
129 this.lastFormatter = formatter;
130 return this.normalFormatter;
132 if (!this.props.showSecond) {
133 if (!this.notShowSecondFormatter) {
134 this.notShowSecondFormatter = getFormatter('HH:mm', locale);
136 return this.notShowSecondFormatter;
138 if (!this.props.showHour) {
139 if (!this.notShowHourFormatter) {
140 this.notShowHourFormatter = getFormatter('mm:ss', locale);
142 return this.notShowHourFormatter;
144 if (!this.normalFormatter) {
145 this.normalFormatter = getFormatter('HH:mm:ss', locale);
147 return this.normalFormatter;
152 prefixCls, defaultValue, locale, placeholder, disabledHours,
153 disabledMinutes, disabledSeconds, hideDisabledOptions,
154 allowEmpty, showHour, showSecond,
158 prefixCls={`${prefixCls}-panel`}
159 ref={this.savePanelRef}
160 value={this.state.value}
161 onChange={this.onPanelChange}
162 gregorianCalendarLocale={locale.calendar}
163 onClear={this.onPanelClear}
164 defaultValue={defaultValue}
167 showSecond={showSecond}
169 allowEmpty={allowEmpty}
170 formatter={this.getFormatter()}
171 placeholder={placeholder}
172 disabledHours={disabledHours}
173 disabledMinutes={disabledMinutes}
174 disabledSeconds={disabledSeconds}
175 hideDisabledOptions={hideDisabledOptions}
180 setOpen(open, callback) {
181 const { onOpen, onClose } = this.props;
182 if (this.state.open !== open) {
198 const { prefixCls, placeholder, placement, align, disabled, transitionName, style, className, showHour, showSecond, getPopupContainer } = this.props;
199 const { open, value } = this.state;
201 if (!showHour || !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="picker" type="text" placeholder={placeholder}
224 onKeyDown={this.onKeyDown}
225 disabled={disabled} value={value && this.getFormatter().format(value) || ''}
227 <span className={`${prefixCls}-icon`}/>
234 export default Picker;