Skip to content

Commit

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

* Additional test

* Lint and test fixes
  • Loading branch information
kujon authored and martijnrusschen committed Dec 18, 2017
1 parent b8ab34c commit f8c9868
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/datepicker.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ General datepicker component.
|`onFocus`|`func`|`function() {}`||
|`onKeyDown`|`func`|`function() {}`||
|`onMonthChange`|`func`|`function() {}`||
|`onYearChange`|`func`|`function() {}`||
|`onSelect`|`func`|`function() {}`||
|`onWeekSelect`|`func`|||
|`openToDate`|`object`|||
Expand Down Expand Up @@ -77,4 +78,4 @@ General datepicker component.
|`value`|`string`|||
|`weekLabel`|`string`|||
|`withPortal`|`bool`|`false`||
|`yearDropdownItemNumber`|`number`|||
|`yearDropdownItemNumber`|`number`|||
9 changes: 8 additions & 1 deletion src/calendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default class Calendar extends React.Component {
monthsShown: PropTypes.number,
onClickOutside: PropTypes.func.isRequired,
onMonthChange: PropTypes.func,
onYearChange: PropTypes.func,
forceShowMonthNavigation: PropTypes.bool,
onDropdownFocus: PropTypes.func,
onSelect: PropTypes.func.isRequired,
Expand Down Expand Up @@ -187,6 +188,12 @@ export default class Calendar extends React.Component {

handleMonthMouseLeave = () => this.setState({ selectingDate: null })

handleYearChange = (date) => {
if (this.props.onYearChange) {
this.props.onYearChange(date)
}
}

handleMonthChange = (date) => {
if (this.props.onMonthChange) {
this.props.onMonthChange(date)
Expand All @@ -204,7 +211,7 @@ export default class Calendar extends React.Component {
changeYear = (year) => {
this.setState({
date: setYear(cloneDate(this.state.date), year)
})
}, () => this.handleYearChange(this.state.date))
}

changeMonth = (month) => {
Expand Down
3 changes: 3 additions & 0 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export default class DatePicker extends React.Component {
onFocus: PropTypes.func,
onKeyDown: PropTypes.func,
onMonthChange: PropTypes.func,
onYearChange: PropTypes.func,
openToDate: PropTypes.object,
peekNextMonth: PropTypes.bool,
placeholderText: PropTypes.string,
Expand Down Expand Up @@ -138,6 +139,7 @@ export default class DatePicker extends React.Component {
onSelect () {},
onClickOutside () {},
onMonthChange () {},
onYearChange () {},
monthsShown: 1,
withPortal: false,
shouldCloseOnSelect: true,
Expand Down Expand Up @@ -456,6 +458,7 @@ export default class DatePicker extends React.Component {
monthsShown={this.props.monthsShown}
onDropdownFocus={this.handleDropdownFocus}
onMonthChange={this.props.onMonthChange}
onYearChange={this.props.onYearChange}
dayClassName={this.props.dayClassName}
showTimeSelect={this.props.showTimeSelect}
onTimeChange={this.handleTimeChange}
Expand Down
26 changes: 26 additions & 0 deletions test/calendar_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,32 @@ describe('Calendar', function () {
})
})

describe('onYearChange', () => {
let onYearChangeSpy = sinon.spy()
let calendar

beforeEach(() => {
onYearChangeSpy = sinon.spy()
calendar = mount(
<Calendar
dateFormat={dateFormat}
onSelect={() => {}}
onClickOutside={() => {}}
hideCalendar={() => {}}
dropdownMode="select"
showYearDropdown
onYearChange={onYearChangeSpy}/>
)
})

it('calls onYearChange prop when year changed from year dropdown', () => {
const select = calendar.find(YearDropdown).find('select')
select.simulate('change')

assert(onYearChangeSpy.called === true, 'onYearChange should be called')
})
})

describe('onDropdownFocus', () => {
let onDropdownFocusSpy = sinon.spy()
let calendar
Expand Down
21 changes: 21 additions & 0 deletions test/datepicker_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,27 @@ describe('DatePicker', () => {
})
})

it('should fire onYearChange when the year is selected', (done) => {
const onYearChangeSpy = sinon.spy()
const datePicker = mount(
<DatePicker
showYearDropdown
dropdownMode="select"
onYearChange={onYearChangeSpy}/>
)
const dateInputWrapper = datePicker.find('input')

dateInputWrapper.simulate('click')
const calendarWrapper = datePicker.find('Calendar')
const yearSelect = calendarWrapper.find('.react-datepicker__year-select')
yearSelect.simulate('change')

defer(() => {
assert(onYearChangeSpy.called === true, 'onYearChange should be called')
done()
})
})

it('should keep the calendar shown when clicking the calendar', () => {
var datePicker = TestUtils.renderIntoDocument(
<DatePicker />
Expand Down

0 comments on commit f8c9868

Please sign in to comment.