Skip to content

Commit 0c3e36b

Browse files
authored
Add support for openCalendarOnFocus prop (#362)
1 parent 3d70172 commit 0c3e36b

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ Displays an input field complete with custom inputs, native input, and a calenda
108108
|onCalendarClose|Function called when the calendar closes.|n/a|`() => alert('Calendar closed')`|
109109
|onCalendarOpen|Function called when the calendar opens.|n/a|`() => alert('Calendar opened')`|
110110
|onChange|Function called when the user clicks an item on the most detailed view available.|n/a|`(value) => alert('New date is: ', value)`|
111+
|openCalendarOnFocus|Whether to open the calendar on input focus.|`true`|`false`|
111112
|required|Whether date input should be required.|`false`|`true`|
112113
|returnValue|Which dates shall be passed by the calendar to the onChange function and onClick{Period} functions. Can be `"start"`, `"end"` or `"range"`. The latter will cause an array with start and end values to be passed.|`"start"`|`"range"`|
113114
|showLeadingZeros|Whether leading zeros should be rendered in date inputs.|`false`|`true`|

src/DatePicker.jsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export default class DatePicker extends PureComponent {
7070
}
7171

7272
onFocus = (event) => {
73-
const { disabled, onFocus } = this.props;
73+
const { disabled, onFocus, openCalendarOnFocus } = this.props;
7474

7575
if (onFocus) {
7676
onFocus(event);
@@ -81,7 +81,9 @@ export default class DatePicker extends PureComponent {
8181
return;
8282
}
8383

84-
this.openCalendar();
84+
if (openCalendarOnFocus) {
85+
this.openCalendar();
86+
}
8587
}
8688

8789
openCalendar = () => {
@@ -304,6 +306,7 @@ DatePicker.defaultProps = {
304306
clearIcon: ClearIcon,
305307
closeCalendar: true,
306308
isOpen: null,
309+
openCalendarOnFocus: true,
307310
returnValue: 'start',
308311
};
309312

@@ -345,6 +348,7 @@ DatePicker.propTypes = {
345348
onCalendarOpen: PropTypes.func,
346349
onChange: PropTypes.func,
347350
onFocus: PropTypes.func,
351+
openCalendarOnFocus: PropTypes.bool,
348352
required: PropTypes.bool,
349353
returnValue: PropTypes.oneOf(['start', 'end', 'range']),
350354
showLeadingZeros: PropTypes.bool,

src/DatePicker.spec.jsx

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -246,22 +246,60 @@ describe('DatePicker', () => {
246246
expect(calendar2).toHaveLength(1);
247247
});
248248

249-
it('opens Calendar component when focusing on an input inside', () => {
250-
const component = mount(
251-
<DatePicker />,
252-
);
249+
describe('handles opening Calendar component when focusing on an input inside properly', () => {
250+
it('opens Calendar component when focusing on an input inside by default', () => {
251+
const component = mount(
252+
<DatePicker />,
253+
);
253254

254-
const calendar = component.find('Calendar');
255-
const input = component.find('input[name="day"]');
255+
const calendar = component.find('Calendar');
256+
const input = component.find('input[name="day"]');
256257

257-
expect(calendar).toHaveLength(0);
258+
expect(calendar).toHaveLength(0);
258259

259-
input.simulate('focus');
260-
component.update();
260+
input.simulate('focus');
261+
component.update();
261262

262-
const calendar2 = component.find('Calendar');
263+
const calendar2 = component.find('Calendar');
263264

264-
expect(calendar2).toHaveLength(1);
265+
expect(calendar2).toHaveLength(1);
266+
});
267+
268+
it('opens Calendar component when focusing on an input inside given openCalendarOnFocus = true', () => {
269+
const component = mount(
270+
<DatePicker openCalendarOnFocus />,
271+
);
272+
273+
const calendar = component.find('Calendar');
274+
const input = component.find('input[name="day"]');
275+
276+
expect(calendar).toHaveLength(0);
277+
278+
input.simulate('focus');
279+
component.update();
280+
281+
const calendar2 = component.find('Calendar');
282+
283+
expect(calendar2).toHaveLength(1);
284+
});
285+
286+
it('does not open Calendar component when focusing on an input inside given openCalendarOnFocus = false', () => {
287+
const component = mount(
288+
<DatePicker openCalendarOnFocus={false} />,
289+
);
290+
291+
const calendar = component.find('Calendar');
292+
const input = component.find('input[name="day"]');
293+
294+
expect(calendar).toHaveLength(0);
295+
296+
input.simulate('focus');
297+
component.update();
298+
299+
const calendar2 = component.find('Calendar');
300+
301+
expect(calendar2).toHaveLength(0);
302+
});
265303
});
266304

267305
it('closes Calendar component when clicked outside', () => {

0 commit comments

Comments
 (0)