Skip to content

Commit 4b1f814

Browse files
committed
Default to current year if year input is missing
Closes #145
1 parent 2eb9957 commit 4b1f814

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/DateTimeInput.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,8 @@ export default class DateTimeInput extends PureComponent {
535535
} else if (
536536
formElements.every((formElement) => formElement.value && formElement.validity.valid)
537537
) {
538-
const year = parseInt(values.year, 10);
539-
const monthIndex = parseInt(values.month, 10) - 1 || 0;
538+
const year = parseInt(values.year, 10) || new Date().getFullYear();
539+
const monthIndex = parseInt(values.month || 1, 10) - 1;
540540
const day = parseInt(values.day || 1, 10);
541541
const hour = parseInt(values.hour24 || convert12to24(values.hour12, values.amPm) || 0, 10);
542542
const minute = parseInt(values.minute || 0, 10);
@@ -545,6 +545,7 @@ export default class DateTimeInput extends PureComponent {
545545
const proposedValue = new Date();
546546
proposedValue.setFullYear(year, monthIndex, day);
547547
proposedValue.setHours(hour, minute, second, 0);
548+
548549
const processedValue = proposedValue;
549550
onChange(processedValue, false);
550551
}

src/DateTimeInput.spec.jsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,26 @@ describe('DateTimeInput', () => {
745745
expect(onChange).toHaveBeenCalledWith(nextDate, false);
746746
});
747747

748+
it('triggers onChange correctly when changed custom input with no year', () => {
749+
const onChange = jest.fn();
750+
const date = new Date(2017, 8, 30, 22, 17, 0);
751+
752+
const component = mount(
753+
<DateTimeInput {...defaultProps} format="dd.MM HH:mm" onChange={onChange} value={date} />,
754+
);
755+
756+
const customInputs = component.find('input[type="number"]');
757+
const hourInput = customInputs.at(2);
758+
759+
hourInput.getDOMNode().value = '20';
760+
hourInput.simulate('change');
761+
762+
const currentYear = new Date().getFullYear();
763+
764+
expect(onChange).toHaveBeenCalled();
765+
expect(onChange).toHaveBeenCalledWith(new Date(currentYear, 8, 30, 20, 17, 0), false);
766+
});
767+
748768
it('triggers onChange correctly when cleared custom inputs', () => {
749769
const onChange = jest.fn();
750770
const date = new Date(2017, 8, 30, 22, 17, 0);

0 commit comments

Comments
 (0)