diff options
Diffstat (limited to 'src/TimePicker.jsx')
-rw-r--r-- | src/TimePicker.jsx | 69 |
1 files changed, 31 insertions, 38 deletions
diff --git a/src/TimePicker.jsx b/src/TimePicker.jsx index 350eeb2..358b596 100644 --- a/src/TimePicker.jsx +++ b/src/TimePicker.jsx | |||
@@ -1,9 +1,8 @@ | |||
1 | import React, { PropTypes } from 'react'; | 1 | import React, { PropTypes } from 'react'; |
2 | import Trigger from 'rc-trigger'; | 2 | import Trigger from 'rc-trigger'; |
3 | import Panel from './module/Panel'; | 3 | import Panel from './Panel'; |
4 | import placements from './util/placements'; | 4 | import placements from './placements'; |
5 | import CommonMixin from './mixin/CommonMixin'; | 5 | import moment from 'moment'; |
6 | import { getFormatter } from './util/index'; | ||
7 | 6 | ||
8 | function noop() { | 7 | function noop() { |
9 | } | 8 | } |
@@ -15,8 +14,9 @@ function refFn(field, component) { | |||
15 | const Picker = React.createClass({ | 14 | const Picker = React.createClass({ |
16 | propTypes: { | 15 | propTypes: { |
17 | prefixCls: PropTypes.string, | 16 | prefixCls: PropTypes.string, |
18 | locale: PropTypes.object, | 17 | clearText: PropTypes.string, |
19 | value: PropTypes.object, | 18 | value: PropTypes.object, |
19 | defaultOpenValue: PropTypes.object, | ||
20 | disabled: PropTypes.bool, | 20 | disabled: PropTypes.bool, |
21 | allowEmpty: PropTypes.bool, | 21 | allowEmpty: PropTypes.bool, |
22 | defaultValue: PropTypes.object, | 22 | defaultValue: PropTypes.object, |
@@ -27,7 +27,7 @@ const Picker = React.createClass({ | |||
27 | transitionName: PropTypes.string, | 27 | transitionName: PropTypes.string, |
28 | getPopupContainer: PropTypes.func, | 28 | getPopupContainer: PropTypes.func, |
29 | placeholder: PropTypes.string, | 29 | placeholder: PropTypes.string, |
30 | formatter: PropTypes.any, | 30 | format: PropTypes.string, |
31 | showHour: PropTypes.bool, | 31 | showHour: PropTypes.bool, |
32 | style: PropTypes.object, | 32 | style: PropTypes.object, |
33 | className: PropTypes.string, | 33 | className: PropTypes.string, |
@@ -39,16 +39,18 @@ const Picker = React.createClass({ | |||
39 | onChange: PropTypes.func, | 39 | onChange: PropTypes.func, |
40 | onOpen: PropTypes.func, | 40 | onOpen: PropTypes.func, |
41 | onClose: PropTypes.func, | 41 | onClose: PropTypes.func, |
42 | addon: PropTypes.func, | ||
42 | }, | 43 | }, |
43 | 44 | ||
44 | mixins: [CommonMixin], | ||
45 | |||
46 | getDefaultProps() { | 45 | getDefaultProps() { |
47 | return { | 46 | return { |
47 | clearText: 'clear', | ||
48 | prefixCls: 'rc-time-picker', | ||
48 | defaultOpen: false, | 49 | defaultOpen: false, |
49 | style: {}, | 50 | style: {}, |
50 | className: '', | 51 | className: '', |
51 | align: {}, | 52 | align: {}, |
53 | defaultOpenValue: moment(), | ||
52 | allowEmpty: true, | 54 | allowEmpty: true, |
53 | showHour: true, | 55 | showHour: true, |
54 | showSecond: true, | 56 | showSecond: true, |
@@ -60,6 +62,7 @@ const Picker = React.createClass({ | |||
60 | onChange: noop, | 62 | onChange: noop, |
61 | onOpen: noop, | 63 | onOpen: noop, |
62 | onClose: noop, | 64 | onClose: noop, |
65 | addon: noop, | ||
63 | }; | 66 | }; |
64 | }, | 67 | }, |
65 | 68 | ||
@@ -117,61 +120,47 @@ const Picker = React.createClass({ | |||
117 | this.props.onChange(value); | 120 | this.props.onChange(value); |
118 | }, | 121 | }, |
119 | 122 | ||
120 | getFormatter() { | 123 | getFormat() { |
121 | const formatter = this.props.formatter; | 124 | const format = this.props.format; |
122 | const locale = this.props.locale; | 125 | if (format) { |
123 | if (formatter) { | 126 | return format; |
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 | } | 127 | } |
131 | if (!this.props.showSecond) { | 128 | if (!this.props.showSecond) { |
132 | if (!this.notShowSecondFormatter) { | 129 | return 'HH:mm'; |
133 | this.notShowSecondFormatter = getFormatter('HH:mm', locale); | ||
134 | } | ||
135 | return this.notShowSecondFormatter; | ||
136 | } | 130 | } |
137 | if (!this.props.showHour) { | 131 | if (!this.props.showHour) { |
138 | if (!this.notShowHourFormatter) { | 132 | return 'mm:ss'; |
139 | this.notShowHourFormatter = getFormatter('mm:ss', locale); | ||
140 | } | ||
141 | return this.notShowHourFormatter; | ||
142 | } | 133 | } |
143 | if (!this.normalFormatter) { | 134 | return 'HH:mm:ss'; |
144 | this.normalFormatter = getFormatter('HH:mm:ss', locale); | ||
145 | } | ||
146 | return this.normalFormatter; | ||
147 | }, | 135 | }, |
148 | 136 | ||
149 | getPanelElement() { | 137 | getPanelElement() { |
150 | const { | 138 | const { |
151 | prefixCls, defaultValue, locale, placeholder, disabledHours, | 139 | prefixCls, placeholder, disabledHours, |
152 | disabledMinutes, disabledSeconds, hideDisabledOptions, | 140 | disabledMinutes, disabledSeconds, hideDisabledOptions, |
153 | allowEmpty, showHour, showSecond, | 141 | allowEmpty, showHour, showSecond, defaultOpenValue, clearText, |
142 | addon, | ||
154 | } = this.props; | 143 | } = this.props; |
155 | return ( | 144 | return ( |
156 | <Panel | 145 | <Panel |
146 | clearText={clearText} | ||
157 | prefixCls={`${prefixCls}-panel`} | 147 | prefixCls={`${prefixCls}-panel`} |
158 | ref={this.savePanelRef} | 148 | ref={this.savePanelRef} |
159 | value={this.state.value} | 149 | value={this.state.value} |
160 | onChange={this.onPanelChange} | 150 | onChange={this.onPanelChange} |
161 | gregorianCalendarLocale={locale.calendar} | ||
162 | onClear={this.onPanelClear} | 151 | onClear={this.onPanelClear} |
163 | defaultValue={defaultValue} | 152 | defaultOpenValue={defaultOpenValue} |
164 | showHour={showHour} | 153 | showHour={showHour} |
165 | onEsc={this.onEsc} | 154 | onEsc={this.onEsc} |
166 | showSecond={showSecond} | 155 | showSecond={showSecond} |
167 | locale={locale} | ||
168 | allowEmpty={allowEmpty} | 156 | allowEmpty={allowEmpty} |
169 | formatter={this.getFormatter()} | 157 | format={this.getFormat()} |
170 | placeholder={placeholder} | 158 | placeholder={placeholder} |
171 | disabledHours={disabledHours} | 159 | disabledHours={disabledHours} |
172 | disabledMinutes={disabledMinutes} | 160 | disabledMinutes={disabledMinutes} |
173 | disabledSeconds={disabledSeconds} | 161 | disabledSeconds={disabledSeconds} |
174 | hideDisabledOptions={hideDisabledOptions} | 162 | hideDisabledOptions={hideDisabledOptions} |
163 | addon={addon} | ||
175 | /> | 164 | /> |
176 | ); | 165 | ); |
177 | }, | 166 | }, |
@@ -194,7 +183,11 @@ const Picker = React.createClass({ | |||
194 | }, | 183 | }, |
195 | 184 | ||
196 | render() { | 185 | render() { |
197 | const { prefixCls, placeholder, placement, align, disabled, transitionName, style, className, showHour, showSecond, getPopupContainer } = this.props; | 186 | const { |
187 | prefixCls, placeholder, placement, align, | ||
188 | disabled, transitionName, style, className, showHour, | ||
189 | showSecond, getPopupContainer, | ||
190 | } = this.props; | ||
198 | const { open, value } = this.state; | 191 | const { open, value } = this.state; |
199 | let popupClassName; | 192 | let popupClassName; |
200 | if (!showHour || !showSecond) { | 193 | if (!showHour || !showSecond) { |
@@ -221,7 +214,7 @@ const Picker = React.createClass({ | |||
221 | ref="picker" type="text" placeholder={placeholder} | 214 | ref="picker" type="text" placeholder={placeholder} |
222 | readOnly | 215 | readOnly |
223 | onKeyDown={this.onKeyDown} | 216 | onKeyDown={this.onKeyDown} |
224 | disabled={disabled} value={value && this.getFormatter().format(value) || ''} | 217 | disabled={disabled} value={value && value.format(this.getFormat()) || ''} |
225 | /> | 218 | /> |
226 | <span className={`${prefixCls}-icon`}/> | 219 | <span className={`${prefixCls}-icon`}/> |
227 | </span> | 220 | </span> |