Skip to content

Commit 732dde9

Browse files
authored
Add support for closeCalendar flag (#251)
1 parent 7969ac6 commit 732dde9

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Displays an input field complete with custom inputs, native input, and a calenda
9393
|className|Class name(s) that will be added along with `"react-date-picker"` to the main React-Date-Picker `<div>` element.|n/a|<ul><li>String: `"class1 class2"`</li><li>Array of strings: `["class1", "class2 class3"]`</li></ul>|
9494
|clearAriaLabel|`aria-label` for the clear button.|n/a|`"Clear value"`|
9595
|clearIcon|Content of the clear button. Setting the value explicitly to `null` will hide the icon.|(default icon)|<ul><li>String: `"Clear"`</li><li>React element: `<ClearIcon />`</li></ul>|
96+
|closeCalendar|Whether to close the calendar on value selection.|`true`|`false`|
9697
|dayAriaLabel|`aria-label` for the day input.|n/a|`"Day"`|
9798
|dayPlaceholder|`placeholder` for the day input.|`"--"`|`"dd"`|
9899
|disabled|Whether the date picker should be disabled.|`false`|`true`|

src/DatePicker.jsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ export default class DatePicker extends PureComponent {
5656
}
5757
}
5858

59-
onChange = (value, closeCalendar = true) => {
60-
this.setState({
61-
isOpen: !closeCalendar,
62-
});
63-
59+
// eslint-disable-next-line react/destructuring-assignment
60+
onChange = (value, closeCalendar = this.props.closeCalendar) => {
6461
const { onChange } = this.props;
62+
63+
if (closeCalendar) {
64+
this.closeCalendar();
65+
}
66+
6567
if (onChange) {
6668
onChange(value);
6769
}
@@ -300,6 +302,7 @@ const ClearIcon = (
300302
DatePicker.defaultProps = {
301303
calendarIcon: CalendarIcon,
302304
clearIcon: ClearIcon,
305+
closeCalendar: true,
303306
isOpen: null,
304307
returnValue: 'start',
305308
};
@@ -323,6 +326,7 @@ DatePicker.propTypes = {
323326
]),
324327
clearAriaLabel: PropTypes.string,
325328
clearIcon: PropTypes.node,
329+
closeCalendar: PropTypes.bool,
326330
dayAriaLabel: PropTypes.string,
327331
dayPlaceholder: PropTypes.string,
328332
disableCalendar: PropTypes.bool,

src/DatePicker.spec.jsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ describe('DatePicker', () => {
333333
expect(component.state('isOpen')).toBe(true);
334334
});
335335

336-
it('closes Calendar when calling internal onChange', () => {
336+
it('closes Calendar when calling internal onChange by default', () => {
337337
const component = mount(
338338
<DatePicker isOpen />
339339
);
@@ -345,6 +345,21 @@ describe('DatePicker', () => {
345345
expect(component.state('isOpen')).toBe(false);
346346
});
347347

348+
it('does not close Calendar when calling internal onChange with prop closeCalendar = false', () => {
349+
const component = mount(
350+
<DatePicker
351+
closeCalendar={false}
352+
isOpen
353+
/>
354+
);
355+
356+
const { onChange } = component.instance();
357+
358+
onChange(new Date());
359+
360+
expect(component.state('isOpen')).toBe(true);
361+
});
362+
348363
it('does not close Calendar when calling internal onChange with closeCalendar = false', () => {
349364
const component = mount(
350365
<DatePicker isOpen />

0 commit comments

Comments
 (0)