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