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