Skip to content

Commit

Permalink
Fix calendar opening when clicking inside the wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ideriabin committed Mar 16, 2023
1 parent 5de4660 commit 3d443db
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/DateInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,13 @@ export default class DateInput extends PureComponent {
}

onClick = (event) => {
if (event.target === event.currentTarget) {
if (
event.target !== this.dayInput.current &&
event.target !== this.monthInput.current &&
event.target !== this.yearInput.current
) {
// Wrapper was directly clicked
const firstInput = event.target.children[1];
const firstInput = event.currentTarget.querySelector('input[data-input]');
focus(firstInput);
}
};
Expand Down
34 changes: 34 additions & 0 deletions src/DatePicker.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -499,4 +499,38 @@ describe('DatePicker', () => {

expect(onChange).toHaveBeenCalledWith(null);
});

it('focuses on first custom input when clicking on divider', () => {
const date = new Date(2017, 8, 30);
let customInput;
let divider;

const { container, rerender } = render(<DatePicker />);

customInput = container.querySelector('input[data-input]');
divider = container.querySelector('.react-date-picker__inputGroup__divider');

fireEvent.click(divider);
expect(customInput).toHaveFocus();

rerender(<DatePicker value={date} />);

customInput = container.querySelector('input[data-input]');
divider = container.querySelector('.react-date-picker__inputGroup__divider');

fireEvent.click(divider);
expect(customInput).toHaveFocus();
});

it('focuses on first custom input when clicking on leading zero', () => {
const date = new Date(2017, 8, 30);

const { container } = render(<DatePicker showLeadingZeros value={date} />);

const customInput = container.querySelector('input[data-input]');
const leadingZero = container.querySelector('.react-date-picker__inputGroup__leadingZero');

fireEvent.click(leadingZero);
expect(customInput).toHaveFocus();
});
});

0 comments on commit 3d443db

Please sign in to comment.