diff options
author | 偏右 <afc163@gmail.com> | 2017-10-22 16:53:32 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-22 16:53:32 +0800 |
commit | 81cdb7024e669b0c2c8cc1911776d47683e3500d (patch) | |
tree | eb9bf3c0b111087cbf868ec434baba96b539be5d | |
parent | 8c22015207c1927b27c90b4bf0f40dc9c56d2fe5 (diff) | |
parent | f0ac29c61df164ab3e867ef86137faa13d85a87f (diff) | |
download | time-picker-81cdb7024e669b0c2c8cc1911776d47683e3500d.tar.gz time-picker-81cdb7024e669b0c2c8cc1911776d47683e3500d.tar.zst time-picker-81cdb7024e669b0c2c8cc1911776d47683e3500d.zip |
Merge pull request #72 from marekpiechut/picker-step
Add hourStep, minuteStep and secondStep props to control intervals in picker
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | src/Panel.jsx | 22 | ||||
-rw-r--r-- | src/TimePicker.jsx | 8 | ||||
-rw-r--r-- | tests/Select.spec.jsx | 34 |
4 files changed, 60 insertions, 7 deletions
@@ -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 | | focusOnOpen | Boolean | false | automatically focus the input when the picker opens | | 83 | | focusOnOpen | Boolean | false | automatically focus the input when the picker opens | |
81 | 84 | ||
82 | ## Test Case | 85 | ## Test Case |
diff --git a/src/Panel.jsx b/src/Panel.jsx index 957c021..a1b7c77 100644 --- a/src/Panel.jsx +++ b/src/Panel.jsx | |||
@@ -8,9 +8,9 @@ import classNames from 'classnames'; | |||
8 | function noop() { | 8 | function noop() { |
9 | } | 9 | } |
10 | 10 | ||
11 | function generateOptions(length, disabledOptions, hideDisabledOptions) { | 11 | function 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 | focusOnOpen: PropTypes.bool, | 46 | focusOnOpen: PropTypes.bool, |
44 | onKeyDown: PropTypes.func, | 47 | onKeyDown: PropTypes.func, |
@@ -92,7 +95,8 @@ class Panel extends Component { | |||
92 | const { | 95 | const { |
93 | prefixCls, className, placeholder, disabledHours, disabledMinutes, | 96 | prefixCls, className, placeholder, disabledHours, disabledMinutes, |
94 | disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond, | 97 | disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond, |
95 | format, defaultOpenValue, clearText, onEsc, addon, use12Hours, onClear, focusOnOpen, onKeyDown, | 98 | format, defaultOpenValue, clearText, onEsc, addon, use12Hours, onClear, |
99 | focusOnOpen, onKeyDown, hourStep, minuteStep, secondStep, | ||
96 | } = this.props; | 100 | } = this.props; |
97 | const { | 101 | const { |
98 | value, currentSelectPanel, | 102 | value, currentSelectPanel, |
@@ -101,9 +105,15 @@ class Panel extends Component { | |||
101 | const disabledMinuteOptions = disabledMinutes(value ? value.hour() : null); | 105 | const disabledMinuteOptions = disabledMinutes(value ? value.hour() : null); |
102 | const disabledSecondOptions = disabledSeconds(value ? value.hour() : null, | 106 | const disabledSecondOptions = disabledSeconds(value ? value.hour() : null, |
103 | value ? value.minute() : null); | 107 | value ? value.minute() : null); |
104 | const hourOptions = generateOptions(24, disabledHourOptions, hideDisabledOptions); | 108 | const hourOptions = generateOptions( |
105 | const minuteOptions = generateOptions(60, disabledMinuteOptions, hideDisabledOptions); | 109 | 24, disabledHourOptions, hideDisabledOptions, hourStep |
106 | const secondOptions = generateOptions(60, disabledSecondOptions, hideDisabledOptions); | 110 | ); |
111 | const minuteOptions = generateOptions( | ||
112 | 60, disabledMinuteOptions, hideDisabledOptions, minuteStep | ||
113 | ); | ||
114 | const secondOptions = generateOptions( | ||
115 | 60, disabledSecondOptions, hideDisabledOptions, secondStep | ||
116 | ); | ||
107 | 117 | ||
108 | return ( | 118 | return ( |
109 | <div className={classNames({ [`${prefixCls}-inner`]: true, [className]: !!className })}> | 119 | <div className={classNames({ [`${prefixCls}-inner`]: true, [className]: !!className })}> |
diff --git a/src/TimePicker.jsx b/src/TimePicker.jsx index 70327d7..c7ff80a 100644 --- a/src/TimePicker.jsx +++ b/src/TimePicker.jsx | |||
@@ -48,6 +48,9 @@ export default class Picker extends Component { | |||
48 | name: PropTypes.string, | 48 | name: PropTypes.string, |
49 | autoComplete: PropTypes.string, | 49 | autoComplete: PropTypes.string, |
50 | use12Hours: PropTypes.bool, | 50 | use12Hours: PropTypes.bool, |
51 | hourStep: PropTypes.number, | ||
52 | minuteStep: PropTypes.number, | ||
53 | secondStep: PropTypes.number, | ||
51 | focusOnOpen: PropTypes.bool, | 54 | focusOnOpen: PropTypes.bool, |
52 | onKeyDown: PropTypes.func, | 55 | onKeyDown: PropTypes.func, |
53 | }; | 56 | }; |
@@ -165,7 +168,7 @@ export default class Picker extends Component { | |||
165 | prefixCls, placeholder, disabledHours, | 168 | prefixCls, placeholder, disabledHours, |
166 | disabledMinutes, disabledSeconds, hideDisabledOptions, | 169 | disabledMinutes, disabledSeconds, hideDisabledOptions, |
167 | allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText, | 170 | allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText, |
168 | addon, use12Hours, focusOnOpen, onKeyDown, | 171 | addon, use12Hours, focusOnOpen, onKeyDown, hourStep, minuteStep, secondStep, |
169 | } = this.props; | 172 | } = this.props; |
170 | return ( | 173 | return ( |
171 | <Panel | 174 | <Panel |
@@ -188,6 +191,9 @@ export default class Picker extends Component { | |||
188 | disabledSeconds={disabledSeconds} | 191 | disabledSeconds={disabledSeconds} |
189 | hideDisabledOptions={hideDisabledOptions} | 192 | hideDisabledOptions={hideDisabledOptions} |
190 | use12Hours={use12Hours} | 193 | use12Hours={use12Hours} |
194 | hourStep={hourStep} | ||
195 | minuteStep={minuteStep} | ||
196 | secondStep={secondStep} | ||
191 | addon={addon} | 197 | addon={addon} |
192 | focusOnOpen={focusOnOpen} | 198 | focusOnOpen={focusOnOpen} |
193 | onKeyDown={onKeyDown} | 199 | onKeyDown={onKeyDown} |
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'; | |||
7 | import async from 'async'; | 7 | import async from 'async'; |
8 | import moment from 'moment'; | 8 | import moment from 'moment'; |
9 | 9 | ||
10 | const map = (arr, fn) => Array.prototype.map.call(arr, fn); | ||
11 | |||
10 | describe('Select', () => { | 12 | describe('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', () => { |