Skip to content

Commit

Permalink
Added preventOpenOnFocus prop (Hacker0x01#1181)
Browse files Browse the repository at this point in the history
* Added preventOpenOnFocus prop

* Added tests for preventOpenOnFocus prop
  • Loading branch information
Jmenache authored and martijnrusschen committed Dec 18, 2017
1 parent f8c9868 commit 1306eb2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export default class DatePicker extends React.Component {
popperClassName: PropTypes.string, // <PopperComponent/> props
popperModifiers: PropTypes.object, // <PopperComponent/> props
popperPlacement: PropTypes.oneOf(popperPlacementPositions), // <PopperComponent/> props
preventOpenOnFocus: PropTypes.bool,
readOnly: PropTypes.bool,
required: PropTypes.bool,
scrollableYearDropdown: PropTypes.bool,
Expand Down Expand Up @@ -139,6 +140,7 @@ export default class DatePicker extends React.Component {
onSelect () {},
onClickOutside () {},
onMonthChange () {},
preventOpenOnFocus: false,
onYearChange () {},
monthsShown: 1,
withPortal: false,
Expand Down Expand Up @@ -215,7 +217,9 @@ export default class DatePicker extends React.Component {
handleFocus = (event) => {
if (!this.state.preventFocus) {
this.props.onFocus(event)
this.setOpen(true)
if (!this.props.preventOpenOnFocus) {
this.setOpen(true)
}
}
}

Expand Down Expand Up @@ -344,7 +348,7 @@ export default class DatePicker extends React.Component {
onInputKeyDown = (event) => {
this.props.onKeyDown(event)
const eventKey = event.key
if (!this.state.open && !this.props.inline) {
if (!this.state.open && !this.props.inline && !this.props.preventOpenOnFocus) {
if (eventKey !== 'Enter' && eventKey !== 'Escape' && eventKey !== 'Tab') {
this.onInputClick()
}
Expand Down
16 changes: 16 additions & 0 deletions test/datepicker_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,4 +784,20 @@ describe('DatePicker', () => {
datePicker.setProps({ selected: future })
expect(utils.formatDate(datePicker.state('preSelection'), 'YYYY-MM-DD')).to.equal(utils.formatDate(future, 'YYYY-MM-DD'))
})
it('should not set open state when focusing on the date input and the preventOpenOnFocus prop is set', () => {
const datePicker = TestUtils.renderIntoDocument(
<DatePicker preventOpenOnFocus />
)
const dateInput = datePicker.input
TestUtils.Simulate.focus(ReactDOM.findDOMNode(dateInput))
expect(datePicker.state.open).to.be.false
})
it('should not set open state onInputKeyDown when preventOpenOnFocus prop is set', () => {
const datePicker = TestUtils.renderIntoDocument(
<DatePicker preventOpenOnFocus />
)
const dateInput = datePicker.input
TestUtils.Simulate.keyDown(ReactDOM.findDOMNode(dateInput), getKey('ArrowLeft'))
expect(datePicker.state.open).to.be.false
})
})

0 comments on commit 1306eb2

Please sign in to comment.