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