Skip to content

Commit c7f6658

Browse files
committed
Merge branch 'master' of github.com:gpbl/react-day-picker
2 parents 8088633 + 0e88e0f commit c7f6658

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

src/DayPickerInput.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
33

44
import DayPicker from './DayPicker';
55
import { getModifiersForDay } from './ModifiersUtils';
6-
import { ESC } from './keys';
6+
import { ESC, TAB } from './keys';
77

88
// When clicking on a day cell, overlay will be hidden after this timeout
99
export const HIDE_TIMEOUT = 100;
@@ -81,6 +81,7 @@ export default class DayPickerInput extends React.Component {
8181
onFocus: PropTypes.func,
8282
onBlur: PropTypes.func,
8383
onKeyUp: PropTypes.func,
84+
onKeyDown: PropTypes.func,
8485
};
8586

8687
static defaultProps = {
@@ -120,6 +121,7 @@ export default class DayPickerInput extends React.Component {
120121
this.handleInputBlur = this.handleInputBlur.bind(this);
121122
this.handleInputChange = this.handleInputChange.bind(this);
122123
this.handleInputKeyUp = this.handleInputKeyUp.bind(this);
124+
this.handleInputKeyDown = this.handleInputKeyDown.bind(this);
123125
this.handleDayClick = this.handleDayClick.bind(this);
124126
}
125127

@@ -334,6 +336,16 @@ export default class DayPickerInput extends React.Component {
334336
this.updateState(day, value);
335337
}
336338

339+
handleInputKeyDown(e) {
340+
if (e.keyCode === TAB) {
341+
this.clickedInside = true;
342+
}
343+
if (this.props.inputProps.onKeyDown) {
344+
e.persist();
345+
this.props.inputProps.onKeyDown(e);
346+
}
347+
}
348+
337349
handleInputKeyUp(e) {
338350
// Hide the overlay if the ESC key is pressed
339351
this.setState({ showOverlay: e.keyCode !== ESC });
@@ -453,6 +465,7 @@ export default class DayPickerInput extends React.Component {
453465
onBlur={this.handleInputBlur}
454466
onKeyUp={this.handleInputKeyUp}
455467
onClick={this.handleInputClick}
468+
onKeyDown={this.handleInputKeyDown}
456469
/>
457470
{this.state.showOverlay && this.renderOverlay()}
458471
</div>

src/addons/MomentLocaleUtils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function formatDate(date, format = 'L', locale = 'en') {
5353
}
5454

5555
export function parseDate(str, format = 'L', locale = 'en') {
56-
const m = moment(str, format, locale);
56+
const m = moment(str, format, locale, true);
5757
if (m.isValid()) {
5858
return m.toDate();
5959
}

src/keys.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export const DOWN = 40;
55
export const ENTER = 13;
66
export const SPACE = 32;
77
export const ESC = 27;
8+
export const TAB = 9;

test/addons/MomentLocaleUtils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,9 @@ describe('MomentLocaleUtils', () => {
101101
const parsed = MomentLocaleUtils.parseDate('20 foo 2018', 'LL', 'it');
102102
expect(parsed).toBeUndefined();
103103
});
104+
it('returns undefined if date does not match the format', () => {
105+
const parsed = MomentLocaleUtils.parseDate('20/11/201', 'DD/MM/YYYY');
106+
expect(parsed).toBeUndefined();
107+
});
104108
});
105109
});

test/daypickerinput/events.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,21 @@ describe('DayPickerInput', () => {
154154
expect(onKeyUp).toHaveBeenCalledTimes(1);
155155
});
156156
});
157+
describe('keydown', () => {
158+
it('should not hide the overlay on TAB', () => {
159+
const wrapper = mount(<DayPickerInput />);
160+
wrapper.instance().showDayPicker();
161+
wrapper.update();
162+
wrapper.find('input').simulate('keydown', { keyCode: keys.TAB });
163+
expect(wrapper.state('showOverlay')).toBe(true);
164+
});
165+
it('should call `onKeyDown` event handler', () => {
166+
const onKeyDown = jest.fn();
167+
const wrapper = mount(<DayPickerInput inputProps={{ onKeyDown }} />);
168+
wrapper.find('input').simulate('keydown');
169+
expect(onKeyDown).toHaveBeenCalledTimes(1);
170+
});
171+
});
157172
describe('dayclick', () => {
158173
it('should call `onDayClick` event handler', () => {
159174
const onDayClick = jest.fn();

0 commit comments

Comments
 (0)