Skip to content

Added ability to pass step amount for minutes value #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ module.exports = {
collectCoverageFrom: ['src/**/*.{js,jsx}'],
testMatch: ['<rootDir>/test/*.test.js'],
bail: true,
verbose: false
verbose: false,
testURL: 'http://localhost/'
};
32 changes: 26 additions & 6 deletions src/TimeInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,36 @@ var TimeInput = CreateReactClass({
if (this.props.value.charAt(index) === ' ') index++;
if (this.mounted) this.setState({ caretIndex: index });
},
handleArrows(event) {
event.preventDefault();
var start = caret.start(this.input);
var value = this.props.value;
var amount = event.which === 38 ? 1 : -1;
getStep() {
var step = this.props.step;
if (typeof step === 'number') {
return step;
} else {
return 1;
}
},
getAmount(event, groupId) {
var amount = 1;
if (groupId === 1) {
amount = this.getStep();
}
if (event.shiftKey) {
amount *= 2;
if (event.metaKey) amount *= 2;
}
value = adder(value, getGroupId(start), amount);
if (event.which === 38) {
return amount;
} else {
return -amount;
}
},
handleArrows(event) {
event.preventDefault();
var start = caret.start(this.input);
var value = this.props.value;
var groupId = getGroupId(start);
var amount = this.getAmount(event, groupId);
value = adder(value, groupId, amount);
this.onChange(value, start);
},
silhouette() {
Expand Down
28 changes: 26 additions & 2 deletions test/arrow-keys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ describe('up', function() {
'01:00:00:000 AM'
);
});
it('should increment minutes with given step amount', function() {
expect(arrow('01:00 AM', 3, true, false, false, 15).input.value).toEqual('01:15 AM');
expect(arrow('01:00:00:000 PM', 3, true, false, false, 15).input.value).toEqual(
'01:15:00:000 PM'
);
});
it('should increment hours with given step for minutes amount', function() {
expect(arrow('01:45 AM', 3, true, false, false, 15).input.value).toEqual('02:00 AM');
expect(arrow('01:45:00:000 PM', 3, true, false, false, 15).input.value).toEqual(
'02:00:00:000 PM'
);
});
});

describe('down', function() {
Expand Down Expand Up @@ -129,11 +141,23 @@ describe('down', function() {
'01:00:00:000 AM'
);
});
it('should decrement minutes with given step amount', function() {
expect(arrow('01:30 AM', 3, false, false, false, 15).input.value).toEqual('01:15 AM');
expect(arrow('01:30:00:000 PM', 3, false, false, false, 15).input.value).toEqual(
'01:15:00:000 PM'
);
});
it('should decrement hours with given step for minutes amount', function() {
expect(arrow('01:00 AM', 3, false, false, false, 15).input.value).toEqual('12:45 AM');
expect(arrow('01:00:00:000 PM', 3, false, false, false, 15).input.value).toEqual(
'12:45:00:000 PM'
);
});
});

function arrow(value, caretIndex, up, shiftKey, metaKey) {
function arrow(value, caretIndex, up, shiftKey, metaKey, step) {
document.body.innerHTML = '<div></div>';
var timeInput = render(value);
var timeInput = render(value, null, false, step);
caret.set(timeInput.input, caretIndex);
ReactTestUtils.Simulate.keyDown(timeInput.input, {
keyCode: up ? 38 : 40,
Expand Down
7 changes: 4 additions & 3 deletions test/lib/renderTimeInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import React from 'react';
import ReactDOM from 'react-dom';
import TimeInput from '../../src/TimeInput';

export default (value, el, omitOnChange) => {
var timeInput = render(value);
export default (value, el, omitOnChange, step) => {
var timeInput = render(value, step);
timeInput.input.focus();
return timeInput;
function render(value) {
function render(value, step) {
return ReactDOM.render(
<TimeInput
step={step}
value={value}
onChange={!omitOnChange ? render : undefined}
defaultValue="00:00:00:000"
Expand Down