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],
56 disabledMinutes: noop,
57 disabledSeconds: noop,
58 hideDisabledOptions: false,
59 placement: 'bottomLeft',
67 this.savePanelRef = refFn.bind(this, 'panelInstance');
68 const { defaultOpen, defaultValue, open = defaultOpen, value = defaultValue } = this.props;
75 componentWillReceiveProps(nextProps) {
76 const { value, open } = nextProps;
77 if ('value' in nextProps) {
82 if (open !== undefined) {
83 this.setState({open});
87 onPanelChange(value) {
96 onVisibleChange(open) {
102 this.refs.picker.focus();
106 if (e.keyCode === 40) {
112 if (!('value' in this.props)) {
117 this.props.onChange(value);
121 const formatter = this.props.formatter;
122 const locale = this.props.locale;
124 if (formatter === this.lastFormatter) {
125 return this.normalFormatter;
127 this.normalFormatter = getFormatter(formatter, locale);
128 this.lastFormatter = formatter;
129 return this.normalFormatter;
131 if (!this.props.showSecond) {
132 if (!this.notShowSecondFormatter) {
133 this.notShowSecondFormatter = getFormatter('HH:mm', locale);
135 return this.notShowSecondFormatter;
137 if (!this.props.showHour) {
138 if (!this.notShowHourFormatter) {
139 this.notShowHourFormatter = getFormatter('mm:ss', locale);
141 return this.notShowHourFormatter;
143 if (!this.normalFormatter) {
144 this.normalFormatter = getFormatter('HH:mm:ss', locale);
146 return this.normalFormatter;
150 const { prefixCls, defaultValue, locale, placeholder, disabledHours,
151 disabledMinutes, disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showSecond } = this.props;
154 prefixCls={`${prefixCls}-panel`}
155 ref={this.savePanelRef}
156 value={this.state.value}
157 onChange={this.onPanelChange}
158 gregorianCalendarLocale={locale.calendar}
159 onClear={this.onPanelClear}
160 defaultValue={defaultValue}
163 showSecond={showSecond}
165 allowEmpty={allowEmpty}
166 formatter={this.getFormatter()}
167 placeholder={placeholder}
168 disabledHours={disabledHours}
169 disabledMinutes={disabledMinutes}
170 disabledSeconds={disabledSeconds}
171 hideDisabledOptions={hideDisabledOptions}
176 setOpen(open, callback) {
177 const {onOpen, onClose} = this.props;
178 if (this.state.open !== open) {
194 const { prefixCls, placeholder, placement, align, disabled, transitionName, style, className, showHour, showSecond, getPopupContainer } = this.props;
195 const { open, value } = this.state;
197 if (!showHour || !showSecond) {
198 popupClassName = `${prefixCls}-panel-narrow`;
202 prefixCls={`${prefixCls}-panel`}
203 popupClassName={popupClassName}
204 popup={this.getPanelElement()}
206 builtinPlacements={placements}
207 popupPlacement={placement}
208 action={disabled ? [] : ['click']}
210 getPopupContainer={getPopupContainer}
211 popupTransitionName={transitionName}
213 onPopupVisibleChange={this.onVisibleChange}
215 <span className={`${prefixCls} ${className}`} style={style}>
216 <input className={`${prefixCls}-input`}
217 ref="picker" type="text" placeholder={placeholder}
219 onKeyDown={this.onKeyDown}
220 disabled={disabled} value={value && this.getFormatter().format(value)}/>
221 <span className={`${prefixCls}-icon`}/>
228 export default Picker;