]> git.immae.eu Git - github/fretlink/time-picker.git/blob - src/TimePicker.jsx
Fix controlled open
[github/fretlink/time-picker.git] / src / TimePicker.jsx
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';
6
7 function noop() {
8 }
9
10 function refFn(field, component) {
11 this[field] = component;
12 }
13
14 const Picker = React.createClass({
15 propTypes: {
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,
23 open: PropTypes.bool,
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 },
47
48 getDefaultProps() {
49 return {
50 clearText: 'clear',
51 prefixCls: 'rc-time-picker',
52 defaultOpen: false,
53 style: {},
54 className: '',
55 align: {},
56 defaultOpenValue: moment(),
57 allowEmpty: true,
58 showHour: true,
59 showMinute: true,
60 showSecond: true,
61 disabledHours: noop,
62 disabledMinutes: noop,
63 disabledSeconds: noop,
64 hideDisabledOptions: false,
65 placement: 'bottomLeft',
66 onChange: noop,
67 onOpen: noop,
68 onClose: noop,
69 addon: noop,
70 };
71 },
72
73 getInitialState() {
74 this.saveInputRef = refFn.bind(this, 'picker');
75 this.savePanelRef = refFn.bind(this, 'panelInstance');
76 const { defaultOpen, defaultValue, open = defaultOpen, value = defaultValue } = this.props;
77 return {
78 open,
79 value,
80 };
81 },
82
83 componentWillReceiveProps(nextProps) {
84 const { value, open } = nextProps;
85 if ('value' in nextProps) {
86 this.setState({
87 value,
88 });
89 }
90 if (open !== undefined) {
91 this.setState({ open });
92 }
93 },
94
95 onPanelChange(value) {
96 this.setValue(value);
97 },
98
99 onPanelClear() {
100 this.setValue(null);
101 this.setOpen(false);
102 },
103
104 onVisibleChange(open) {
105 this.setOpen(open);
106 },
107
108 onEsc() {
109 this.setOpen(false);
110 this.focus();
111 },
112
113 onKeyDown(e) {
114 if (e.keyCode === 40) {
115 this.setOpen(true);
116 }
117 },
118
119 setValue(value) {
120 if (!('value' in this.props)) {
121 this.setState({
122 value,
123 });
124 }
125 this.props.onChange(value);
126 },
127
128 getFormat() {
129 const { format, showHour, showMinute, showSecond } = this.props;
130 if (format) {
131 return format;
132 }
133 return [
134 showHour ? 'HH' : '',
135 showMinute ? 'mm' : '',
136 showSecond ? 'ss' : '',
137 ].filter(item => !!item).join(':');
138 },
139
140 getPanelElement() {
141 const {
142 prefixCls, placeholder, disabledHours,
143 disabledMinutes, disabledSeconds, hideDisabledOptions,
144 allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText,
145 addon,
146 } = this.props;
147 return (
148 <Panel
149 clearText={clearText}
150 prefixCls={`${prefixCls}-panel`}
151 ref={this.savePanelRef}
152 value={this.state.value}
153 onChange={this.onPanelChange}
154 onClear={this.onPanelClear}
155 defaultOpenValue={defaultOpenValue}
156 showHour={showHour}
157 showMinute={showMinute}
158 showSecond={showSecond}
159 onEsc={this.onEsc}
160 allowEmpty={allowEmpty}
161 format={this.getFormat()}
162 placeholder={placeholder}
163 disabledHours={disabledHours}
164 disabledMinutes={disabledMinutes}
165 disabledSeconds={disabledSeconds}
166 hideDisabledOptions={hideDisabledOptions}
167 addon={addon}
168 />
169 );
170 },
171
172 setOpen(open) {
173 const { onOpen, onClose } = this.props;
174 if (this.state.open !== open) {
175 if (!('open' in this.props)) {
176 this.setState({ open });
177 }
178 if (open) {
179 onOpen({ open });
180 } else {
181 onClose({ open });
182 }
183 }
184 },
185
186 focus() {
187 this.picker.focus();
188 },
189
190 render() {
191 const {
192 prefixCls, placeholder, placement, align,
193 disabled, transitionName, style, className, showHour,
194 showMinute, showSecond, getPopupContainer, name, autoComplete,
195 } = this.props;
196 const { open, value } = this.state;
197 let popupClassName;
198 if (!showHour || !showMinute || !showSecond) {
199 popupClassName = `${prefixCls}-panel-narrow`;
200 }
201 return (
202 <Trigger
203 prefixCls={`${prefixCls}-panel`}
204 popupClassName={popupClassName}
205 popup={this.getPanelElement()}
206 popupAlign={align}
207 builtinPlacements={placements}
208 popupPlacement={placement}
209 action={disabled ? [] : ['click']}
210 destroyPopupOnHide
211 getPopupContainer={getPopupContainer}
212 popupTransitionName={transitionName}
213 popupVisible={open}
214 onPopupVisibleChange={this.onVisibleChange}
215 >
216 <span className={`${prefixCls} ${className}`} style={style}>
217 <input
218 className={`${prefixCls}-input`}
219 ref={this.saveInputRef}
220 type="text"
221 placeholder={placeholder}
222 name={name}
223 readOnly
224 onKeyDown={this.onKeyDown}
225 disabled={disabled} value={value && value.format(this.getFormat()) || ''}
226 autoComplete={autoComplete}
227 />
228 <span className={`${prefixCls}-icon`}/>
229 </span>
230 </Trigger>
231 );
232 },
233 });
234
235 export default Picker;