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