aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMarek Piechut <marek.piechut@gmail.com>2017-10-09 13:38:00 +0200
committerMarek Piechut <marek.piechut@gmail.com>2017-10-09 13:38:00 +0200
commit5827568ef882ae5a474f37ff5c843e931528ebe8 (patch)
treede49d29b22db47adcaba3dee2bdf94227fdae665
parent35d5ed7815f4707c79565a45261ffadc21d604be (diff)
downloadtime-picker-5827568ef882ae5a474f37ff5c843e931528ebe8.tar.gz
time-picker-5827568ef882ae5a474f37ff5c843e931528ebe8.tar.zst
time-picker-5827568ef882ae5a474f37ff5c843e931528ebe8.zip
Add hourStep, minuteStep and secondStep props to control intervals in picker
-rw-r--r--README.md3
-rw-r--r--src/Panel.jsx20
-rw-r--r--src/TimePicker.jsx8
-rw-r--r--tests/Select.spec.jsx34
4 files changed, 59 insertions, 6 deletions
diff --git a/README.md b/README.md
index a2758b9..26e05fc 100644
--- a/README.md
+++ b/README.md
@@ -77,6 +77,9 @@ API
77| name | String | - | sets the name of the generated input | 77| name | String | - | sets the name of the generated input |
78| onOpen | Function({ open }) | | when TimePicker panel is opened | 78| onOpen | Function({ open }) | | when TimePicker panel is opened |
79| onClose | Function({ open }) | | when TimePicker panel is opened | 79| onClose | Function({ open }) | | when TimePicker panel is opened |
80| hourStep | Number | 1 | interval between hours in picker |
81| minuteStep | Number | 1 | interval between minutes in picker |
82| secondStep | Number | 1 | interval between seconds in picker |
80 83
81## Test Case 84## Test Case
82 85
diff --git a/src/Panel.jsx b/src/Panel.jsx
index 1953ad4..468eb37 100644
--- a/src/Panel.jsx
+++ b/src/Panel.jsx
@@ -8,9 +8,9 @@ import classNames from 'classnames';
8function noop() { 8function noop() {
9} 9}
10 10
11function generateOptions(length, disabledOptions, hideDisabledOptions) { 11function generateOptions(length, disabledOptions, hideDisabledOptions, step = 1) {
12 const arr = []; 12 const arr = [];
13 for (let value = 0; value < length; value++) { 13 for (let value = 0; value < length; value += step) {
14 if (!disabledOptions || disabledOptions.indexOf(value) < 0 || !hideDisabledOptions) { 14 if (!disabledOptions || disabledOptions.indexOf(value) < 0 || !hideDisabledOptions) {
15 arr.push(value); 15 arr.push(value);
16 } 16 }
@@ -39,6 +39,9 @@ class Panel extends Component {
39 showSecond: PropTypes.bool, 39 showSecond: PropTypes.bool,
40 onClear: PropTypes.func, 40 onClear: PropTypes.func,
41 use12Hours: PropTypes.bool, 41 use12Hours: PropTypes.bool,
42 hourStep: PropTypes.number,
43 minuteStep: PropTypes.number,
44 secondStep: PropTypes.number,
42 addon: PropTypes.func, 45 addon: PropTypes.func,
43 }; 46 };
44 47
@@ -90,6 +93,7 @@ class Panel extends Component {
90 prefixCls, className, placeholder, disabledHours, disabledMinutes, 93 prefixCls, className, placeholder, disabledHours, disabledMinutes,
91 disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond, 94 disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond,
92 format, defaultOpenValue, clearText, onEsc, addon, use12Hours, onClear, 95 format, defaultOpenValue, clearText, onEsc, addon, use12Hours, onClear,
96 hourStep, minuteStep, secondStep,
93 } = this.props; 97 } = this.props;
94 const { 98 const {
95 value, currentSelectPanel, 99 value, currentSelectPanel,
@@ -98,9 +102,15 @@ class Panel extends Component {
98 const disabledMinuteOptions = disabledMinutes(value ? value.hour() : null); 102 const disabledMinuteOptions = disabledMinutes(value ? value.hour() : null);
99 const disabledSecondOptions = disabledSeconds(value ? value.hour() : null, 103 const disabledSecondOptions = disabledSeconds(value ? value.hour() : null,
100 value ? value.minute() : null); 104 value ? value.minute() : null);
101 const hourOptions = generateOptions(24, disabledHourOptions, hideDisabledOptions); 105 const hourOptions = generateOptions(
102 const minuteOptions = generateOptions(60, disabledMinuteOptions, hideDisabledOptions); 106 24, disabledHourOptions, hideDisabledOptions, hourStep
103 const secondOptions = generateOptions(60, disabledSecondOptions, hideDisabledOptions); 107 );
108 const minuteOptions = generateOptions(
109 60, disabledMinuteOptions, hideDisabledOptions, minuteStep
110 );
111 const secondOptions = generateOptions(
112 60, disabledSecondOptions, hideDisabledOptions, secondStep
113 );
104 114
105 return ( 115 return (
106 <div className={classNames({ [`${prefixCls}-inner`]: true, [className]: !!className })}> 116 <div className={classNames({ [`${prefixCls}-inner`]: true, [className]: !!className })}>
diff --git a/src/TimePicker.jsx b/src/TimePicker.jsx
index 2c6a1f1..85dc3e2 100644
--- a/src/TimePicker.jsx
+++ b/src/TimePicker.jsx
@@ -46,6 +46,9 @@ export default class Picker extends Component {
46 name: PropTypes.string, 46 name: PropTypes.string,
47 autoComplete: PropTypes.string, 47 autoComplete: PropTypes.string,
48 use12Hours: PropTypes.bool, 48 use12Hours: PropTypes.bool,
49 hourStep: PropTypes.number,
50 minuteStep: PropTypes.number,
51 secondStep: PropTypes.number,
49 }; 52 };
50 53
51 static defaultProps = { 54 static defaultProps = {
@@ -157,7 +160,7 @@ export default class Picker extends Component {
157 prefixCls, placeholder, disabledHours, 160 prefixCls, placeholder, disabledHours,
158 disabledMinutes, disabledSeconds, hideDisabledOptions, 161 disabledMinutes, disabledSeconds, hideDisabledOptions,
159 allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText, 162 allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText,
160 addon, use12Hours, 163 addon, use12Hours, hourStep, minuteStep, secondStep,
161 } = this.props; 164 } = this.props;
162 return ( 165 return (
163 <Panel 166 <Panel
@@ -180,6 +183,9 @@ export default class Picker extends Component {
180 disabledSeconds={disabledSeconds} 183 disabledSeconds={disabledSeconds}
181 hideDisabledOptions={hideDisabledOptions} 184 hideDisabledOptions={hideDisabledOptions}
182 use12Hours={use12Hours} 185 use12Hours={use12Hours}
186 hourStep={hourStep}
187 minuteStep={minuteStep}
188 secondStep={secondStep}
183 addon={addon} 189 addon={addon}
184 /> 190 />
185 ); 191 );
diff --git a/tests/Select.spec.jsx b/tests/Select.spec.jsx
index b2c111d..2a15e7c 100644
--- a/tests/Select.spec.jsx
+++ b/tests/Select.spec.jsx
@@ -7,6 +7,8 @@ import expect from 'expect.js';
7import async from 'async'; 7import async from 'async';
8import moment from 'moment'; 8import moment from 'moment';
9 9
10const map = (arr, fn) => Array.prototype.map.call(arr, fn);
11
10describe('Select', () => { 12describe('Select', () => {
11 let container; 13 let container;
12 14
@@ -59,6 +61,38 @@ describe('Select', () => {
59 done(); 61 done();
60 }); 62 });
61 }); 63 });
64
65 it('shows only numbers according to step props', (done) => {
66 const picker = renderPicker({
67 hourStep: 5,
68 minuteStep: 15,
69 secondStep: 21,
70 });
71 const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
72 'rc-time-picker-input')[0];
73 async.series([(next) => {
74 Simulate.click(input);
75 setTimeout(next, 100);
76 }, (next) => {
77 const selectors = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
78 'rc-time-picker-panel-select');
79
80 const hourSelector = selectors[0];
81 const minuteSelector = selectors[1];
82 const secondSelector = selectors[2];
83
84 const hours = map(hourSelector.getElementsByTagName('li'), (li) => li.innerHTML);
85 expect(hours).to.eql(['00', '05', '10', '15', '20']);
86
87 const minutes = map(minuteSelector.getElementsByTagName('li'), (li) => li.innerHTML);
88 expect(minutes).to.eql(['00', '15', '30', '45']);
89
90 const seconds = map(secondSelector.getElementsByTagName('li'), (li) => li.innerHTML);
91 expect(seconds).to.eql(['00', '21', '42']);
92
93 next();
94 }], done);
95 });
62 }); 96 });
63 97
64 describe('select number', () => { 98 describe('select number', () => {