Skip to content

Commit

Permalink
Add support for disabling months and years according to isValidDate
Browse files Browse the repository at this point in the history
* disable months according to isValidDate function

* deactivate click on disabled month

* pass unit tests (month view)

* disable years according to isValidDate props function

* add example valid date with a view month

* add unit tests
  • Loading branch information
michelre authored and simeg committed Nov 15, 2016
1 parent 701646f commit c306f2f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
8 changes: 7 additions & 1 deletion example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ var React = require('react');
var ReactDOM = require('react-dom');

ReactDOM.render(
React.createElement(DateTime, { timeFormat: true }),
React.createElement(DateTime, {
viewMode: 'months',
dateFormat: 'MMMM',
isValidDate: function(current){
return current.isBefore(DateTime.moment().startOf('month'));
}
}),
document.getElementById('datetime')
);
16 changes: 14 additions & 2 deletions src/MonthsView.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,30 @@ var DateTimePickerMonths = React.createClass({
i = 0,
months = [],
renderer = this.props.renderMonth || this.renderMonth,
isValid = this.props.isValidDate || this.isValidDate,
classes, props
;

while (i < 12) {
classes = 'rdtMonth';
var currentMonth = this.props.viewDate.clone().set({ year: year, month: i, date: 1 });
var disabled = !isValid(currentMonth);

if ( disabled )
classes += ' rdtDisabled';

if ( date && i === month && year === date.year() )
classes += ' rdtActive';

props = {
key: i,
'data-value': i,
className: classes,
onClick: this.props.updateOn === 'months'? this.updateSelectedMonth : this.props.setDate('month')
className: classes
};

if ( !disabled )
props.onClick = this.props.updateOn === 'months'? this.updateSelectedMonth : this.props.setDate('month');

months.push( renderer( props, i, year, date && date.clone() ));

if ( months.length === 4 ){
Expand All @@ -61,6 +70,9 @@ var DateTimePickerMonths = React.createClass({
? capitalize( monthsShort.standalone[ month ] )
: monthsShort[ month ]
);
},
isValidDate: function(){
return 1;
}
});

Expand Down
17 changes: 15 additions & 2 deletions src/YearsView.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,33 @@ var DateTimePickerYears = React.createClass({
rows = [],
renderer = this.props.renderYear || this.renderYear,
selectedDate = this.props.selectedDate,
isValid = this.props.isValidDate || this.isValidDate,
classes, props
;

year--;
while (i < 11) {
classes = 'rdtYear';
var currentYear = this.props.viewDate.clone().set({ year: year, month: 1, date: 1 });
if ( i === -1 | i === 10 )
classes += ' rdtOld';

var disabled = !isValid(currentYear);
if ( disabled )
classes += ' rdtDisabled';

if ( selectedDate && selectedDate.year() === year )
classes += ' rdtActive';

props = {
key: year,
'data-value': year,
className: classes,
onClick: this.props.updateOn === 'years' ? this.updateSelectedYear : this.props.setDate('year')
className: classes
};

if ( !disabled )
props.onClick = this.props.updateOn === 'years' ? this.updateSelectedYear : this.props.setDate('year');

years.push( renderer( props, year, selectedDate && selectedDate.clone() ));

if ( years.length === 4 ){
Expand All @@ -61,6 +70,10 @@ var DateTimePickerYears = React.createClass({

renderYear: function( props, year ){
return DOM.td( props, year );
},

isValidDate: function(){
return 1;
}
});

Expand Down
18 changes: 18 additions & 0 deletions tests/datetime-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,4 +692,22 @@ describe( 'Datetime', function(){
dt.input().value = invalidStrDate;
ev.change( dt.input() );
});

it( 'disable months', function(){
createDatetime({ viewMode: 'months', isValidDate: function( current ){
return current.isBefore(moment('2016-06-01', 'YYYY-MM-DD'));
}});
assert.equal( dt.month(0).className, 'rdtMonth' );
assert.equal( dt.month(4).className, 'rdtMonth' );
assert.equal( dt.month(5).className, 'rdtMonth rdtDisabled' );
assert.equal( dt.month(11).className, 'rdtMonth rdtDisabled' );
});

it( 'disable years', function(){
createDatetime({ viewMode: 'years', isValidDate: function( current ){
return current.isBefore(moment('2016-01-01', 'YYYY-MM-DD'));
}});
assert.equal( dt.year(6).className, 'rdtYear' );
assert.equal( dt.year(7).className, 'rdtYear rdtDisabled' );
});
});

0 comments on commit c306f2f

Please sign in to comment.