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