Skip to content

Commit c808882

Browse files
committed
Merge branch 'romainberger-onfocus'
2 parents 5f5a44b + 39f4bbe commit c808882

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

DateTime.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var Datetime = React.createClass({
2424
// value: TYPES.object | TYPES.string,
2525
// defaultValue: TYPES.object | TYPES.string,
2626
closeOnSelect: TYPES.bool,
27+
onFocus: TYPES.func,
2728
onBlur: TYPES.func,
2829
onChange: TYPES.func,
2930
locale: TYPES.string,
@@ -45,6 +46,7 @@ var Datetime = React.createClass({
4546
viewMode: 'days',
4647
inputProps: {},
4748
input: true,
49+
onFocus: nof,
4850
onBlur: nof,
4951
onChange: nof,
5052
timeFormat: true,
@@ -256,7 +258,10 @@ var Datetime = React.createClass({
256258
},
257259

258260
openCalendar: function() {
259-
this.setState({ open: true });
261+
if (!this.state.open) {
262+
this.props.onFocus();
263+
this.setState({ open: true });
264+
}
260265
},
261266

262267
closeCalendar: function() {

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ API
5252
| **open** | boolean | null | Wether to open or close the picker. If not set react-datetime will open the datepicker on input focus and close it on click outside. |
5353
| **locale** | string | null | Manually set the locale for the react-datetime instance. Moment.js locale needs to be loaded to be used, see [i18n docs](#i18n).
5454
| **onChange** | function | empty function | Callback trigger when the date changes. The callback receives the selected `moment` object as only parameter, if the date in the input is valid. If it isn't, the value of the input (a string) is returned. |
55+
| **onFocus** | function | empty function | Callback trigger for when the user opens the datepicker. |
5556
| **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 it isn't, the value of the input (a string) is returned. |
5657
| **viewMode** | string or number | 'days' | The default view to display when the picker is shown. ('years', 'months', 'days', 'time') |
5758
| **className** | string | `""` | Extra class names for the component markup. |

tests/datetime-spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,16 @@ describe( 'Datetime', function(){
453453
ev.click( dt.day( 4 ) );
454454
});
455455

456+
it( 'onFocus', function(){
457+
var focus = false;
458+
createDatetime({ value: date, onFocus: function( selected ){
459+
focus = true;
460+
}});
461+
462+
ev.focus( dt.input() );
463+
assert.equal( focus, true );
464+
});
465+
456466
it( 'onBlur', function(){
457467
createDatetime({ value: date, onBlur: function( selected ){
458468
assert.equal( dt.dt().className.indexOf( 'rdtOpen' ), -1 );

0 commit comments

Comments
 (0)