Skip to content

Commit e8643de

Browse files
authored
Jump between fields if e.g. "01" was typed in (#219)
* Jump between fields if e.g. "01" was typed in * Add unit tests
1 parent 610c3eb commit e8643de

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/DateInput.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,15 +352,15 @@ export default class DateInput extends PureComponent {
352352
}
353353

354354
const { value } = input;
355-
const max = parseInt(input.getAttribute('max'), 10);
355+
const max = input.getAttribute('max');
356356

357357
/**
358358
* Given 1, the smallest possible number the user could type by adding another digit is 10.
359359
* 10 would be a valid value given max = 12, so we won't jump to the next input.
360360
* However, given 2, smallers possible number would be 20, and thus keeping the focus in
361361
* this field doesn't make sense.
362362
*/
363-
if (value * 10 > max) {
363+
if ((value * 10 > max) || (value.length >= max.length)) {
364364
const property = 'nextElementSibling';
365365
const nextInput = findInput(input, property);
366366
focus(nextInput);

src/DateInput.spec.jsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ describe('DateInput', () => {
567567
expect(document.activeElement).toBe(dayInput.getDOMNode());
568568
});
569569

570-
it('jumps to the next field when a value which can\'t be extended to another valid value is entered ', () => {
570+
it('jumps to the next field when a value which can\'t be extended to another valid value is entered', () => {
571571
const component = mount(
572572
<DateInput {...defaultProps} />
573573
);
@@ -584,7 +584,24 @@ describe('DateInput', () => {
584584
expect(document.activeElement).toBe(monthInput.getDOMNode());
585585
});
586586

587-
it('does not jump the next field when a value which can be extended to another valid value is entered ', () => {
587+
it('jumps to the next field when a value as long as the length of maximum value is entered', () => {
588+
const component = mount(
589+
<DateInput {...defaultProps} />
590+
);
591+
592+
const customInputs = component.find('input[type="number"]');
593+
const dayInput = customInputs.at(0);
594+
const monthInput = customInputs.at(1);
595+
596+
dayInput.getDOMNode().focus();
597+
dayInput.getDOMNode().value = '03';
598+
599+
dayInput.simulate('keyup', { target: dayInput.getDOMNode(), key: '3' });
600+
601+
expect(document.activeElement).toBe(monthInput.getDOMNode());
602+
});
603+
604+
it('does not jump the next field when a value which can be extended to another valid value is entered', () => {
588605
const component = mount(
589606
<DateInput {...defaultProps} />
590607
);

0 commit comments

Comments
 (0)