Skip to content

Commit

Permalink
auto focus calendar input when opening
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyann committed Nov 14, 2018
1 parent c92cf57 commit 01e24fd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Calendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ const Calendar = createReactClass({
);
},

saveDateInput(dateInput) {
this.saveFocusElement(dateInput ? dateInput.dateInputInstance : undefined);
},

render() {
const { props, state } = this;
const {
Expand Down Expand Up @@ -240,6 +244,7 @@ const Calendar = createReactClass({
selectedValue={selectedValue}
onChange={this.onDateInputChange}
clearIcon={clearIcon}
ref={this.saveDateInput}
/>
) : null;
const children = [
Expand Down
8 changes: 7 additions & 1 deletion src/mixin/CommonMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,17 @@ export default {
},

focus() {
if (this.rootInstance) {
if (this.focusElement) {
this.focusElement.focus();
} else if (this.rootInstance) {
this.rootInstance.focus();
}
},

saveFocusElement(focusElement) {
this.focusElement = focusElement;
},

saveRoot(root) {
this.rootInstance = root;
},
Expand Down
9 changes: 9 additions & 0 deletions tests/Picker.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,13 @@ describe('DatePicker', () => {
picker.find('.rc-calendar-clear-btn').simulate('click');
expect(picker.state().open).toBe(false);
});

it('auto focuses the calendar input when opening', () => {
jest.useFakeTimers();
const picker = renderPicker({ value: moment() });
picker.find('.rc-calendar-picker-input').simulate('click');
jest.runAllTimers();
expect(document.activeElement).toBeDefined();
expect(document.activeElement.classList).toContain('rc-calendar-input');
});
});

0 comments on commit 01e24fd

Please sign in to comment.