]>
Commit | Line | Data |
---|---|---|
1 | import React, { Component } from 'react'; | |
2 | import PropTypes from 'prop-types'; | |
3 | import Trigger from 'rc-trigger'; | |
4 | import Panel from './Panel'; | |
5 | import placements from './placements'; | |
6 | import moment from 'moment'; | |
7 | ||
8 | function noop() { | |
9 | } | |
10 | ||
11 | function refFn(field, component) { | |
12 | this[field] = component; | |
13 | } | |
14 | ||
15 | class Picker extends Component { | |
16 | static propTypes = { | |
17 | prefixCls: PropTypes.string, | |
18 | clearText: PropTypes.string, | |
19 | value: PropTypes.object, | |
20 | defaultOpenValue: 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 | placeholder: PropTypes.string, | |
31 | format: PropTypes.string, | |
32 | showHour: PropTypes.bool, | |
33 | showMinute: PropTypes.bool, | |
34 | showSecond: PropTypes.bool, | |
35 | style: PropTypes.object, | |
36 | className: PropTypes.string, | |
37 | disabledHours: PropTypes.func, | |
38 | disabledMinutes: PropTypes.func, | |
39 | disabledSeconds: PropTypes.func, | |
40 | hideDisabledOptions: PropTypes.bool, | |
41 | onChange: PropTypes.func, | |
42 | onOpen: PropTypes.func, | |
43 | onClose: PropTypes.func, | |
44 | addon: PropTypes.func, | |
45 | name: PropTypes.string, | |
46 | autoComplete: PropTypes.string, | |
47 | use12Hours: PropTypes.bool, | |
48 | }; | |
49 | ||
50 | static defaultProps = { | |
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 | constructor(props) { | |
75 | super(props); | |
76 | this.saveInputRef = refFn.bind(this, 'picker'); | |
77 | this.savePanelRef = refFn.bind(this, 'panelInstance'); | |
78 | const { defaultOpen, defaultValue, open = defaultOpen, value = defaultValue } = props; | |
79 | this.state = { | |
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, use12Hours } = this.props; | |
132 | if (format) { | |
133 | return format; | |
134 | } | |
135 | ||
136 | if (use12Hours) { | |
137 | const fmtString = ([ | |
138 | showHour ? 'h' : '', | |
139 | showMinute ? 'mm' : '', | |
140 | showSecond ? 'ss' : '', | |
141 | ].filter(item => !!item).join(':')); | |
142 | ||
143 | return fmtString.concat(' a'); | |
144 | } | |
145 | ||
146 | return [ | |
147 | showHour ? 'HH' : '', | |
148 | showMinute ? 'mm' : '', | |
149 | showSecond ? 'ss' : '', | |
150 | ].filter(item => !!item).join(':'); | |
151 | } | |
152 | ||
153 | getPanelElement() { | |
154 | const { | |
155 | prefixCls, placeholder, disabledHours, | |
156 | disabledMinutes, disabledSeconds, hideDisabledOptions, | |
157 | allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText, | |
158 | addon, use12Hours, | |
159 | } = this.props; | |
160 | return ( | |
161 | <Panel | |
162 | clearText={clearText} | |
163 | prefixCls={`${prefixCls}-panel`} | |
164 | ref={this.savePanelRef} | |
165 | value={this.state.value} | |
166 | onChange={this.onPanelChange} | |
167 | onClear={this.onPanelClear} | |
168 | defaultOpenValue={defaultOpenValue} | |
169 | showHour={showHour} | |
170 | showMinute={showMinute} | |
171 | showSecond={showSecond} | |
172 | onEsc={this.onEsc} | |
173 | allowEmpty={allowEmpty} | |
174 | format={this.getFormat()} | |
175 | placeholder={placeholder} | |
176 | disabledHours={disabledHours} | |
177 | disabledMinutes={disabledMinutes} | |
178 | disabledSeconds={disabledSeconds} | |
179 | hideDisabledOptions={hideDisabledOptions} | |
180 | use12Hours={use12Hours} | |
181 | addon={addon} | |
182 | /> | |
183 | ); | |
184 | } | |
185 | ||
186 | setOpen(open) { | |
187 | const { onOpen, onClose } = this.props; | |
188 | if (this.state.open !== open) { | |
189 | if (!('open' in this.props)) { | |
190 | this.setState({ open }); | |
191 | } | |
192 | if (open) { | |
193 | onOpen({ open }); | |
194 | } else { | |
195 | onClose({ open }); | |
196 | } | |
197 | } | |
198 | } | |
199 | ||
200 | focus() { | |
201 | this.picker.focus(); | |
202 | } | |
203 | ||
204 | render() { | |
205 | const { | |
206 | prefixCls, placeholder, placement, align, | |
207 | disabled, transitionName, style, className, showHour, | |
208 | showMinute, showSecond, getPopupContainer, name, autoComplete, | |
209 | use12Hours, | |
210 | } = this.props; | |
211 | const { open, value } = this.state; | |
212 | let popupClassName; | |
213 | if ((!showHour || !showMinute || !showSecond) && !use12Hours) { | |
214 | popupClassName = `${prefixCls}-panel-narrow`; | |
215 | } | |
216 | return ( | |
217 | <Trigger | |
218 | prefixCls={`${prefixCls}-panel`} | |
219 | popupClassName={popupClassName} | |
220 | popup={this.getPanelElement()} | |
221 | popupAlign={align} | |
222 | builtinPlacements={placements} | |
223 | popupPlacement={placement} | |
224 | action={disabled ? [] : ['click']} | |
225 | destroyPopupOnHide | |
226 | getPopupContainer={getPopupContainer} | |
227 | popupTransitionName={transitionName} | |
228 | popupVisible={open} | |
229 | onPopupVisibleChange={this.onVisibleChange} | |
230 | > | |
231 | <span className={`${prefixCls} ${className}`} style={style}> | |
232 | <input | |
233 | className={`${prefixCls}-input`} | |
234 | ref={this.saveInputRef} | |
235 | type="text" | |
236 | placeholder={placeholder} | |
237 | name={name} | |
238 | readOnly | |
239 | onKeyDown={this.onKeyDown} | |
240 | disabled={disabled} value={value && value.format(this.getFormat()) || ''} | |
241 | autoComplete={autoComplete} | |
242 | /> | |
243 | <span className={`${prefixCls}-icon`}/> | |
244 | </span> | |
245 | </Trigger> | |
246 | ); | |
247 | } | |
248 | } | |
249 | ||
250 | export default Picker; |