]>
Commit | Line | Data |
---|---|---|
1 | import React, { Component } from 'react'; | |
2 | import PropTypes from 'prop-types'; | |
3 | import Select from './Select'; | |
4 | ||
5 | const formatOption = (option, disabledOptions) => { | |
6 | let value = `${option}`; | |
7 | if (option < 10) { | |
8 | value = `0${option}`; | |
9 | } | |
10 | ||
11 | let disabled = false; | |
12 | if (disabledOptions && disabledOptions.indexOf(option) >= 0) { | |
13 | disabled = true; | |
14 | } | |
15 | ||
16 | return { | |
17 | value, | |
18 | disabled, | |
19 | }; | |
20 | }; | |
21 | ||
22 | class Combobox extends Component { | |
23 | static propTypes = { | |
24 | format: PropTypes.string, | |
25 | defaultOpenValue: PropTypes.object, | |
26 | prefixCls: PropTypes.string, | |
27 | value: PropTypes.object, | |
28 | onChange: PropTypes.func, | |
29 | showHour: PropTypes.bool, | |
30 | showMinute: PropTypes.bool, | |
31 | showSecond: PropTypes.bool, | |
32 | hourOptions: PropTypes.array, | |
33 | minuteOptions: PropTypes.array, | |
34 | secondOptions: PropTypes.array, | |
35 | disabledHours: PropTypes.func, | |
36 | disabledMinutes: PropTypes.func, | |
37 | disabledSeconds: PropTypes.func, | |
38 | onCurrentSelectPanelChange: PropTypes.func, | |
39 | use12Hours: PropTypes.bool, | |
40 | isAM: PropTypes.bool, | |
41 | }; | |
42 | ||
43 | onItemChange = (type, itemValue) => { | |
44 | const { onChange, defaultOpenValue, use12Hours } = this.props; | |
45 | const value = (this.props.value || defaultOpenValue).clone(); | |
46 | ||
47 | if (type === 'hour') { | |
48 | if (use12Hours) { | |
49 | if (this.props.isAM) { | |
50 | value.hour(+itemValue % 12); | |
51 | } else { | |
52 | value.hour((+itemValue % 12) + 12); | |
53 | } | |
54 | } else { | |
55 | value.hour(+itemValue); | |
56 | } | |
57 | } else if (type === 'minute') { | |
58 | value.minute(+itemValue); | |
59 | } else if (type === 'ampm') { | |
60 | const ampm = itemValue.toUpperCase(); | |
61 | if (use12Hours) { | |
62 | if (ampm === 'PM' && value.hour() < 12) { | |
63 | value.hour((value.hour() % 12) + 12); | |
64 | } | |
65 | ||
66 | if (ampm === 'AM') { | |
67 | if (value.hour() >= 12) { | |
68 | value.hour(value.hour() - 12); | |
69 | } | |
70 | } | |
71 | } | |
72 | } else { | |
73 | value.second(+itemValue); | |
74 | } | |
75 | onChange(value); | |
76 | } | |
77 | ||
78 | onEnterSelectPanel = (range) => { | |
79 | this.props.onCurrentSelectPanelChange(range); | |
80 | } | |
81 | ||
82 | getHourSelect(hour) { | |
83 | const { prefixCls, hourOptions, disabledHours, showHour, use12Hours } = this.props; | |
84 | if (!showHour) { | |
85 | return null; | |
86 | } | |
87 | const disabledOptions = disabledHours(); | |
88 | let hourOptionsAdj; | |
89 | let hourAdj; | |
90 | if (use12Hours) { | |
91 | hourOptionsAdj = [12].concat(hourOptions.filter(h => h < 12 && h > 0)); | |
92 | hourAdj = (hour % 12) || 12; | |
93 | } else { | |
94 | hourOptionsAdj = hourOptions; | |
95 | hourAdj = hour; | |
96 | } | |
97 | ||
98 | return ( | |
99 | <Select | |
100 | prefixCls={prefixCls} | |
101 | options={hourOptionsAdj.map(option => formatOption(option, disabledOptions))} | |
102 | selectedIndex={hourOptionsAdj.indexOf(hourAdj)} | |
103 | type="hour" | |
104 | onSelect={this.onItemChange} | |
105 | onMouseEnter={this.onEnterSelectPanel.bind(this, 'hour')} | |
106 | /> | |
107 | ); | |
108 | } | |
109 | ||
110 | getMinuteSelect(minute) { | |
111 | const { prefixCls, minuteOptions, disabledMinutes, defaultOpenValue, showMinute } = this.props; | |
112 | if (!showMinute) { | |
113 | return null; | |
114 | } | |
115 | const value = this.props.value || defaultOpenValue; | |
116 | const disabledOptions = disabledMinutes(value.hour()); | |
117 | ||
118 | return ( | |
119 | <Select | |
120 | prefixCls={prefixCls} | |
121 | options={minuteOptions.map(option => formatOption(option, disabledOptions))} | |
122 | selectedIndex={minuteOptions.indexOf(minute)} | |
123 | type="minute" | |
124 | onSelect={this.onItemChange} | |
125 | onMouseEnter={this.onEnterSelectPanel.bind(this, 'minute')} | |
126 | /> | |
127 | ); | |
128 | } | |
129 | ||
130 | getSecondSelect(second) { | |
131 | const { prefixCls, secondOptions, disabledSeconds, showSecond, defaultOpenValue } = this.props; | |
132 | if (!showSecond) { | |
133 | return null; | |
134 | } | |
135 | const value = this.props.value || defaultOpenValue; | |
136 | const disabledOptions = disabledSeconds(value.hour(), value.minute()); | |
137 | ||
138 | return ( | |
139 | <Select | |
140 | prefixCls={prefixCls} | |
141 | options={secondOptions.map(option => formatOption(option, disabledOptions))} | |
142 | selectedIndex={secondOptions.indexOf(second)} | |
143 | type="second" | |
144 | onSelect={this.onItemChange} | |
145 | onMouseEnter={this.onEnterSelectPanel.bind(this, 'second')} | |
146 | /> | |
147 | ); | |
148 | } | |
149 | ||
150 | getAMPMSelect() { | |
151 | const { prefixCls, use12Hours, format } = this.props; | |
152 | if (!use12Hours) { | |
153 | return null; | |
154 | } | |
155 | ||
156 | const AMPMOptions = ['am', 'pm'] // If format has A char, then we should uppercase AM/PM | |
157 | .map(c => format.match(/\sA/) ? c.toUpperCase() : c) | |
158 | .map(c => ({ value: c })); | |
159 | ||
160 | const selected = this.props.isAM ? 0 : 1; | |
161 | ||
162 | return ( | |
163 | <Select | |
164 | prefixCls={prefixCls} | |
165 | options={AMPMOptions} | |
166 | selectedIndex={selected} | |
167 | type="ampm" | |
168 | onSelect={this.onItemChange} | |
169 | onMouseEnter={this.onEnterSelectPanel.bind(this, 'ampm')} | |
170 | /> | |
171 | ); | |
172 | } | |
173 | ||
174 | render() { | |
175 | const { prefixCls, defaultOpenValue } = this.props; | |
176 | const value = this.props.value || defaultOpenValue; | |
177 | return ( | |
178 | <div className={`${prefixCls}-combobox`}> | |
179 | {this.getHourSelect(value.hour())} | |
180 | {this.getMinuteSelect(value.minute())} | |
181 | {this.getSecondSelect(value.second())} | |
182 | {this.getAMPMSelect(value.hour())} | |
183 | </div> | |
184 | ); | |
185 | } | |
186 | } | |
187 | ||
188 | export default Combobox; |