]> git.immae.eu Git - github/fretlink/time-picker.git/blob - src/Panel.jsx
Merge branch 'master' into focus-on-open
[github/fretlink/time-picker.git] / src / Panel.jsx
1 import React, { Component } from 'react';
2 import PropTypes from 'prop-types';
3 import Header from './Header';
4 import Combobox from './Combobox';
5 import moment from 'moment';
6 import classNames from 'classnames';
7
8 function noop() {
9 }
10
11 function generateOptions(length, disabledOptions, hideDisabledOptions) {
12 const arr = [];
13 for (let value = 0; value < length; value++) {
14 if (!disabledOptions || disabledOptions.indexOf(value) < 0 || !hideDisabledOptions) {
15 arr.push(value);
16 }
17 }
18 return arr;
19 }
20
21 class Panel extends Component {
22 static propTypes = {
23 clearText: PropTypes.string,
24 prefixCls: PropTypes.string,
25 className: PropTypes.string,
26 defaultOpenValue: PropTypes.object,
27 value: PropTypes.object,
28 placeholder: PropTypes.string,
29 format: PropTypes.string,
30 disabledHours: PropTypes.func,
31 disabledMinutes: PropTypes.func,
32 disabledSeconds: PropTypes.func,
33 hideDisabledOptions: PropTypes.bool,
34 onChange: PropTypes.func,
35 onEsc: PropTypes.func,
36 allowEmpty: PropTypes.bool,
37 showHour: PropTypes.bool,
38 showMinute: PropTypes.bool,
39 showSecond: PropTypes.bool,
40 onClear: PropTypes.func,
41 use12Hours: PropTypes.bool,
42 addon: PropTypes.func,
43 focusOnOpen: PropTypes.bool,
44 onKeyDown: PropTypes.func,
45 };
46
47 static defaultProps = {
48 prefixCls: 'rc-time-picker-panel',
49 onChange: noop,
50 onClear: noop,
51 disabledHours: noop,
52 disabledMinutes: noop,
53 disabledSeconds: noop,
54 defaultOpenValue: moment(),
55 use12Hours: false,
56 addon: noop,
57 onKeyDown: noop,
58 };
59
60 constructor(props) {
61 super(props);
62 this.state = {
63 value: props.value,
64 selectionRange: [],
65 };
66 }
67
68 componentWillReceiveProps(nextProps) {
69 const value = nextProps.value;
70 if (value) {
71 this.setState({
72 value,
73 });
74 }
75 }
76
77 onChange = (newValue) => {
78 this.setState({ value: newValue });
79 this.props.onChange(newValue);
80 }
81
82 onCurrentSelectPanelChange = (currentSelectPanel) => {
83 this.setState({ currentSelectPanel });
84 }
85
86 // https://github.com/ant-design/ant-design/issues/5829
87 close() {
88 this.props.onEsc();
89 }
90
91 render() {
92 const {
93 prefixCls, className, placeholder, disabledHours, disabledMinutes,
94 disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond,
95 format, defaultOpenValue, clearText, onEsc, addon, use12Hours, onClear, focusOnOpen, onKeyDown,
96 } = this.props;
97 const {
98 value, currentSelectPanel,
99 } = this.state;
100 const disabledHourOptions = disabledHours();
101 const disabledMinuteOptions = disabledMinutes(value ? value.hour() : null);
102 const disabledSecondOptions = disabledSeconds(value ? value.hour() : null,
103 value ? value.minute() : null);
104 const hourOptions = generateOptions(24, disabledHourOptions, hideDisabledOptions);
105 const minuteOptions = generateOptions(60, disabledMinuteOptions, hideDisabledOptions);
106 const secondOptions = generateOptions(60, disabledSecondOptions, hideDisabledOptions);
107
108 return (
109 <div className={classNames({ [`${prefixCls}-inner`]: true, [className]: !!className })}>
110 <Header
111 clearText={clearText}
112 prefixCls={prefixCls}
113 defaultOpenValue={defaultOpenValue}
114 value={value}
115 currentSelectPanel={currentSelectPanel}
116 onEsc={onEsc}
117 format={format}
118 placeholder={placeholder}
119 hourOptions={hourOptions}
120 minuteOptions={minuteOptions}
121 secondOptions={secondOptions}
122 disabledHours={disabledHours}
123 disabledMinutes={disabledMinutes}
124 disabledSeconds={disabledSeconds}
125 onChange={this.onChange}
126 onClear={onClear}
127 allowEmpty={allowEmpty}
128 focusOnOpen={focusOnOpen}
129 onKeyDown={onKeyDown}
130 />
131 <Combobox
132 prefixCls={prefixCls}
133 value={value}
134 defaultOpenValue={defaultOpenValue}
135 format={format}
136 onChange={this.onChange}
137 showHour={showHour}
138 showMinute={showMinute}
139 showSecond={showSecond}
140 hourOptions={hourOptions}
141 minuteOptions={minuteOptions}
142 secondOptions={secondOptions}
143 disabledHours={disabledHours}
144 disabledMinutes={disabledMinutes}
145 disabledSeconds={disabledSeconds}
146 onCurrentSelectPanelChange={this.onCurrentSelectPanelChange}
147 use12Hours={use12Hours}
148 />
149 {addon(this)}
150 </div>
151 );
152 }
153 }
154
155 export default Panel;