aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Panel.jsx
diff options
context:
space:
mode:
authoryiminghe <yiminghe@gmail.com>2016-08-04 19:53:55 +0800
committeryiminghe <yiminghe@gmail.com>2016-08-04 19:53:55 +0800
commit4984ed85e54f442998a335db70618d6184fa397e (patch)
tree6ae348b2cac5f48f3afb6f7b8dd0c2fd02f044fc /src/Panel.jsx
parentdeaa6062ea2e274d50d58c70251c1237c0c03c67 (diff)
downloadtime-picker-4984ed85e54f442998a335db70618d6184fa397e.tar.gz
time-picker-4984ed85e54f442998a335db70618d6184fa397e.tar.zst
time-picker-4984ed85e54f442998a335db70618d6184fa397e.zip
2.x :boom:2.0.0
Diffstat (limited to 'src/Panel.jsx')
-rw-r--r--src/Panel.jsx136
1 files changed, 136 insertions, 0 deletions
diff --git a/src/Panel.jsx b/src/Panel.jsx
new file mode 100644
index 0000000..f70cf38
--- /dev/null
+++ b/src/Panel.jsx
@@ -0,0 +1,136 @@
1import React, { PropTypes } from 'react';
2import Header from './Header';
3import Combobox from './Combobox';
4import moment from 'moment';
5
6function noop() {
7}
8
9function generateOptions(length, disabledOptions, hideDisabledOptions) {
10 const arr = [];
11 for (let value = 0; value < length; value++) {
12 if (!disabledOptions || disabledOptions.indexOf(value) < 0 || !hideDisabledOptions) {
13 arr.push(value);
14 }
15 }
16 return arr;
17}
18
19const Panel = React.createClass({
20 propTypes: {
21 clearText: PropTypes.string,
22 prefixCls: PropTypes.string,
23 defaultOpenValue: PropTypes.object,
24 value: PropTypes.object,
25 placeholder: PropTypes.string,
26 format: PropTypes.string,
27 disabledHours: PropTypes.func,
28 disabledMinutes: PropTypes.func,
29 disabledSeconds: PropTypes.func,
30 hideDisabledOptions: PropTypes.bool,
31 onChange: PropTypes.func,
32 onEsc: PropTypes.func,
33 allowEmpty: PropTypes.bool,
34 showHour: PropTypes.bool,
35 showSecond: PropTypes.bool,
36 onClear: PropTypes.func,
37 },
38
39 getDefaultProps() {
40 return {
41 prefixCls: 'rc-time-picker-panel',
42 onChange: noop,
43 onClear: noop,
44 defaultOpenValue: moment(),
45 };
46 },
47
48 getInitialState() {
49 return {
50 value: this.props.value,
51 selectionRange: [],
52 };
53 },
54
55 componentWillReceiveProps(nextProps) {
56 const value = nextProps.value;
57 if (value) {
58 this.setState({
59 value,
60 });
61 }
62 },
63
64 onChange(newValue) {
65 this.setState({ value: newValue });
66 this.props.onChange(newValue);
67 },
68
69 onClear() {
70 this.props.onClear();
71 },
72
73 onCurrentSelectPanelChange(currentSelectPanel) {
74 this.setState({ currentSelectPanel });
75 },
76
77 render() {
78 const {
79 prefixCls, placeholder, disabledHours, disabledMinutes,
80 disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showSecond,
81 format, defaultOpenValue, clearText, onEsc,
82 } = this.props;
83 const {
84 value, currentSelectPanel,
85 } = this.state;
86 const disabledHourOptions = disabledHours();
87 const disabledMinuteOptions = disabledMinutes(value ? value.hour() : null);
88 const disabledSecondOptions = disabledSeconds(value ? value.hour() : null,
89 value ? value.minute() : null);
90 const hourOptions = generateOptions(24, disabledHourOptions, hideDisabledOptions);
91 const minuteOptions = generateOptions(60, disabledMinuteOptions, hideDisabledOptions);
92 const secondOptions = generateOptions(60, disabledSecondOptions, hideDisabledOptions);
93
94 return (
95 <div className={`${prefixCls}-inner`}>
96 <Header
97 clearText={clearText}
98 prefixCls={prefixCls}
99 defaultOpenValue={defaultOpenValue}
100 value={value}
101 currentSelectPanel={currentSelectPanel}
102 onEsc={onEsc}
103 format={format}
104 placeholder={placeholder}
105 hourOptions={hourOptions}
106 minuteOptions={minuteOptions}
107 secondOptions={secondOptions}
108 disabledHours={disabledHours}
109 disabledMinutes={disabledMinutes}
110 disabledSeconds={disabledSeconds}
111 onChange={this.onChange}
112 onClear={this.onClear}
113 allowEmpty={allowEmpty}
114 />
115 <Combobox
116 prefixCls={prefixCls}
117 value={value}
118 defaultOpenValue={defaultOpenValue}
119 format={format}
120 onChange={this.onChange}
121 showHour={showHour}
122 showSecond={showSecond}
123 hourOptions={hourOptions}
124 minuteOptions={minuteOptions}
125 secondOptions={secondOptions}
126 disabledHours={disabledHours}
127 disabledMinutes={disabledMinutes}
128 disabledSeconds={disabledSeconds}
129 onCurrentSelectPanelChange={this.onCurrentSelectPanelChange}
130 />
131 </div>
132 );
133 },
134});
135
136export default Panel;