]> git.immae.eu Git - github/fretlink/time-picker.git/blame - src/TimePicker.jsx
lint
[github/fretlink/time-picker.git] / src / TimePicker.jsx
CommitLineData
02de449a 1import React, {PropTypes} from 'react';
02de449a 2import Trigger from 'rc-trigger';
15bb18d9 3import Panel from './module/Panel';
02de449a 4import placements from './util/placements';
5import CommonMixin from './mixin/CommonMixin';
8133e8cf 6import {getFormatter} from './util/index';
7import defaultGregorianCalendarLocale from 'gregorian-calendar/lib/locale/en_US';
02de449a 8
9function noop() {
10}
11
12function refFn(field, component) {
13 this[field] = component;
14}
15
16const Picker = React.createClass({
17 propTypes: {
18 prefixCls: PropTypes.string,
4acbf95c 19 locale: PropTypes.object,
9f9f39e4 20 value: PropTypes.object,
02de449a 21 disabled: PropTypes.bool,
8133e8cf 22 allowEmpty: PropTypes.bool,
63541ed7 23 defaultValue: PropTypes.object,
02de449a 24 open: PropTypes.bool,
8133e8cf 25 defaultOpen: PropTypes.bool,
02de449a 26 align: PropTypes.object,
27 placement: PropTypes.any,
28 transitionName: PropTypes.string,
8133e8cf 29 getPopupContainer: PropTypes.func,
30 gregorianCalendarLocale: PropTypes.object,
4acbf95c 31 placeholder: PropTypes.string,
8133e8cf 32 formatter: PropTypes.any,
33 showHour: PropTypes.bool,
34 style: PropTypes.object,
35 showSecond: PropTypes.bool,
4acbf95c
M
36 hourOptions: PropTypes.array,
37 minuteOptions: PropTypes.array,
38 secondOptions: PropTypes.array,
02de449a 39 onChange: PropTypes.func,
40 onOpen: PropTypes.func,
41 onClose: PropTypes.func,
42 },
43
44 mixins: [CommonMixin],
45
46 getDefaultProps() {
47 return {
8133e8cf 48 defaultOpen: false,
49 style: {},
50 gregorianCalendarLocale: defaultGregorianCalendarLocale,
02de449a 51 align: {},
8133e8cf 52 allowEmpty: true,
53 showHour: true,
54 showSecond: true,
02de449a 55 placement: 'bottomLeft',
56 onChange: noop,
57 onOpen: noop,
58 onClose: noop,
59 };
60 },
61
62 getInitialState() {
63 this.savePanelRef = refFn.bind(this, 'panelInstance');
8133e8cf 64 const { defaultOpen, defaultValue, open = defaultOpen, value = defaultValue } = this.props;
e75ed0c6 65 return {
8133e8cf 66 open,
67 value,
e75ed0c6 68 };
02de449a 69 },
70
02de449a 71 componentWillReceiveProps(nextProps) {
9f9f39e4 72 const { value, open } = nextProps;
73 if (value !== undefined) {
74 this.setState({
75 value,
76 });
02de449a 77 }
78 if (open !== undefined) {
79 this.setState({open});
80 }
81 },
82
02de449a 83 onPanelChange(value) {
9f9f39e4 84 this.setValue(value);
02de449a 85 },
86
87 onPanelClear() {
7702bb67 88 this.setValue(null);
63541ed7 89 this.setOpen(false);
02de449a 90 },
91
92 onVisibleChange(open) {
8133e8cf 93 this.setOpen(open);
94 },
95
96 onEsc() {
97 this.setOpen(false);
98 this.refs.picker.focus();
99 },
100
101 onKeyDown(e) {
102 if (e.keyCode === 40) {
103 this.setOpen(true);
104 }
02de449a 105 },
106
9f9f39e4 107 setValue(value) {
108 if (!('value' in this.props)) {
109 this.setState({
110 value,
111 });
112 }
113 this.props.onChange(value);
114 },
115
8133e8cf 116 getFormatter() {
117 const formatter = this.props.formatter;
118 const locale = this.props.locale;
119 if (formatter) {
120 if (formatter === this.lastFormatter) {
121 return this.normalFormatter;
122 }
123 this.normalFormatter = getFormatter(formatter, locale);
124 this.lastFormatter = formatter;
125 return this.normalFormatter;
126 }
127 if (!this.props.showSecond) {
128 if (!this.notShowSecondFormatter) {
129 this.notShowSecondFormatter = getFormatter('HH:mm', locale);
130 }
131 return this.notShowSecondFormatter;
132 }
133 if (!this.props.showHour) {
134 if (!this.notShowHourFormatter) {
135 this.notShowHourFormatter = getFormatter('mm:ss', locale);
136 }
137 return this.notShowHourFormatter;
138 }
139 if (!this.normalFormatter) {
140 this.normalFormatter = getFormatter('HH:mm:ss', locale);
141 }
142 return this.normalFormatter;
143 },
144
145 getPanelElement() {
146 const { prefixCls, defaultValue, locale, placeholder, hourOptions, minuteOptions, secondOptions, allowEmpty, showHour, showSecond, gregorianCalendarLocale, value } = this.props;
147 let calendarLocale;
148 if (value) {
149 calendarLocale = value.locale;
150 } else if (defaultValue) {
151 calendarLocale = defaultValue.locale;
152 } else {
153 calendarLocale = gregorianCalendarLocale;
154 }
4acbf95c
M
155 return (
156 <Panel
8133e8cf 157 prefixCls={`${prefixCls}-panel`}
158 ref={this.savePanelRef}
159 value={this.state.value}
160 onChange={this.onPanelChange}
161 gregorianCalendarLocale={calendarLocale}
162 onClear={this.onPanelClear}
63541ed7 163 defaultValue={defaultValue}
8133e8cf 164 showHour={showHour}
165 onEsc={this.onEsc}
166 showSecond={showSecond}
4acbf95c 167 locale={locale}
8133e8cf 168 allowEmpty={allowEmpty}
169 formatter={this.getFormatter()}
4acbf95c
M
170 placeholder={placeholder}
171 hourOptions={hourOptions}
172 minuteOptions={minuteOptions}
173 secondOptions={secondOptions}
174 />
175 );
176 },
177
02de449a 178 setOpen(open, callback) {
179 const {onOpen, onClose} = this.props;
180 if (this.state.open !== open) {
181 this.setState({
8133e8cf 182 open,
02de449a 183 }, callback);
184 const event = {
8133e8cf 185 open,
02de449a 186 };
187 if (open) {
188 onOpen(event);
189 } else {
190 onClose(event);
191 }
192 }
193 },
194
02de449a 195 render() {
8133e8cf 196 const { prefixCls, placeholder, placement, align, disabled, transitionName, style, showHour, showSecond, getPopupContainer } = this.props;
4acbf95c 197 const { open, value } = this.state;
8133e8cf 198 let popupClassName;
199 if (!showHour || !showSecond) {
200 popupClassName = `${prefixCls}-panel-narrow}`;
201 }
02de449a 202 return (
203 <Trigger
96a4cefc 204 prefixCls={`${prefixCls}-panel`}
8133e8cf 205 popupClassName={popupClassName}
02de449a 206 popup={this.getPanelElement()}
207 popupAlign={align}
208 builtinPlacements={placements}
209 popupPlacement={placement}
210 action={disabled ? [] : ['click']}
211 destroyPopupOnHide
8133e8cf 212 getPopupContainer={getPopupContainer}
02de449a 213 popupTransitionName={transitionName}
4acbf95c 214 popupVisible={open}
02de449a 215 onPopupVisibleChange={this.onVisibleChange}
216 >
8133e8cf 217 <span className={`${prefixCls}`} style={style}>
218 <input className={`${prefixCls}-input`}
219 ref="picker" type="text" placeholder={placeholder}
220 readOnly
221 onKeyDown={this.onKeyDown}
222 disabled={disabled} value={value && this.getFormatter().format(value)}/>
9f9f39e4 223 <span className={`${prefixCls}-icon`}/>
02de449a 224 </span>
225 </Trigger>
226 );
227 },
228});
229
230export default Picker;