Skip to content

Commit 4fd85eb

Browse files
m1n0smartijnrusschen
authored andcommitted
Change selection of inline picker. fix Hacker0x01#927. (Hacker0x01#960)
* Change selection of inline picker. fix Hacker0x01#927. * Reduce amount of recalculations * Add test for inline mode changing preSelection state
1 parent fa1abff commit 4fd85eb

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/datepicker.jsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ export default class DatePicker extends React.Component {
110110
this.state = this.calcInitialState()
111111
}
112112

113+
componentWillReceiveProps (nextProps) {
114+
if (this.props.inline && !this.props.selected.isSame(nextProps.selected, 'month')) {
115+
this.setPreSelection(nextProps.selected)
116+
}
117+
}
118+
113119
componentWillUnmount () {
114120
this.clearPreventFocusTimeout()
115121
}
@@ -182,7 +188,9 @@ export default class DatePicker extends React.Component {
182188
}
183189

184190
handleCalendarClickOutside = (event) => {
185-
this.setOpen(false)
191+
if (!this.props.inline) {
192+
this.setOpen(false)
193+
}
186194
this.props.onClickOutside(event)
187195
if (this.props.withPortal) { event.preventDefault() }
188196
}

test/datepicker_test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,4 +601,14 @@ describe('DatePicker', () => {
601601
)
602602
expect(datePicker.state('preSelection').format('YYYY-MM-DD')).to.equal(moment().format('YYYY-MM-DD'))
603603
})
604+
it('should switch month in inline mode immediately', () => {
605+
const selected = moment()
606+
const future = moment().add(100, 'day')
607+
const datePicker = mount(
608+
<DatePicker inline selected={selected}/>
609+
)
610+
expect(datePicker.state('preSelection').format('YYYY-MM-DD')).to.equal(selected.format('YYYY-MM-DD'))
611+
datePicker.setProps({ selected: future })
612+
expect(datePicker.state('preSelection').format('YYYY-MM-DD')).to.equal(future.format('YYYY-MM-DD'))
613+
})
604614
})

0 commit comments

Comments
 (0)