Skip to content

Commit d40f6d8

Browse files
DaanDDsimeg
authored andcommitted
Add onSubtractTime and onAddTime hooks (arqex#508)
* Add onSubtractTime and onAddTime hooks * Add tests for onSubtractTime and onAddTime hooks * Fix mixup in describe blocks * Give onSubtractTime and onAddTime hooks more logical names onNavigateBack and onNavigateForward
1 parent 0fbbc40 commit d40f6d8

File tree

5 files changed

+102
-15
lines changed

5 files changed

+102
-15
lines changed

DateTime.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ declare namespace ReactDatetimeClass {
9898
string ('years', 'months', 'days', 'time') as only parameter.
9999
*/
100100
onViewModeChange?: (viewMode: string) => void;
101+
/*
102+
Callback trigger when the user navigates to the previous month, year or decade.
103+
The callback receives the amount and type ('month', 'year') as parameters.
104+
*/
105+
onNavigateBack?: (amount: number, type: string) => void;
106+
/*
107+
Callback trigger when the user navigates to the next month, year or decade.
108+
The callback receives the amount and type ('month', 'year') as parameters.
109+
*/
110+
onNavigateForward?: (amount: number, type: string) => void;
101111
/*
102112
The default view to display when the picker is shown. ('years', 'months', 'days', 'time')
103113
*/

DateTime.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ var Datetime = createClass({
2626
onBlur: TYPES.func,
2727
onChange: TYPES.func,
2828
onViewModeChange: TYPES.func,
29+
onNavigateBack: TYPES.func,
30+
onNavigateForward: TYPES.func,
2931
locale: TYPES.string,
3032
utc: TYPES.bool,
3133
input: TYPES.bool,
@@ -254,26 +256,29 @@ var Datetime = createClass({
254256
};
255257
},
256258

257-
addTime: function( amount, type, toSelected ) {
258-
return this.updateTime( 'add', amount, type, toSelected );
259-
},
260-
261259
subtractTime: function( amount, type, toSelected ) {
262-
return this.updateTime( 'subtract', amount, type, toSelected );
260+
var me = this;
261+
return function() {
262+
me.props.onNavigateBack( amount, type );
263+
me.updateTime( 'subtract', amount, type, toSelected );
264+
};
263265
},
264266

265-
updateTime: function( op, amount, type, toSelected ) {
267+
addTime: function( amount, type, toSelected ) {
266268
var me = this;
267-
268269
return function() {
269-
var update = {},
270-
date = toSelected ? 'selectedDate' : 'viewDate'
271-
;
270+
me.props.onNavigateForward( amount, type );
271+
me.updateTime( 'add', amount, type, toSelected );
272+
};
273+
},
272274

273-
update[ date ] = me.state[ date ].clone()[ op ]( amount, type );
275+
updateTime: function( op, amount, type, toSelected ) {
276+
var update = {},
277+
date = toSelected ? 'selectedDate' : 'viewDate';
274278

275-
me.setState( update );
276-
};
279+
update[ date ] = this.state[ date ].clone()[ op ]( amount, type );
280+
281+
this.setState( update );
277282
},
278283

279284
allowedSetTime: ['hours', 'minutes', 'seconds', 'milliseconds'],
@@ -459,6 +464,8 @@ Datetime.defaultProps = {
459464
onBlur: function() {},
460465
onChange: function() {},
461466
onViewModeChange: function() {},
467+
onNavigateBack: function() {},
468+
onNavigateForward: function() {},
462469
timeFormat: true,
463470
timeConstraints: {},
464471
dateFormat: true,

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ render: function() {
5454
| **onFocus** | `function` | empty function | Callback trigger for when the user opens the datepicker. The callback receives an event of type SyntheticEvent. |
5555
| **onBlur** | `function` | empty function | Callback trigger for when the user clicks outside of the input, simulating a regular onBlur. The callback receives the selected `moment` object as only parameter, if the date in the input is valid. If the date in the input is not valid, the callback returned. |
5656
| **onViewModeChange** | `function` | empty function | Callback trigger when the view mode changes. The callback receives the selected view mode string (`years`, `months`, `days` or `time`) as only parameter.|
57+
| **onNavigateBack** | `function` | empty function | Callback trigger when the user navigates to the previous month, year or decade. The callback receives the amount and type ('month', 'year') as parameters. |
58+
| **onNavigateForward** | `function` | empty function | Callback trigger when the user navigates to the next month, year or decade. The callback receives the amount and type ('month', 'year') as parameters. |
5759
| **viewMode** | `string` or `number` | `'days'` | The default view to display when the picker is shown (`'years'`, `'months'`, `'days'`, `'time'`). |
5860
| **className** | `string` or `string array` | `''` | Extra class name for the outermost markup element. |
5961
| **inputProps** | `object` | `undefined` | Defines additional attributes for the input element of the component. For example: `onClick`, `placeholder`, `disabled`, `required`, `name` and `className` (`className` *sets* the class attribute for the input element). See [Customize the Input Appearance](#customize-the-input-appearance). |

react-datetime.d.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ declare module ReactDatetime {
7979
string ('years', 'months', 'days', 'time') as only parameter.
8080
*/
8181
onViewModeChange?: (viewMode: string) => void;
82+
/*
83+
Callback trigger when the user navigates to the previous month, year or decade.
84+
The callback receives the amount and type ('month', 'year') as parameters.
85+
*/
86+
onNavigateBack?: (amount: number, type: string) => void;
87+
/*
88+
Callback trigger when the user navigates to the next month, year or decade.
89+
The callback receives the amount and type ('month', 'year') as parameters.
90+
*/
91+
onNavigateForward?: (amount: number, type: string) => void;
8292
/*
8393
The default view to display when the picker is shown. ('years', 'months', 'days', 'time')
8494
*/
@@ -92,8 +102,8 @@ declare module ReactDatetime {
92102
*/
93103
inputProps?: Object;
94104
/*
95-
Replace the rendering of the input element. The accepted function has openCalendar
96-
(a function which opens the calendar) and the default calculated props for the input.
105+
Replace the rendering of the input element. The accepted function has openCalendar
106+
(a function which opens the calendar) and the default calculated props for the input.
97107
Must return a React component or null.
98108
*/
99109
renderInput?: (props: Object, openCalendar: Function) => React.Component<any, any>;

test/tests.spec.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,64 @@ describe('Datetime', () => {
10871087

10881088
});
10891089

1090+
describe('onNavigateForward', () => {
1091+
it('when moving to next month', () => {
1092+
const component = utils.createDatetime({ onNavigateForward: (amount, type) => {
1093+
expect(amount).toEqual(1);
1094+
expect(type).toEqual('months');
1095+
}});
1096+
1097+
utils.clickOnElement(component.find('.rdtNext'));
1098+
});
1099+
1100+
it('when moving to next year', () => {
1101+
const component = utils.createDatetime({ viewMode: 'months', onNavigateForward: (amount, type) => {
1102+
expect(amount).toEqual(1);
1103+
expect(type).toEqual('years');
1104+
}});
1105+
1106+
utils.clickOnElement(component.find('.rdtNext'));
1107+
});
1108+
1109+
it('when moving decade forward', () => {
1110+
const component = utils.createDatetime({ viewMode: 'years', onNavigateForward: (amount, type) => {
1111+
expect(amount).toEqual(10);
1112+
expect(type).toEqual('years');
1113+
}});
1114+
1115+
utils.clickOnElement(component.find('.rdtNext'));
1116+
});
1117+
});
1118+
1119+
describe('onNavigateBack', () => {
1120+
it('when moving to previous month', () => {
1121+
const component = utils.createDatetime({ onNavigateBack: (amount, type) => {
1122+
expect(amount).toEqual(1);
1123+
expect(type).toEqual('months');
1124+
}});
1125+
1126+
utils.clickOnElement(component.find('.rdtPrev'));
1127+
});
1128+
1129+
it('when moving to previous year', () => {
1130+
const component = utils.createDatetime({ viewMode: 'months', onNavigateBack: (amount, type) => {
1131+
expect(amount).toEqual(1);
1132+
expect(type).toEqual('years');
1133+
}});
1134+
1135+
utils.clickOnElement(component.find('.rdtPrev'));
1136+
});
1137+
1138+
it('when moving decade back', () => {
1139+
const component = utils.createDatetime({ viewMode: 'years', onNavigateBack: (amount, type) => {
1140+
expect(amount).toEqual(10);
1141+
expect(type).toEqual('years');
1142+
}});
1143+
1144+
utils.clickOnElement(component.find('.rdtPrev'));
1145+
});
1146+
});
1147+
10901148
describe('with set value', () => {
10911149
it('date value', () => {
10921150
const date = new Date(2000, 0, 15, 2, 2, 2, 2),

0 commit comments

Comments
 (0)